title: Parsing Json Response from api call - foreach error
body: <p>I am working with the instagram api. This question is not <code>Instagram</code> 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 <code>foreach</code> loop I attempt the parse but I get this error: <code>Invalid argument supplied for foreach()</code>. How could I parse the json file values properly in the <code>foreach</code> loop? </p>
  
  <p><a href="http://techchurian.wordpress.com/2013/02/13/displaying-all-your-instagram-images-ignoring-next-page-and-pagination/" rel="nofollow">LINK</a> to article that explains the process if not familiar with api</p>
  
  <p><strong>Issue Here</strong>: Trying to parse in <code>foreach loop</code> - Included a test access token. This can be tested from anywhere</p>
  
  <pre><code>$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'];
      }
  </code></pre>
  
  <p>Here’s an example of an Instagram response,</p>
  
  <pre><code>Array
  (
      [0] =&gt; stdClass Object
          (
              [pagination] =&gt; stdClass Object
                  (
                      [next_min_id] =&gt; 1407287255810188
                      [deprecation_warning] =&gt; next_max_id and min_id are deprecated for this endpoint; use min_tag_id and max_tag_id instead
                      [min_tag_id] =&gt; 1407287255810188
                  )
  
              [meta] =&gt; stdClass Object
                  (
                      [code] =&gt; 200
                  )
  
              [data] =&gt; Array
                  (
                      [0] =&gt; stdClass Object
                          (
                              [attribution] =&gt; 
                              [tags] =&gt; Array
                                  (
                                      [0] =&gt; muff
                                      [1] =&gt; hairstylist
                                  )
  //Cut the rest for example purposes
  </code></pre>
  
  <p><em>Make the api call</em></p>
  
  <pre><code>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);
      }
  </code></pre>
  
  <p><em>Fetch all pagination results</em></p>
  
  <pre><code>function getResults($url){
  
          $gotAllResults = false;
          $results = array();
  
          while(!$gotAllResults) {
          $result = $this-&amp;gt;__apiCall($url);
          $results[] = $result;
  
          if (!property_exists($result-&amp;gt;pagination, 'next_url')) {
              $gotAllResults = true;
          } else {
              $url = $result-&amp;gt;pagination-&amp;gt;next_url;
          }
      }
  
      return $results;
  
      }
  </code></pre>