Instagram从PHP中的代码获取访问令牌

Instagram从PHP中的代码获取访问令牌,instagram,Instagram,下面的php代码不适合我获取访问令牌 在客户端id和密钥中,我已替换为真实的客户端id和密钥 <?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.insta

下面的php代码不适合我获取访问令牌 在客户端id和密钥中,我已替换为真实的客户端id和密钥

<?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。。我不明白这为什么不起作用


有人调试这段代码并告诉我代码中有什么问题吗

您的代码有一些问题。首先,您的重定向uri似乎是localhost。显然Instagram的本地主机是他们自己的服务器。您需要提供FQDN/world可访问url

也来自代码

应该是简单的吗

$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';

您的代码是正确的,但需要更改

$redirect\u uri='您的站点地址(非本地主机)'

例如:

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

)
如果使用此更改执行此脚本,则可以获得此结果

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);  
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, false);

因为它已经是https了,所以你需要把它放在你的代码中

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

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

)
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);  
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, false);