json - using curl in php to post a head and URL to a website? -


i wondering how post data website called oanda using curl in php. call should this: curl -i -h "x-accept-datetime-format: unix" "https://api-fxpractice.oanda.com/v1/candles?instrument=eur_usd&start=137849394&count=1"

i have written code try in php goes follows:

<?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 @ output curl_setopt($ch, curlopt_customrequest, $head); // http request 'head' $content = curl_exec ($ch); echo "<p>" . $content . "</p>"; curl_close ($ch); ?> 

when run without header works fine header error: 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" }

is in code wrong or code fine using wrong call in header. if code wrong can supply me sample code or give ling 1 can learn how fix it? request server want make. site says make this: $curl -x post -d "instrument=eur_usd&units=1000&side=buy&type=market" https://api-fxpractice.oanda.com/v1/accounts/6531071/orders

i don't have idea -x means , how convert php code. sample code or link tutorial on how works great. thank time , help!

first need remove leading space $head variable's value. so:

$head = 'x-accept-datetime-format: unix'; 

next, send custom header, should say:

curl_setopt($ch, curlopt_httpheader, array($head) ); 

where doing curl_setopt($ch, curlopt_customrequest, $head);, telling server request type (which should get or post) x-accept-datetime-format: unix , quite doesn't make sense http/https request type.

now second question, -x post being used command line flag specify http request type post. replicate this, can write: curl_setopt($ch, curlopt_customrequest, 'post'); (however, if not specify curlopt_customrequest parameter, then, default, php's curl automatically set request type post if upload data in request body; line unnecessary.) send post requests, need include content-length , content-type in request header.

for more on http post requests in curl, check out link: http://www.lornajane.net/posts/2011/posting-json-data-with-php-curl

for figuring out of command line options mean oanda using in examples, check out curl man pages: http://curl.haxx.se/docs/manpage.html

for more php's implementation of curl, should read on documentation at: http://www.php.net/manual/en/function.curl-setopt.php


Comments