LoginSignup
46
37

More than 5 years have passed since last update.

Instagram APIの利用方法

Last updated at Posted at 2016-09-12

まずは Instagramのサイトでデベロッパー登録してアプリを登録してください。
調べればたくさんいい情報が出てくるのでアプリ登録などは割愛します。

※まず重要なこととしてInstagramはsandboxという仕様が最近になり追加されて基本は自分の情報しか取得できません。

さらに,サードパーティー製のアプリを認めておらず,画像の投稿はできませんし,自分の情報しか取得できません.公式によると品質を保つための処置とのことですが,到底納得できません.この場を借りてFUCKと言いたい.

他のユーザの写真を扱いたい場合は承認リクエストを送って承認してもらう必要があります。
その場合は下の画像を参考にしてください。デベロッパーページのManage Clientsというタブをクリックするといけます。

instarewuest.png

まずは自分のアクセストークンを取得します。APIを使用するのに必ず必要になります。

https://api.instagram.com/oauth/authorize/?client_id
=あなたのクライエントID&redirect_uri=あなたのリダイレクトURL&response_type=token

リダイレクトURLは何でもいいです。ここでは私のホームページを指定していますが,www.yahoo.co.jpでもいいです.

スクリーンショット 2017-03-22 17.57.09.png

これをブラウザなどで打つとリダイレクトのサイトに飛びURLにアクセストークンが書かれているのでメモってください。

次は自分以外のユーザのデータを扱うために認証をしなければいけません。&scope=public_contentというものをつけると認証画面に飛ぶのでAuthorizedというボタンをクリックしてください。

https://api.instagram.com/oauth/authorize/?client_id
=あなたのクライエントID&redirect_uri=あなたのリダイレクトURL&response_type=token&scope=public_content

次は自分の情報を取得します。

https://api.instagram.com/v1/users/self/?access_token=自分のアクセストークン

すると次のようなJSONデータが返ってきます。

{"meta": {"code": 200}, "data": {"username": "noriakiooshita", "bio": "", "website": "", "profile_picture": "https://scontent.cdninstagram.com/t51.2885-19/s150x150/14309932_672799636207393_810192813_a.jpg", "full_name": "Noriaki  Ooshita", "counts": {"media": 12, "followed_by": 13, "follows": 21}, "id": "3285987949"}}

最後のidと言うのが自分のuserIdとなります。※ユーザ名とは違うので注意

次に自分の最近投稿した画像を取得します。次のURLをGETすると取得できます。

https://api.instagram.com/v1/users/self/media/recent/?access_token=自分のアクセストークン

また&count=というパラメータを与えると取得できるデータ量を調整できます。サンプルは最新の投稿を一つだけ取得します。

https://api.instagram.com/v1/users/self/media/recent/?access_token=自分のアクセストークン&count=1

次にJSONデータを取得するだけのサンプルです。JSONを取得した後は自由に自分でコードを書いてください。

getJson.php(すぐ実行する場合はこっち)
<?php
$userApiUrl = 'https://api.instagram.com/v1/users/self/media/recent/?access_token=あなたのアクセストークン';
// JSON($json)を連想配列に変換(デコード)する
$array = json_decode(@file_get_contents($userApiUrl), true);
echo var_dump($array);
getJson.php(Githubにあげる場合はこっちを使用)
<?php
//getenv('instagram')で環境変数内にあるaccess_tokenを取得する
$userApiUrl = 'https://api.instagram.com/v1/users/self/media/recent/?access_token='.getenv('instagram');
// JSON($json)を連想配列に変換(デコード)する
$array = json_decode(@file_get_contents($userApiUrl), true);
echo var_dump($array);

コードを実行する前に環境変数を設定します。
以下のようにターミナルで実行すると環境変数を設定できます。
export instagram = アクセストークン;

追記 2016/09/16
自分の投稿した画像のURLを取得するコードを書いてみました。

getUrl.php
<?php
// getenv('instagram')で環境変数内にあるaccess_tokenを取得する
// ※アクセストークンを代入したinstagramという環境変数を用意する必要あり。
$userApiUrl = 'https://api.instagram.com/v1/users/self/media/recent/?access_token='.getenv('instagram');
// JSON($json)を連想配列に変換(デコード)する
$array = json_decode(@file_get_contents($userApiUrl),true);
//echo var_dump($array);
// 主な画像だけ取得するstandard_resolutionと言うのが基本的な拡張子の画像
// $array["data"]が自分の画像の個数でcountで取得
for($i = 0;$i < count($array["data"]); $i++) {
    $standardResolution = $array["data"][$i]["images"]["standard_resolution"]["url"];
    // 正規表現で画像のURLを取得する。
    if(preg_match_all('(https?://[-_.!~*\'()a-zA-Z0-9;/?:@&=+$,%#]+(jpg))',$standardResolution, $result) !== false) {
        foreach($result[0] as $value) {
            echo $value . "\n";
         }
    }
}

実行すると以下のように取得できます。(私の投稿です。見てくださいw)

https://scontent.cdninstagram.com/t51.2885-15/s640x640/sh0.08/e35/14350866_230
942723975455_1771702316_n.jpg
https://scontent.cdninstagram.com/t51.2885-15/s640x640/sh0.08/e35/14360215_172
719019802054_309583810_n.jpg
https://scontent.cdninstagram.com/t51.2885-15/s640x640/sh0.08/e35/14309719_117
1936102877324_478963574_n.jpg
https://scontent.cdninstagram.com/t51.2885-15/s640x640/sh0.08/e35/14288132_175
8885511039658_1278510291_n.jpg
https://scontent.cdninstagram.com/t51.2885-15/s640x640/sh0.08/e35/14294978_301
645610195455_2017865958_n.jpg
https://scontent.cdninstagram.com/t51.2885-15/s640x640/sh0.08/e35/14287943_947
099732085435_1635677123_n.jpg
https://scontent.cdninstagram.com/t51.2885-15/s640x640/sh0.08/e35/14280330_178
2771581958885_431802882_n.jpg
https://scontent.cdninstagram.com/t51.2885-15/s640x640/sh0.08/e35/14099391_177
3674969573696_1463047144_n.jpg
https://scontent.cdninstagram.com/t51.2885-15/s640x640/sh0.08/e35/14128701_758
319120976919_1765627975_n.jpg
https://scontent.cdninstagram.com/t51.2885-15/s640x640/sh0.08/e35/14240577_104
8018718677973_316466444_n.jpg
https://scontent.cdninstagram.com/t51.2885-15/s640x640/sh0.08/e35/14145595_281
442478904875_1255757835_n.jpg
https://scontent.cdninstagram.com/t51.2885-15/s640x640/sh0.08/e35/14052784_177
2462993007561_975840683_n.jpg
https://scontent.cdninstagram.com/t51.2885-15/s640x640/sh0.08/e35/14052684_174
4826309103721_159081042_n.jpg
https://scontent.cdninstagram.com/t51.2885-15/s640x640/sh0.08/e35/14027412_176
8536240102378_1872215609_n.jpg

最後にInstagramの私のユーザネームです。フォローしてください。笑

noriakiooshita

46
37
1

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
46
37