title: How to get instagram photos with PHP by API?
body: <p>Last two days I was trying to display photos from my Instagram profile by raw PHP. But I'm getting these errors.</p>
  
  <pre><code>Notice: Trying to get property of non-object in E:\xampp7\htdocs\instagram\index.php on line 29
  
  Warning: Invalid argument supplied for foreach() in E:\xampp7\htdocs\instagram\index.php on line 29
  </code></pre>
  
  <p>I created proper <strong>authentication</strong> from instagram API. My requested API <code>clients access</code> is valid (<code>clientId, userId, accessToken</code>). When I failed with PHP than I tried with a JavaScript Plugin <a href="http://instafeedjs.com/" rel="nofollow noreferrer">instafeedjs</a>. Now It’s working well. Here is my PHP code, given below:-</p>
  
  <pre><code>&lt;body&gt;
  &lt;?php
  function fetchData($url){
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_TIMEOUT, 20);
      $result = curl_exec($ch);
      curl_close($ch);
      return $result;
      }
  
      $result = fetchData("https://api.instagram.com/v1/users/3550421370/media/recent/?access_token=MY_VALID_ACCESS_TOKEN_IS_HERE");
  
      $result = json_decode($result);
      foreach ($result-&gt;data as $post) {
      if(empty($post-&gt;caption-&gt;text)) {
      // Do Nothing
      }
      else {
      echo '&lt;a class="instagram-unit" target="blank" href="'.$post-&gt;link.'"&gt;
          &lt;img src="'.$post-&gt;images-&gt;low_resolution-&gt;url.'" alt="'.$post-&gt;caption-&gt;text.'" width="100%" height="auto" /&gt;
          &lt;div class="instagram-desc"&gt;'.htmlentities($post-&gt;caption-&gt;text).' | '.htmlentities(date("F j, Y, g:i a", $post-&gt;caption-&gt;created_time)).'&lt;/div&gt;&lt;/a&gt;';
      }
  }
  
  ?&gt;
  
  &lt;/body&gt;
  </code></pre>