将终端 CURL 转换为 PHP CURL?

Converting terminal CURL to PHP CURL?

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

 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 文件。

您不应该在 url 中输入用户名和密码。您可以将 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);