dongxie2613
2019-02-22 10:41 阅读 73

PHP JSON响应以字符串值开头

I have some code which is quite simple, it looks to query an API using CURL and return the json response.

Here is the code:

<?php

...[VARIABLES]...

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

$result = curl_exec($ch);
var_dump($result);

?>

Here is the response:

string(310) "{"totalPages":0,"firstPage":true,"lastPage":true,"numberOfElements":0,"number":0,"totalElements":0,"columns":{"columnIds":["metrics/visits:::0","metrics/visitors:::2","metrics/timespentvisit:::4"]},"summaryData":{"totals":[2740.0,1384.0,241.4753313696613]},"oberonRequestXML":[null],"oberonResponseXML":[null]}"

I'm struggling to understand why the string(310) is being shown at the front of the json response, since I am expecting a JSON response only.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    dshmvqnl98119 dshmvqnl98119 2019-02-22 10:45

    use echo to print response

    <?php
    
        ...[VARIABLES]...
    
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_PROXY, $proxy);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    
        $result = curl_exec($ch);
        echo $result;
    
        ?>
    
    点赞 评论 复制链接分享

相关推荐