我想知道如何使用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代码。示例代码或指导教程如何工作会很好。感谢您的时间和帮助!