2011-09-04 88 views
1
<?php 

define('YOUR_APP_ID', 'x'); 
define('YOUR_APP_SECRET', 'x'); 

function get_facebook_cookie($app_id, $app_secret) { 
    $args = array(); 
    parse_str(trim($_COOKIE['fbs_' . $app_id], '\\"'), $args); 
    ksort($args); 
    $payload = ''; 
    foreach ($args as $key => $value) { 
    if ($key != 'sig') { 
     $payload .= $key . '=' . $value; 
    } 
    } 
    if (md5($payload . $app_secret) != $args['sig']) { 
    return null; 
    } 
    return $args; 
} 

$cookie = get_facebook_cookie(YOUR_APP_ID, YOUR_APP_SECRET); 

$url = 'https://graph.facebook.com/me?access_token=' . $access_token; 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
$response = curl_exec($ch); 
curl_close($ch); 

$user = json_decode($response); 
print_r($user); 

?> 
<html> 
    <body> 
    <?php if ($cookie) { ?> 
     Welcome <?php ?> 
    <?php } else { ?> 
     <fb:login-button></fb:login-button> 
    <?php } ?> 
    <div id="fb-root"></div> 
    <script src="http://connect.facebook.net/en_US/all.js"></script> 
    <script> 
     FB.init({appId: '<?= YOUR_APP_ID ?>', status: true, 
       cookie: true, xfbml: true}); 
     FB.Event.subscribe('auth.login', function(response) { 
     window.location.reload(); 
     }); 
    </script> 
    </body> 
</html> 

有誰知道爲什麼我得到此異常? $ user的print_r正在屈服必須使用活動訪問令牌來查詢有關當前用戶的信息

stdClass Object ([error] => stdClass Object ([type] => OAuthException [message] => An active access token must be used to query information about the current user.)) 

爲什麼會發生這種情況?

回答

1

還有一件事$ access_token在使用之前沒有設置?

+0

是的。正在努力。謝謝你指出。 – Giuseppe

相關問題