Instagram从PHP中的代码获取访问令牌 [英] Instagram Get Access Token from Code in PHP

查看:127
本文介绍了Instagram从PHP中的代码获取访问令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下php代码对我来说不起作用,无法获取访问令牌 在客户端ID和密钥中,我已替换为真实的客户端ID和密钥.

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;
}
?>

上面的代码总是返回false..我不知道为什么这不起作用.

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?

推荐答案

由于已经是https,因此您需要将其放在代码中

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);

这与您的重定向uri是否在本地无关.这是因为未在curl选项中设置SSL_VERIFYPEER

It's not about your redirect uri is in local or not. It's because SSL_VERIFYPEER is not set in your curl option

这篇关于Instagram从PHP中的代码获取访问令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆