Reputation: 199
Following php code is not working for me to get access token In client id and secret key, i have replaced with my real client id and key.
<?php
$client_id = 'MY CLIENT ID';
$client_secret = 'MY CLIENT SECRET';
$redirect_uri = 'http://localhost/instagram/demo.php';
$scope = 'basic+likes+comments+relationships';
$url = "https://api.instagram.com/oauth/authorize?client_id=$client_id&redirect_uri=$redirect_uri&scope=$scope&response_type=code";
if(!isset($_GET['code']))
{
echo '<a href="'.$url.'">Login With Instagram</a>';
}
else
{
$code = $_GET['code'];
$apiData = array(
'client_id' => $client_id,
'client_secret' => $client_secret,
'grant_type' => 'authorization_code',
'redirect_uri' => $redirect_uri,
'code' => $code
);
$apiHost = 'https://api.instagram.com/oauth/access_token';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiHost);
curl_setopt($ch, CURLOPT_POST, count($apiData));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($apiData));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$jsonData = curl_exec($ch);
curl_close($ch);
var_dump($jsonData);
$user = @json_decode($jsonData);
echo '<pre>';
print_r($user);
exit;
}
?>
Above code always return false.. I can not figure out why this is not working.
Anybody debug this code and let me know what is problem in my code?
Upvotes: 3
Views: 12518
Reputation: 41
Since it's https already so you need to put this on your code
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, false);
It's not about your redirect uri is in local or not. It's because SSL_VERIFYPEER is not set in your curl option
Upvotes: 3
Reputation: 64
Your code is correct, but you need change
$redirect_uri = 'your site addres (not localhost)';
Example:
$client_id = 'YOUR_CLIENT_ID';
$client_secret = 'YOUR CLIENT SECRET';
$redirect_uri = 'http://www.YOUR_SERVER.com/instagram/demo.php';
$scope = 'basic+likes+comments+relationships';
If you execute this script with this change you can obtain this result.
stdClass Object
(
[access_token] => 'your_ID'.xxxxxxx.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
[user] => stdClass Object
(
[username] => 'your username'
[bio] =>
[website] =>
[profile_picture] => 'your url picture profile'
[full_name] =>
[id] => 'your_ID'
)
)
Upvotes: 0
Reputation: 326
Well there are a few issues with your code. For one it seems like your redirect_uri is localhost. Obviously localhost to Instagram is their own servers. You need to provide a FQDN/world accessible url.
Also from http://php.net/manual/en/function.curl-setopt.php the code
curl_setopt($ch, CURLOPT_POST, count($apiData));
Should simple be
curl_setopt($ch, CURLOPT_POST, 1);
Upvotes: 0