我正在使用以下腳本從Facebook示例:獲取用戶主頁城市和當前位置
腳本抓住電子郵件罰款,但故鄉和位置觸發我的服務器上500錯誤。我需要獲取統計數據。
<?php
$app_id = "API_ID_GOES_HERE";
$app_secret = "SECRET_GOES_HERE";
$my_url = "REDIRECT_URL_GOES_HERE";
$permissions ="user_hometown,user_location,email";
session_start();
$code = $_REQUEST["code"];
if(empty($code)) {
$_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection
$dialog_url = "http://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url) . "&scope=".$permissions.
"&state="
. $_SESSION['state'];
echo("<script> top.location.href='" . $dialog_url . "'</script>");
}
if($_REQUEST['state'] == $_SESSION['state']) {
$token_url = "https://graph.facebook.com/oauth/access_token?"
. "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url)
. "&scope=".$permissions. "&client_secret=" . $app_secret . "&code=" . $code;
$response = file_get_contents($token_url);
$params = null;
parse_str($response, $params);
$graph_url = "https://graph.facebook.com/me?access_token="
. $params['access_token'];
$user = json_decode(file_get_contents($graph_url));
echo("Hello " . $user->name);
echo ("Location ". $user->location);
}
else {
echo("The state does not match. You may be a victim of CSRF.");
}
?>
首先要做'的print_r($用戶);'並檢查這些參數是否到來。 –
我得到的信息包括我請求的權限信息,但它們顯示如下/ me:stdClass Object([id] => 111806898837603 [name] => Adana,Turkey)[location] => stdClass Object([ id] => 106270412737566 [name] =>科羅拉多州科羅拉多斯普林斯) 那麼如何通過json獲得stdClass對象(注意我將它們全部存儲爲php變量) –
$ user-> hometown-> name輸出是家鄉的名字。謝謝你的幫助。 –