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

    评论

报告相同问题?

悬赏问题

  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择