2015-01-14 141 views
0

我在命令行中使用了這個cURL命令,它工作正常。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 { ... } 

如何包括--data二進制@ /選擇/的test.xml在PHP捲曲

+0

你會得到任何錯誤? – Scriptable

+0

你是否希望發送'binary'作爲'Content-Transfer-Encoding'? – MackieeE

+0

我讀了一些我將不得不使用application/x-www-form-urlencoded的地方,如果我將它發送給二進制數據 –

回答

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