Client ID, Client Secretを発行し、php、mysqlでInstagramのOauthの仕組みを使ったログインを実装していきます。
リフェレンス
http_build_query:Generates a URL-encoded query string from the associative (or indexed) array provided.
PDO::prepare — 文を実行する準備を行い、文オブジェクトを返す
$dbh = new PDO:PDOオブジェクトのメソッドでデータベースを操作、$dbhという変数でPDOオブジェクトを管理
fetch:フィールドの配列参照として次の行を取り出す
Instagram Authentication: https://www.instagram.com/developer/authentication/
<?php
require_once('config.php');
session_start();
if (empty($_GET['code'])){
// 認証前の準備
$params = array(
'client_id' => CLIENT_ID,
'redirect_uri' => SITE_URL.'redirect.php',
'scope' => 'basic',
'response_type' => 'code'
);
$url = 'https://api.instagram.com/oauth/authorize/?'.http_build_query($params);
header('Location: '.$url);
exit;
// instagramへ飛ばす
} else {
// 認証後の処理
// usr情報の取得
$params = array(
'client_id' => CLIENT_ID,
'client_secret' => CLIENT_SECRET,
'code' => $_GET['code'],
'redirect_uri' => SITE_URL.'redirect.php',
'grant_type' => 'authorization_code'
);
$url = "https://api.instagram.com/oauth/access_token";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($params));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$res = curl_exec($curl);
curl_close($curl);
var_dump($res);
exit;
//user情報の格納
// ログイン処理
// index.phpに飛ばす
}