简体   繁体   中英

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

I was wondering how to post data to a website called Oanda using curl in php. The say that a call should look something like 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 this code to try to do this in php which goes as 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 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);
?>

when I run this without the header it works fine but with the header I get this 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 their something in my code that is wrong or is my code fine but I am using the wrong call in my header. If my code is wrong can you supply me with sample code or give a ling to where one can learn how to fix it? Also Their is another request to their server that I want to make. the site says to make it like 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 any idea what the -X means and how to convert it into php code. sample code or a link to a tutorial on how this works would be great. Thank you for your time and help!

First you need to remove the leading space from your $head variable's value. So:

$head = 'X-Accept-Datetime-Format: UNIX';

Next, to send a custom header, you should say:

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

Where you were doing curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $head); , this was telling the server that your request type (which should normally be something like GET or POST ) is X-Accept-Datetime-Format: UNIX and quite obviously that doesn't make sense for an HTTP/HTTPS request type.

Now for your second question, -X POST is being used as a command line flag to specify that the HTTP request type is POST . So to replicate this, you can write: curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); ( However, if you do not specify a CURLOPT_CUSTOMREQUEST parameter, then, by default, PHP's curl will automatically set the request type to POST if you upload data in the request body; so this line is unnecessary. ) Also to send POST requests, you will need to include the content-length and content-type in your request header.

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

For figuring out what some of the command line options mean that Oanda is using in their examples, check out the curl man pages: http://curl.haxx.se/docs/manpage.html

For more about PHP's implementation of cURL, you should read over the documentation at: http://www.php.net/manual/en/function.curl-setopt.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM