2015-09-26 144 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