如何获取用户的Instagram Feed

jay*_*onp 15 php instagram

我想用PHP获取用户的Instagram Feed.我已经注册了一个Instagram开发者帐户并尝试提取用户的信息和照片,但响应并不稳定.有时候我会收到回复,有时候我会收到错误:access_token丢失了.是否有通过用户名获取用户的照片供稿的可靠示例?

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

$instagram = new Instagram();
$photos = $instagram->getPhotos("username-goes-here");
Run Code Online (Sandbox Code Playgroud)

Instagram是一个处理所有请求的类.任何帮助或方向表示赞赏.谢谢!

Ton*_*ark 56

试试这个,

<?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.
  }
?>
Run Code Online (Sandbox Code Playgroud)

愿这对你有所帮助.

  • 这应该标记为@jaysonp接受的答案 (4认同)
  • 这很棒.也可能对某些人有所帮助:获取OAuth令牌:http://jelled.com/instagram/access-token从用户名获取ID:http://jelled.com/instagram/lookup-user-id (4认同)

Loo*_*gLA 6

我这样做了:

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

  }
?>
Run Code Online (Sandbox Code Playgroud)


Jos*_*ndo 5

根据我在互联网和本页面上看到的内容,我在下面创建了一个 Instagram 类(非常简单,仅用于提取提要等)。

class Instagram {
    public static $result;
    public static $display_size = 'thumbnail'; // you can choose between "low_resolution", "thumbnail" and "standard_resolution"
    public static $access_token = "DEFAULTACCESSTOKEN"; // default access token, optional
    public static $count = 10;
    public static function fetch($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;
    }
    function __construct($Token=null){
        if(!empty($Token)){
            self::$access_token = $Token;

            // Remove from memory -- not sure if really needed.
            $Token = null;
            unset($Token);
        }
        self::$result = json_decode(self::fetch("https://api.instagram.com/v1/users/self/media/recent?count=" . self::$count . "&access_token=" . self::$access_token), true);
    }
}
$Instagram = new Instagram('ACCESSTOKENIFCHANGEDORNULLOREMPTY');
foreach ($Instagram::$result->data as $photo) {
    $img = $photo->images->{$Instagram::$display_size};
}
Run Code Online (Sandbox Code Playgroud)