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 ));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效