LoginSignup
1
3

More than 3 years have passed since last update.

ホームページにinstagramの写真を埋め込む。

Posted at

表示部分

index.html
<div class="instagram"></div>

js部分

instagram.js
$(function(){
    var $container = $(".instagram");
    var html = "";
    $.ajax({
        url: "instagram.php",//PHPファイルURL
        type:"POST",
        dataType: "json"
    }).done(function(data){
        //通信成功時の処理
        var counter = 0;
        $.each(data.data,function(i,item){
            if(counter === 6) {
                return false;
            }
            var imgurl = item.images.low_resolution.url; //低解像度の画像のURLを取得
            var link = item.link; //リンクを取得
            html += "<div><a href='" + link + "' target='_blank'><img src='" + imgurl + "'></a></div>";
            counter++;
        });
    }).fail(function(){
        //通信失敗時の処理
        html = "<li>画像が取得できませんでした。</li>";
    }).always(function(){
        //通信完了時の処理
        $container.html(html);
    });
});

PHP部分

instagram.php
<?php
//POSTリクエストの場合のみ受付
if($_SERVER['REQUEST_METHOD'] == 'POST'){
    //アクセストークン
    $access_token = "アクセストークンを入れる";
    //JSONデータを取得して出力
    echo @file_get_contents("https://api.instagram.com/v1/users/self/media/recent/?access_token={$access_token}");
    //終了
    exit;
}
?>

これで無事に出来た。

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3