Jump to content

Looping logic on JSON


gorgon2k

Recommended Posts

Hi guys, I'm at a logical roadblock on how to implement something I'm doing. I'm working with the Instagram API(not important to this question) and in their JSON response they give you 20 results and a link to the next 20 results. I'm trying to build an OOP library that can get the response from the initial API call, search if it has a "next_url" value, and if it does, get the next 20 responses and so un until there's no longer an next URL. I can't quite gather the logic on how to do this...

 

currently here's the code to get the the first 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;

    
    }

 

and here's a snippet from the JSON response....

 

	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
                                (
                                )

                        )

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.