简体   繁体   English

在PHP中使用curl将标题和URL发布到网站?

[英]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. 我想知道如何使用php中的curl将数据发布到名为Oanda的网站。 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" 通话的说法应该像这样: 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中执行以下操作:

<?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" } 当我在没有标题的情况下运行此文件时,它工作正常,但在标题下却出现以下错误: 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 该网站说要这样: $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. 我不知道-X的含义以及如何将其转换为php代码。 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. 首先,您需要从$head变量的值中删除前导空格。 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); 你在哪里做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. ,这是在告诉服务器您的请求类型(通常应为GETPOST )是X-Accept-Datetime-Format: UNIX ,很显然,这对于HTTP / HTTPS请求类型没有意义。

Now for your second question, -X POST is being used as a command line flag to specify that the HTTP request type is POST . 现在,对于第二个问题, -X POST被用作命令行标志,以指定HTTP请求类型为POST So to replicate this, you can write: curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); 因此,要复制此代码,您可以编写: 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. 但是,如果您未指定CURLOPT_CUSTOMREQUEST参数,则默认情况下,如果您在请求正文中上传数据,PHP的curl将自动将请求类型设置为POST ;因此此行是不必要的。 )另外,要发送POST请求,您您需要在请求标头中包含content-length和content-type。

For more on HTTP POST requests in curl, check out this link: http://www.lornajane.net/posts/2011/posting-json-data-with-php-curl 有关curl中HTTP POST请求的更多信息,请查看以下链接: 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 要弄清楚Oanda在示例中使用的某些命令行选项,请查看curl手册页: 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 有关PHP的cURL实现的更多信息,您应该阅读以下文档: http : //www.php.net/manual/zh/function.curl-setopt.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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