json_decode呈现Response内容必须是实现__toString()的字符串或对象

时间:2013-07-25 16:26:47

标签: php json laravel

我使用Laravel和函数json_decode。除此之外,我在一个流行的模块中使用它,它在我自己的非常简单的测试用例中不起作用。它似乎与我的设置/设置有关。 (我在pagodabox和本地运行它)。我提供的数据来自Twitter / Instagram,因此内容为JSON。

以下是一个不起作用的示例:

<?php

class InstagramController extends BaseController {

    /*
    |--------------------------------------------------------------------------
    | Default Home Controller
    |--------------------------------------------------------------------------
    |
    | You may wish to use controllers instead of, or in addition to, Closure
    | based routes. That's great! Here is an example controller method to
    | get you started. To route to this controller, just add the route:
    |
    |   Route::get('/', 'HomeController@showWelcome');
    |
    */

    public function read($q)
    {
        $client_id = '#############';

        $uri = 'https://api.instagram.com/v1/tags/'.$q.'/media/recent?client_id='.$client_id;
        $response = $this->sendRequest($uri);

        return json_decode($response);
    }

    public function sendRequest($uri){
        $curl = curl_init($uri);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
        $response = curl_exec($curl);
        curl_close($curl);
        return $response;
    }

}

0 个答案:

没有答案