17

PHP を使用してユーザーの Instagram フィードを取得したいと考えています。Instagram 開発者アカウントにサインアップして、ユーザーの情報と写真を取り込もうとしましたが、応答が安定しません。応答が返されることもあれば、「access_token is missing」というエラーが表示されることもあります。ユーザーの写真のフィードをユーザー名で取得する確かな例はありますか?

理想的には、次のようにシンプルにしたいと思います。

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

Instagram は、すべてのリクエストを処理するクラスです。どんな助けや指示も大歓迎です。ありがとう!

4

8 に答える 8

57

これを試して、

<?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.
  }
?>

これがお役に立てば幸いです。

于 2012-07-10T10:13:44.667 に答える
8

これは私がしました:

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

  }
?>
于 2013-08-23T16:03:58.007 に答える
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};
}
于 2014-08-30T07:42:42.563 に答える
2

更新: 2017 年 6 月 15 日 - Instagram がエンドポイントを変更したため、以下は機能しなくなりました。

承認されたアプリケーションなしでランダムなユーザー フィードを取得することはもはや不可能であるため、非公式 API を使用してそれを取得する方法を考え出しました。

#!/bin/bash
instagram_user_id=25025320
count=12
csrftoken=$(curl --head -k https://www.instagram.com/ 2>&1 | grep -Po "^Set-Cookie: csrftoken=\K(.*?)(?=;)")
curl "https://www.instagram.com/query/" -H "cookie: csrftoken=$csrftoken;" -H "x-csrftoken: $csrftoken" -H "referer: https://www.instagram.com/" --data "q=ig_user($instagram_user_id)%20%7B%20media.after(0%2C%20$count)%20%7B%0A%20%20count%2C%0A%20%20nodes%20%7B%0A%20%20%20%20caption%2C%0A%20%20%20%20code%2C%0A%20%20%20%20comments%20%7B%0A%20%20%20%20%20%20count%0A%20%20%20%20%7D%2C%0A%20%20%20%20date%2C%0A%20%20%20%20dimensions%20%7B%0A%20%20%20%20%20%20height%2C%0A%20%20%20%20%20%20width%0A%20%20%20%20%7D%2C%0A%20%20%20%20display_src%2C%0A%20%20%20%20id%2C%0A%20%20%20%20is_video%2C%0A%20%20%20%20likes%20%7B%0A%20%20%20%20%20%20count%0A%20%20%20%20%7D%2C%0A%20%20%20%20owner%20%7B%0A%20%20%20%20%20%20id%2C%0A%20%20%20%20%20%20username%2C%0A%20%20%20%20%20%20full_name%2C%0A%20%20%20%20%20%20profile_pic_url%0A%20%20%20%20%7D%2C%0A%20%20%20%20thumbnail_src%2C%0A%20%20%20%20video_views%0A%20%20%7D%2C%0A%20%20page_info%0A%7D%0A%20%7D" -k

後で PHP でこの回答を改善します。これも PHP で行う必要があります。

于 2016-06-04T17:19:34.743 に答える
1

この関数は App クラスに入りますが、通常の関数にすることができ、とにかく動作します。

<?php
    public function instagram(){
      $user = 'your user here';
      // you can get your token from here: https://instagram.pixelunion.net/
      $access_token = 'your access token here';
      $photo_count = 6;// you can choose the amount. 20 is the max per query

      $json_link = "https://api.instagram.com/v1/users/self/media/recent/?";
      $json_link .="access_token={$access_token}&count={$photo_count}";
      $json = file_get_contents($json_link);
      return json_decode($json);
    }

結果は、このツールを使用してインタラクティブにナビゲートできます: http://jsonviewer.stack.hu/

API によって返される json ファイル

私の場合、ブレード テンプレート エンジンを使用しています ( https://laravel.com/docs/5.8/blade )

したがって、テンプレートは

@foreach($instagram->data as $gram)
   <img src="{{$gram->images->thumbnail->url}}">
@endforeach

それでおしまい!

于 2019-10-22T02:08:12.950 に答える