dongxie2613 2019-02-22 02:41
浏览 91
已采纳

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 2019-02-22 02: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;
    
        ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部