自分のサイトにInstagramの最新の画像を表示するために
phpでInstagram APIを叩いて画像URLを取得できるようにする。
公式のヘルプ
https://developers.facebook.com/docs/instagram
準備
- Facebookアプリを作成
- https://developers.facebook.com > アプリを作成 > アプリタイプ: なし
- 表示名とメールアドレス設定
- アプリダッシュボード画面が表示される
- 設定 > ベーシック > プラットフォームを追加 >
Website
を選択 - サイトURLを入力
- Instagram APIの構成
- Instagramテストユーザーを追加
-
Instagramユーザーで認証
-
トークンを取得
-
ターミナルで以下を叩く
curl -X POST \ https://api.instagram.com/oauth/access_token \ -F client_id=(1.3 で取得したInstagramアプリID) \ -F client_secret=(1.3 で取得したInstagram App Secret) \ -F grant_type=authorization_code \ -F redirect_uri=(2.4 で設定したクライアントOAuth設定のパス) \ -F code=(4.3 で取得した認証コード)
-
以下のjsonが返り、アクセストークンが取得できる
{"access_token": "(access_token)", "user_id": (user_id)}
-
-
今回は、短期 → 長期トークンに切り替える
- 参考 https://developers.facebook.com/docs/instagram-basic-display-api/guides/long-lived-access-tokens?locale=ja_JP
-
ターミナルで以下を叩く
curl -i -X GET "https://graph.instagram.com/access_token ?grant_type=ig_exchange_token &client_secret=(1.3 で取得したInstagram App Secret) &access_token=(5.2 で取得したアクセストークン)"
-
以下のjsonが返り、長期アクセストークンが取得できる(60日間有効)
{"access_token": "(access_token)","token_type":"bearer","expires_in":XXXXXX}
PHP + Instagram API で画像取得
参考: https://developers.facebook.com/docs/instagram-basic-display-api/reference/user/media
-
以下のURLを叩く
curl -i -X GET "https://graph.instagram.com/me/media?fields=id,media_type,media_url&access_token=(6.3 で取得したアクセストークン)"
-
返ったjsonから画像URLが取得できることを確認
{ "data":[ {"id":"XXXXX","media_type":"IMAGE","media_url":"(画像URL)"}, {"id":"XXXXX","media_type":"IMAGE","media_url":"(画像URL)"}, ... ], "paging":{ "cursors":{"before":"XXXXX","after":"XXXXX"} } }
-
phpで取得できるようにする
//アクセストークン $token = "(6.3 で取得したアクセストークン)"; $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, 'https://graph.instagram.com/me/media?fields=id,media_type,media_url,thumbnail_url&access_token=' . $token); curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); $response = curl_exec($curl); curl_close($curl); $result = json_decode($response, TRUE); print $result['data'][0]['media_url'];
-
アクセストークンの更新と、アクセストークンをDBに保持する処理を追加してクラス化
class_insta.phpclass InstagramMedia { private $token; private $db; public function __construct(){ $this->db = new DataBaseModel; } public function get_media_url() { $this->refresh_token(); return $this->get_instagram_media_url(); } private function get_instagram_media_url() { if (!$this->token) { return false; } $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, 'https://graph.instagram.com/me/media?fields=id,media_type,media_url,thumbnail_url&access_token=' . $this->token); curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); $response = curl_exec($curl); $errno = curl_errno($curl); curl_close($curl); if ($errno !== CURLE_OK) { return false; } $result = json_decode($response, TRUE); return $result['data'][0]['media_url']; } private function refresh_token() { $token = $this->get_token(); $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, 'https://graph.instagram.com/refresh_access_token?grant_type=ig_refresh_token&access_token=' . $token); curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); $response = curl_exec($curl); $errno = curl_errno($curl); curl_close($curl); if ($errno !== CURLE_OK) { return false; } $result = json_decode($response, true); print_r($result); $token = $result['access_token']; $this->token = $token; $this->update_token($token); } private function get_token() { $result = $this->db->find('access_token','token', "app = 'instagram'"); return $result['token']; } private function update_token($token) { $this->db->upd('access_token', array('token'=>$token), "app = 'instagram'"); } }
-
呼び出して使えるようにする
get_media_url.phprequire_once dirname(__FILE__) . '/config.php'; require_once dirname(__FILE__) . '/class_db.php'; require_once dirname(__FILE__) . '/class_insta.php'; $insta = new InstagramMedia; print $insta->get_media_url();
完了。
最終的なソースコードはこちら
https://github.com/background-color/php_instagram_media_sandbox