如何获取用户的 Instagram 动态

2022-08-30 23:52:45

我想使用PHP获取用户的Instagram提要。我注册了Instagram开发者帐户,并尝试提取用户的信息和照片,但响应不稳定。有时我得到回应,有时我不断收到错误:access_token丢失。有没有一个可靠的例子来获取用户通过用户名的照片提要?

理想情况下,我希望它像这样简单:

$instagram = new Instagram();
$photos = $instagram->getPhotos("username-goes-here");

其中Instagram是一个处理所有请求的类。任何帮助或指导都是值得赞赏的。谢谢!


答案 1

试试这个,

<?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/ID-GOES-HERE/media/recent/?access_token=TOKEN-GOES-HERE");
  $result = json_decode($result);
  foreach ($result->data as $post) {
    // Do something with this data.
  }
?>

愿这对你有所帮助。


答案 2

我这样做了:

<?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>';
     }

  }
?>

推荐