如何将命令行cURL请求转换为php?

时间:2015-09-26 04:11:59

标签: php curl

我正在尝试制作一个PHP脚本,它将使用这个名为Oanda的新网站并在外汇市场上交易虚拟货币。

我正在尝试将此命令行代码转换为php:

$curl -X POST -d "instrument=EUR_USD&units=1000&side=buy&type=market" https://api-fxpractice.oanda.com/v1/accounts/6531071/orders

如果有人可以提供源代码或解释-X POST-d意味着什么,以及如何将它们转换为php,这将是非常棒的。

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

请尝试以下代码,如果有任何 身份验证 ,请将其包含在内。

POST 表示数据应作为帖子请求传递

-d 表示您应该在请求中传递数据

//the data you should passed
$data = array(
    "instrument" => 'EUR_USD',
    "units" => "1000",
    "side" => "buy",
    "type" => "market",
);

//encode it as json to become a string
$data_string = json_encode($data);
// print_r($data_string);

$curl = curl_init('https://api-fxpractice.oanda.com/v1/accounts/6531071/orders');

curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");

//the content type(please reffer your api documentation)
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($data_string)
));

curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

//set post data
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);

$result = curl_exec($curl);
curl_close($curl);//close the curl request

if ($result) {
    print_r($result); // print the response
}

请更多信息Curl了解更多信息

相关问题