PHP cURL 中的 cURL 命令


cURL command in PHP cURL

我在命令行中使用此 cURL 命令并且可以工作。

curl --data-binary @/opt/test.xml http://xxxxxxxxxxx/gateway/submit?source=source&conversationId=3039350

但是如何使用 PHP cURL 执行此操作?

我试过这个:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://xxxxxxxx.xxx/xx/xx");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
            "source=source&conversationId=6");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));

// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
// further processing ....
if ($server_output == "OK") { ... } else { ... }

如何在 PHP cURL 中包含 --data-binary @/opt/test.xml

$postdata = array('source' => 'source', 'conversationId' => '6', 'file_contents' => '@' . realpath('/opt/test.xml'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);