dongxun7962 2015-09-26 04:11
浏览 60

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

I am trying to make a php script that will use this new site called Oanda and trade virtual money on the Forex market.

I am trying to convert this command line code in to php:

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

If anyone can give source code or explain what the -X POST and the -d mean and how to convert them to php that would be awesome.

Thank you for your help!

  • 写回答

1条回答 默认 最新

  • duanchui1955 2015-09-26 04:35
    关注

    Try the below code and if there is any authentication please include them..

    POST Means the data should be passed as post request

    -d Means The data you should pass in the request

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

    Plese reffer Curl for more information

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化