curl调用后返回有效的json内容

时间:2022-09-20 23:55:57

I use curl to get a feed from the facebook graph :

我使用curl从facebook图表中获取一个feed:

            $ch = curl_init();
            $options = array(
                CURLOPT_URL         => $url,
                CURLOPT_RETURNTRANSFER  => true,
                CURLOPT_HEADER      => false,
                CURLOPT_FOLLOWLOCATION  => true,
                CURLOPT_ENCODING        => "",
                // CURLOPT_AUTOREFERER  => true,
                // CURLOPT_CONNECTTIMEOUT   => 120,
                // CURLOPT_TIMEOUT      => 120,
                // CURLOPT_MAXREDIRS        => 10,
                CURLOPT_USERAGENT       => 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0'
            );
            curl_setopt_array( $ch, $options );

            $resultat = curl_exec($ch);
            $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
            curl_close($ch);

            // return
            $answer = array(
                'response' => 'ok',
                'resultat' => $resultat
            );

with cakePHP (i added $this->RequestHandler->respondAs('json');).

与cakePHP(我添加了$ this-> RequestHandler-> respondAs('json');)。

but there is a problem in the return : curl调用后返回有效的json内容

但回报中存在问题:

the lines are not correctly displayed, my json reader can't read this, so what is the good option to correct that ? I tried json_encode/decode but it's not better.

线条没有正确显示,我的json阅读器无法读取,所以有什么好的选择来纠正它?我尝试了json_encode / decode,但并不是更好。

1 个解决方案

#1


1  

Try to decode your json and encode it again.

尝试解码你的json并再次编码。

$answer = '{ "key": "Your json values" }'; $answer = json_encode(json_decode($answer));

$ answer ='{“key”:“你的json值”}'; $ answer = json_encode(json_decode($ answer));

#1


1  

Try to decode your json and encode it again.

尝试解码你的json并再次编码。

$answer = '{ "key": "Your json values" }'; $answer = json_encode(json_decode($answer));

$ answer ='{“key”:“你的json值”}'; $ answer = json_encode(json_decode($ answer));