更新 2019 / 11 / 29

インスタグラムの連携

新たなapi変更により、インスタグラムの写真・動画の取得方法は下記。第3アクセストークン・インスタidを入力。

掲載元:https://oksho.net/other/instagram-graph-api/

css

                        
<?php
          /*     初期設定     */
          $max_posts      = 12; // 投稿の最大取得数
          $graph_api      = 'https://graph.facebook.com/v5.0/';
          $ig_buisiness   = 'インスタID。数字の羅列'; // ビジネスID
          $fields         = 'name,username,profile_picture_url,media_count,followers_count,follows_count,media.limit('. $max_posts. '){caption,like_count,media_type,media_url,permalink,timestamp,thumbnail_url}';
          $access_token   = '第3アクセストークンを記載'; // 無期限のページアクセストークン
          $cache_lifetime = 3600; // キャッシュ時間の設定 (最短更新間隔 [sec])
          $show_mode      = 'grid'; // 表示形式 (グリッド表示は 'grid'、一覧表示は 'list')


          /*     取得処理     */
          if(!file_exists(dirname(__FILE__). '/cache/')){
              if(mkdir(dirname(__FILE__). '/cache/', 0774)){
                  chmod(dirname(__FILE__). '/cache/', 0774);
              }
          }
          $cache_lastmodified = @filemtime(dirname(__FILE__). '/cache/instagram_graph_api.dat');
          if(!$cache_lastmodified){
              $ig_json = @file_get_contents($graph_api. $ig_buisiness. '?fields='. $fields. '&access_token='. $access_token);
              file_put_contents(dirname(__FILE__). '/cache/instagram_graph_api.dat', $ig_json, LOCK_EX);
          } else{
              if(time() - $cache_lastmodified > $cache_lifetime){
                  $ig_json = @file_get_contents($graph_api. $ig_buisiness. '?fields='. $fields. '&access_token='. $access_token);
                  file_put_contents(dirname(__FILE__). '/cache/instagram_graph_api.dat', $ig_json, LOCK_EX);
              } else{
                  $ig_json = @file_get_contents(dirname(__FILE__). '/cache/instagram_graph_api.dat');
              }
          }
          // 取得したJSON形式データを配列に展開する
          if($ig_json){
              $ig_data = json_decode($ig_json);
              if(isset($ig_data->error)){
                  $ig_data = null;
              }
          }
          // データ取得に失敗した場合
          if(!$ig_json || !$ig_data){
              header('HTTP', true, 500);
              exit;
          }
          ?>


          <ul class="insta-img-list">
          <?php
            if(is_array($ig_data->media->data)){
              foreach($ig_data->media->data as $post){
                $caption = $post->caption;
                $caption = mb_convert_encoding($caption, 'UTF8', 'ASCII,JIS,UTF-8,EUC-JP,SJIS-WIN');
                $caption = preg_replace('/\n/', '', $caption);
          ?>
          <li class="item">
            <a href="<?php echo $post->permalink; ?>" class="instagram" target="_blank" rel="noopener" title="likes:<?php echo $post->like_count; ?>">
              <img src="<?php if($post->media_type=='VIDEO'){ echo $post->thumbnail_url; }else{ echo $post->media_url; } ?>" alt="<?php echo $caption; ?>">
            </a>
          </li>
          <?php
                  }
              }
          ?>
          </ul>