2015-09-26 105 views
0

我想做一个PHP脚本,将使用这个名为Oanda的新网站,并在外汇市场上交易虚拟货币。如何将一个命令行的cURL请求转换为php?

我想这个命令行代码转换到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这将是真棒。

谢谢你的帮助!

回答

0

尝试下面的代码,如果有任何认证请包括他们..

POST指本数据应POST请求

传递 - d意味着数据你应该pa SS在请求

//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 
} 

普莱舍reffer Curl了解更多信息

0
// create a new cURL resource 
$ch = curl_init(); 

// set URL and other appropriate options 
    $defaults = array(
    CURLOPT_URL => 'https://api-fxpractice.oanda.com/v1/accounts/6531071/orders', 
    CURLOPT_POST => true, 
    CURLOPT_POSTFIELDS => "instrument=EUR_USD&units=1000&side=buy&type=market"); 

    curl_setopt_array($ch, $defaults); 

// grab URL and pass it to the browser 
    $exec = curl_exec($ch); 

    // close cURL resource, and free up system resources 
curl_close($ch); 

if ($exec) { 
    print_r($exec); //print results 
} 

并回答你的问题:

卷曲 - X POST意味着一个HTTP POST请求,-d参数(长 版本:--data)告诉curl接下来将是POST参数

如果您想了解更多信息,你可以在这里找到:cURL Functions这里: Manual -- curl usage explained