0

我在如何实现我正在做的事情上遇到了一个合乎逻辑的障碍。我正在使用 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
                                (
                                )

                        )
4

2 回答 2

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函数名称中使用两个下划线。这让我觉得神奇的事情正在发生。

于 2012-04-18T19:28:06.030 回答
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;

    }
于 2012-04-19T14:03:05.080 回答