2013-02-27 5 views
3

PHP 코드가 작동하지 않아서 액세스 토큰을 얻지 못했습니다. 클라이언트 ID와 비밀 키에서 실제 클라이언트 ID와 키로 바뀌 었습니다.Instagram PHP 코드에서 액세스 토큰 가져 오기

<?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를 반환합니다. 왜 이것이 작동하지 않는지 알 수 없습니다.

아무나이 코드를 디버그하고 내 코드의 문제점을 알려주십시오.

답변

0

글쎄 코드에 몇 가지 문제가 있습니다. 하나는 redirect_uri가 localhost 인 것 같습니다. 분명 Instagram의 localhost는 자체 서버입니다. FQDN/세계에 액세스 할 수있는 URL을 제공해야합니다. http://php.net/manual/en/function.curl-setopt.php에서 또한

코드

curl_setopt($ch, CURLOPT_POST, count($apiData)); 

한다 간단이 될

curl_setopt($ch, CURLOPT_POST, 1); 
0

코드는 올바르지 만은 변경할 필요

$redirect_uri = 'your site addres (not localhost)';

예 :

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

이 변경으로이 스크립트를 실행하면이 결과를 얻을 수 있습니다. 이 HTTPS이기 때문에 당신의 리디렉션 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' 
     ) 

) 
2

은 이미 당신은 당신의 코드

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

그것은 아니다에 넣어해야합니다. 컬링 옵션에 SSL_VERIFYPEER가 설정되어 있지 않기 때문입니다

관련 문제