0

I am working with the instagram api. This question is not Instagram specific but more about parsing the response returned by the api call. The api works with pagination. With some research I found the way to fetch all the results in one huge bulk and avoid pagination. I am having an issue parsing the json file. In the foreach loop I attempt the parse but I get this error: Invalid argument supplied for foreach(). How could I parse the json file values properly in the foreach loop?

LINK to article that explains the process if not familiar with api

Issue Here: Trying to parse in foreach loop - Included a test access token. This can be tested from anywhere

$get_media_url = 'https://api.instagram.com/v1/tags/nataliguatemipoa/media/recent?access_token=6678174.467ede5.205a03ebc4b74d4082823781c3149575';

$media = getResults($get_media_url); //Look Below to find function details
foreach($media['data'] as $insta){
        $source_id = $insta['id'];
    }

Here’s an example of an Instagram response,

Array
(
    [0] => stdClass Object
        (
            [pagination] => stdClass Object
                (
                    [next_min_id] => 1407287255810188
                    [deprecation_warning] => next_max_id and min_id are deprecated for this endpoint; use min_tag_id and max_tag_id instead
                    [min_tag_id] => 1407287255810188
                )

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

            [data] => Array
                (
                    [0] => stdClass Object
                        (
                            [attribution] => 
                            [tags] => Array
                                (
                                    [0] => muff
                                    [1] => hairstylist
                                )
//Cut the rest for example purposes

Make the api call

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

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

        // Close cURL session
        curl_close ($curl_session);

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

Fetch all pagination results

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;

    }

2 Answers 2

1

The $insta is an object, not an array. You should be referencing as such:

foreach($media[data] as $insta){
    $source_id = $insta->id;
}

Objects are referenced as ->. Arrays are referenced as []

One of two options here, you could try referencing it as this first one:

foreach($media['data'] as $insta){
    $source_id = $insta->id;
}

Or you could try as an object

foreach($media->data as $insta){
    $source_id = $insta->id;
}
Sign up to request clarification or add additional context in comments.

8 Comments

Ok it is still giving the same error of Invalid argument supplied for foreach()
What does print_r($media) supply?
But is data present in there?
Yes data is present
Showing that print_r($media) would be great so we can help you debug
|
0

Try with casting it to array -

foreach((array)$media[0]['data'] as $insta) {
   $source_id = $insta->id;
}

8 Comments

Ok so the error goes away but no values are placed in the foreach loop. It gives this error Notice: Undefined index: data in /index.php on line 226
var_dump($media) and add see what you are getting or display it.
array(1) { [0]=> object(stdClass)#3 (3) { ["pagination"]=> object(stdClass)#4 (3) { ["next_min_id"]=> string(16) "1407287255810188" ["deprecation_warning"]=> string(94) "next_max_id and min_id are deprecated for this endpoint; use min_tag_id and max_tag_id instead" ["min_tag_id"]=> string(16) "1407287255810188" } ["meta"]=> object(stdClass)#5 (1) { ["code"]=> int(200) } ["data"]=> array(3) { [0]=> object(stdClass)#6 (15)
It yield this error Cannot use object of type stdClass as array
it was coming because the $insta is an object.if $insta has id in next level then you can access it else again loop must be added.updated the answer.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.