在JSON上循环逻辑

时间:2012-04-18 19:00:02

标签: php json loops

我在如何实现我正在做的事情上遇到了逻辑上的障碍。我正在使用Instagram API(对这个问题不重要),在他们的JSON响应中,他们会给你20个结果,并链接到接下来的20个结果。我正在尝试构建一个OOP库,它可以从初始API调用中获取响应,搜索它是否具有“next_url”值,如果是,则搜索接下来的20个响应,直到不再存在下一个URL 。我不能完全掌握如何做到这一点的逻辑......

目前这里是获得前20名的代码......

function __apiCall($url, $post_parameters = FALSE) {

        $curl_session = curl_init();

        curl_setopt($curl_session, CURLOPT_URL, $url);

        if($post_parameters !== FALSE) {
            curl_setopt ($curl_session, CURLOPT_POSTFIELDS, $post_parameters);
        }

        // Return the curl results to a variable
        curl_setopt($curl_session, CURLOPT_RETURNTRANSFER, 1);

        curl_setopt($curl_session, CURLOPT_SSL_VERIFYPEER, $this->codeigniter_instance->config->item('instagram_ssl_verify'));

        $contents = curl_exec ($curl_session);

        curl_close ($curl_session);

        $return = json_decode($contents);
        return $return;


    }

这里是JSON响应的片段....

    stdClass Object
(
    [pagination] => stdClass Object
        (
            [next_url] => https://api.instagram.com/v1/locations/3937885/media/recent?min_timestamp=&min_id=&max_timestamp=&access_token=xxxxxxxxxxxx49414762bea69258210d8872&max_id=133226850290424667_21341717
            [next_max_id] => 133226850290424667_21341717
        )

    [meta] => stdClass Object
        (
            [code] => 200
        )

    [data] => Array
        (
            [0] => stdClass Object
                (
                    [tags] => Array
                        (
                        )

                    [type] => image
                    [location] => stdClass Object
                        (
                            [latitude] => 39.95022
                            [name] => Neiman Group
                            [longitude] => -75.168322
                            [id] => 3937885
                        )

                    [comments] => stdClass Object
                        (
                            [count] => 0
                            [data] => Array
                                (
                                )

                        )

2 个答案:

答案 0 :(得分:2)

function getResults($url)
{
    $gotAllResults = false;
    $results = array();

    while(!$gotAllResults) {
        $result = $this->__apiCall($url);
        $results[] = $result;

        if (!property_exist($result->pagination, 'next_url') {
            $gotAllResults = true;
        } else {
            $url = $result->pagination->next_url;
        }
    }

    return $results;
}

function __apiCall($url)
{
        // settings for cURL

        $contents = curl_exec($curl_session);

        curl_close ($curl_session);

        $result = json_decode($contents);
        return $return;
}

顺便说一句。为什么在__apiCall函数的名称中使用两个下划线。这让我觉得magical正在发生一些事情。

答案 1 :(得分:0)

function __apiCall($url, $post_parameters = FALSE) {

        // Initialize the cURL session
        $curl_session = curl_init();

        // Set the URL of api call
        curl_setopt($curl_session, CURLOPT_URL, $url);

        // If there are post fields add them to the call
        if($post_parameters !== FALSE) {
            curl_setopt ($curl_session, CURLOPT_POSTFIELDS, $post_parameters);
        }

        // Return the curl results to a variable
        curl_setopt($curl_session, CURLOPT_RETURNTRANSFER, 1);

        // There was issues with some servers not being able to retrieve the data through https
        // The config variable is set to TRUE by default. If you have this problem set the config variable to FALSE
        // See https://github.com/ianckc/CodeIgniter-Instagram-Library/issues/5 for a discussion on this
        curl_setopt($curl_session, CURLOPT_SSL_VERIFYPEER, $this->codeigniter_instance->config->item('instagram_ssl_verify'));

        // Execute the cURL session
        $contents = curl_exec ($curl_session);

        // Close cURL session
        curl_close ($curl_session);

        // Return the response
        return  json_decode($contents);


    }

    function getResults($url){

        $gotAllResults = false;
        $results = array();

        while(!$gotAllResults) {
        $result = $this->__apiCall($url);
        $results[] = $result;

        if (!property_exists($result->pagination, 'next_url')) {
            $gotAllResults = true;
        } else {
            $url = $result->pagination->next_url;
        }
    }

    return $results;

    }