将终端CURL转换为PHP CURL?

时间:2016-11-10 20:50:01

标签: php curl php-curl

如何使用此test.xml命令在PHP中提交此CURL文件:

 curl -X POST -k -d @test.xml -H "Content-Type: application/xml" --user username:password https://www.url.com

这对我不起作用:

 $ch = curl_init();

 //Get the response from cURL
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

 //Set the Url
 curl_setopt($ch, CURLOPT_URL, 'https://username:password@url.com');

 //Create a POST array with the file in it
 $postData = array(
'testData' => '@test.xml',
 );
 curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);

 // Execute the request
 $response = curl_exec($ch);

我也想知道如何提交用代码而不是XML文件编写的XML。

1 个答案:

答案 0 :(得分:1)

您不应该在网址中输入用户名和密码。您可以curl_setopt($p, CURLOPT_USERPWD, $username . ":" . $password);使用$username$password。如下:

$p = curl_init($url);
curl_setopt($p, CURLOPT_HTTPHEADER, array('Content-Type: application/xml'));
curl_setopt($p, CURLOPT_HEADER, 1);
curl_setopt($p, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($p, CURLOPT_POSTFIELDS, $postData);
curl_setopt($p, CURLOPT_POST, 1);
curl_setopt($p, CURLOPT_RETURNTRANSFER, TRUE);
$return = curl_exec($p);
curl_close($p);