doulingzhuang3079 2016-06-21 02:22
浏览 51
已采纳

curl命令进入php

I am able to use the api token and the command below to receive a json response that I need. The issue is when I try to transition into php. Here is the working curl example (obviously minus the string api token).

curl -v -D - -H 'Authorization: Token token="[private_api_token_here]"' -H "Accept: application/json" -H "Content-type: application/json" -X GET -d ' {"status":"collected,pending","transaction_type":"sale,credit","amount_min":"50.00"}' "https://app.mobilecause.com/api/v2/reports/transactions.json?"

Here is my latest attempt in php.

    $json = '{
              "status": "collected",
              "transaction_type": "sale",
              "amount_min": "50.00"
            }';

        $assoc_array = json_decode($json);
        $urlEncodedString = http_build_query($assoc_array);
        $URL = "https://app.mobilecause.com/api/v2/reports/transactions.json?" . $urlEncodedString;
        $ch = curl_init();
        $header = array();
        $header[] = 'Content-length: 0';
        $header[] = 'Content-type: application/json';
        $header[] = 'Authorization: Bearer [private_api_token_here]';
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_URL, $URL);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLINFO_HEADER_OUT, true);
        $info = curl_getinfo($ch);
        $json = json_decode(curl_exec($ch));
        curl_close($ch);
        var_dump($json);

It seems to me that I am not quite getting the proper headers submitted. I kept the api token as a plain string. In previous attempts I submitted the token as base 64 encoded. In addition, I tried the below as plain string and base 64 to no avail:

'Authorization: Token [private_api_token_here]'

and

'Authorization: Token token=[private_api_token_here]'

I appreciate any ideas.

  • 写回答

1条回答 默认 最新

  • douqiao1997 2016-06-21 02:33
    关注

    Please try this:

    $ch = curl_init();    
    curl_setopt($ch, CURLOPT_URL, "https://app.mobilecause.com/api/v2/reports/transactions.json?");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, " {\"status\":\"collected,pending\",\"transaction_type\":\"sale,credit\",\"amount_min\":\"50.00\"}");
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
    
    
    $headers = [];
    $headers[] = "Authorization: Token token=\"[private_api_token_here]\"";
    $headers[] = "Accept: application/json";
    $headers[] = "Content-Type: application/json";
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    
    $result = curl_exec($ch);
    if (curl_errno($ch)) {
        echo 'Error:' . curl_error($ch);
    }
    curl_close ($ch);
    
    var_dump(json_decode( $result ));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘