Search code examples
javascriptphpinstagram-api

How do I add Instagram feed in website in PHP?


i have used this code.

<?php foreach ($result->data as $post){ ?>
    <!-- Renders images. @Options (thumbnail,low_resoulution, high_resolution) -->
    <a class="group" rel="group1" href="<?= $post->images->standard_resolution->url ?>"><img src="<?= $post->images->thumbnail->url ?>"></a>
<?php  } ?>

I want to add Instagram feed in my website but I am not able to do that please help me with this.


Solution

  • You can get Instagram feed like this:

    <?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/USER ID HERE/media/recent/?access_token=ACCES TOKEN HERE&count=14");
    
    
      $result = json_decode($result);
      foreach ($result->data as $post) {
         if(empty($post->caption->text)) {
           // Do Nothing
         }
         else {
            echo '<a class="instagram-unit" target="blank" href="'.$post->link.'">
            <img src="'.$post->images->low_resolution->url.'" alt="'.$post->caption->text.'" width="100%" height="auto" />
            <div class="instagram-desc">'.htmlentities($post->caption->text).' | '.htmlentities(date("F j, Y, g:i a", $post->caption->created_time)).'</div></a>';
         }
    
      }
    ?>
    

    Resource: How to get a user's Instagram feed