Created
June 8, 2016 09:10
-
-
Save tkc/ea88c7a26117573975e1282e29b56381 to your computer and use it in GitHub Desktop.
instagram api get token By php server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$client_id = <client_id>; | |
$client_secret = <client_secret>; | |
$redirect_uri = <redirect_url>; | |
$scope = 'basic+public_content+comments+relationships+likes'; | |
session_start(); | |
if (isset($_GET['code']) && !empty($_GET['code']) && isset($_SESSION['state']) && !empty($_SESSION['state']) && isset($_GET['state']) && !empty($_GET['state']) && $_SESSION['state'] == $_GET['state']) { | |
$context = array( | |
'http' => array( | |
'method' => 'POST', | |
'content' => http_build_query(array( | |
'client_id' => $client_id, | |
'client_secret' => $client_secret, | |
'grant_type' => 'authorization_code', | |
'redirect_uri' => $redirect_uri, | |
'code' => $_GET['code'], | |
)), | |
), | |
); | |
$curl = curl_init(); | |
curl_setopt($curl, CURLOPT_URL, 'https://api.instagram.com/oauth/access_token'); | |
curl_setopt($curl, CURLOPT_HEADER, 1); | |
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $context['http']['method']); | |
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($curl, CURLOPT_POSTFIELDS, $context['http']['content']); | |
curl_setopt($curl, CURLOPT_TIMEOUT, 5); | |
$res1 = curl_exec($curl); | |
$res2 = curl_getinfo($curl); | |
curl_close($curl); | |
$json = substr($res1, $res2['header_size']); | |
$header = substr($res1, 0, $res2['header_size']); | |
$obj = json_decode($json); | |
print_r($obj); | |
} else { | |
session_regenerate_id(true); | |
$state = sha1(uniqid(mt_rand(), true)); | |
$_SESSION['state'] = $state; | |
header('Location: https://api.instagram.com/oauth/authorize/?client_id=' . $client_id . '&redirect_uri=' . rawurlencode($redirect_uri) . '&scope=' . $scope . '&response_type=code&state=' . $state); | |
exit; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment