2013-04-18 3 views
2

내 이름을 에코하려고하는데 오류가 있습니다. 객체가 아닌 속성을 얻으려고합니다. 도와주세요. 앱 액세스 토큰을받는 데 문제가 없지만 사용자 액세스 토큰을받는 데 문제가 있습니다.(ERROR) 비 객체 (Facebook PHP SDK)의 속성을 얻으려고 시도합니다.

<!DOCTYPE html> 
<html> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
    <title></title> 
</head> 
<body> 
<?php 

require_once('facebook.php'); 
$config = array(); 
$config['appId'] = "MY_APP_ID"; 
$config['secret'] ="MY_APP_SECRET"; 
$facebook = new Facebook($config); 

$app_id = "MY_APP_ID"; 
$app_secret = "MY_APP_SECRET"; 
$my_url = "http://localhost:8080/samplewebsite/index.php/"; 



$code = $_REQUEST['code']; 

if(empty($code)) 
{ 
    $_SESSION['state'] = md5(uniqid(rand(),TRUE)); 
    $dialog_url = "https://www.facebook.com/dialog/oauth?client_id=" 
       . $app_id . "&redirect_uri=" . urlencode($my_url) . "&state=" . $_SESSION['state'] 
       . "&scope=publish_actions,read_stream"; 
    header("Location: " . $dialog_url); 
} 
else 
{ 
    echo "Hello, your OAuth code is " . $code . "<br>";   
} 

if ($_REQUEST['state'] == $_SESSION['state']) 
{ 
    $facebook->api('oauth/access_token',array(
     'client_id' => $app_id, 
     'client_secret' => $app_secret, 
     'type' => 'client_cred', 
     'code' => $code, 
    )); 

    $token = $facebook->getAccessToken(); 
    echo "<br>" . "Hello, Your access_token is: " . $token; 


    $graph_url = "https://graph.facebook.com/me?access_token=" . $token; 
    //$user = json_decode(file_get_contents($graph_url)); 
    //echo $user->name; 

    function curl($url) { 
     $ch = curl_init(); 
     curl_setopt($ch, CURLOPT_HEADER, 0); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser. 
     curl_setopt($ch, CURLOPT_URL, $url); 
     $data = curl_exec($ch); 
     curl_close($ch); 
     return $data; 

    } 
    $user = json_decode(curl($graph_url)); 
    echo $user->name; 
} 




?> 
</body> 
</html> 

답변

0

당신은 기본적으로 객체를 반환하지 않습니다 echo $user->name하지만 json_decode에 노력하고 있습니다. 두 번째 매개 변수로 TRUE을 전달하지 않으면 연관 배열을 반환합니다.

당신은 변경 그래서 경우 :

$user = json_decode(curl($graph_url));

에 :

$user = json_decode(curl($graph_url), TRUE);

사용자가 원하는대로 작동하지 않을 수 있습니다. 그렇지 않다면 curl() 호출이 반환하는 내용과 $user에 실제로 var_dump()과 같은 내용이 포함되어 있는지 확인해보십시오.

+0

감사합니다. 도움을 주신 SilverSnake. 정말로 감사드립니다. 그러나 코딩은 여전히 ​​작동하지 않습니다. –

+0

좋아, 그리고 그것을 분해하고 각 단계에서 데이터를 면밀히 조사한다면? 먼저 $ graph_url을보고 올바른지 확인한 다음 curl() 만 실행하고 반환되는 내용을 살펴본 다음 json_decode로 반환 한 내용을 확인하십시오. 도중에 오류가 있는지 확인하십시오. – SilverSnake

+0

그래, 다시 $ graph_url을 다시 검사 할 것이다. –

관련 문제