我想知道如何使用curl在php中將數據發佈到名爲Oanda的網站。該說的調用應該是這個樣子:curl -i -H "X-Accept-Datetime-Format: UNIX" "https://api-fxpractice.oanda.com/v1/candles?instrument=EUR_USD&start=137849394&count=1"
在PHP中使用curl將頭部和URL發佈到網站?
我寫了這個代碼,試圖做到這一點在PHP肚裏如下:
<?php
$ch = curl_init();
$url = "http://api-sandbox.oanda.com/v1/prices?instruments=EUR_USD&side=buy";
$head = " X-Accept-Datetime-Format: UNIX";
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true); // header will be at output
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $head); // HTTP request is 'HEAD'
$content = curl_exec ($ch);
echo "<p>" . $content . "</p>";
curl_close ($ch);
?>
當我運行這個無頭正常工作但與頭我得到這個錯誤:HTTP/1.1 400 Bad Request Server: openresty/1.7.0.1 Date: Fri, 25 Sep 2015 17:18:56 GMT Content-Type: application/json Content-Length: 137 Connection: close ETag: "53f4b7f5-89" { "code" : 400, "message" : "Bad Request", "moreInfo" : "http:\/\/developer.oanda.com\/docs\/v1\/troubleshooting\/#errors" }
是他們在我的代碼是錯的,或者是我的代碼好,但我在我的頭中使用錯誤的電話。如果我的代碼錯了,你能否給我提供示例代碼,或者給我一個可以學習如何解決它的方法?另外他們是我想要製作的服務器的另一個請求。該網站說,使它是這樣的:$curl -X POST -d "instrument=EUR_USD&units=1000&side=buy&type=market" https://api-fxpractice.oanda.com/v1/accounts/6531071/orders
我不知道什麼-X的手段和如何將其轉換爲PHP代碼。示例代碼或指導教程如何工作會很好。感謝您的時間和幫助!