duskosobic
1/25/2017 - 10:34 AM

Retrieve Images from Instagram with PHP, not JS. We are using PHP's CURL library to make our requests.

Retrieve Images from Instagram with PHP, not JS. We are using PHP's CURL library to make our requests.

<?php
  // https://www.instagram.com/developer/endpoints/users/
  // https://blueprintinteractive.com/blog/how-instagram-api-fancybox-simplified
  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);
      if(curl_error($ch))
      {
          return 'error:' . curl_error($ch);
      }else return $result;
  }
  $result = fetchData("https://api.instagram.com/v1/users/1521783613/media/recent/?access_token=1521783613.1677ed0.ddcab4df753740f1b76a0f3a4934fee0");
  $result = json_decode($result);
  foreach ($result->data as $post) {
      // Do something with this data.
  }
?>