2016-10-03 153 views
-2

我有命令如何将命令行cURL转换为PHP cURL?

curl -v -X POST -H "Content-Type: application/xml" -d @case.xml --user test:test url 

在PHP发送

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL,$url); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml')); 
curl_setopt($ch, CURLOPT_USERPWD, "test:test"); 
curl_setopt($ch, CURLOPT_POSTFIELDS, ['data' => new CurlFile('case.xml')]); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 
$result = curl_exec($ch); 
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
curl_close($ch); 

但是这个代码不工作,error 400 Bad Request

Web服务器使用基本HTTP认证。我使用的是7.0.11

+0

什么是您的$ URL中包含,是指给我看你的网址是什么? –

+0

是的,$ url ='http://example.com'; – vladimir

+1

[如何使用PHP curl使用HTTP基本身份验证进行请求?](http://stackoverflow.com/questions/2140419/how-do-i-make-a-request-using-http-basic -authentication-with-php-curl) – halfer

回答

0
<?php 
$login='test'; 
$password='test'; 
$xml_data =file_get_contents(realpath('case.xml')); 
$url ='mega_url'; 
$ch = curl_init($url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch,CURLOPT_HEADER,true); 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml')); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data); 
curl_setopt($ch, CURLOPT_USERPWD, "$login:$password"); 
$output = curl_exec($ch); 

是工作