我能够手动获取用户的数据,但不能以编程方式。
使用来自 - https://developers.facebook.com/docs/authentication/的服务器端代码
<?php
$app_id = "MY_APP_ID";
$app_secret = "MY_APP_SECRET";
$my_url = "ADDRESS_OF_CURRENT_PAGE";
session_start();
$code = $_REQUEST["code"];
if(empty($code)) {
$_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection
$dialog_url = "https://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url) . "&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)
. "&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);
}
else {
echo("The state does not match. You may be a victim of CSRF.");
}
?>
输出只有你好,用户名不显示!
我认为问题出在 file_get_contents() 上,因为 echo-ing $response 没有输出,而 $token_url 具有适当的值。
$response = @file_get_contents($token_url);
- PHP 版本 = 5.2.9
- 错误报告已关闭,但将其包含在代码中 - error_reporting(E_ALL); 没有输出。
更新- 所以,我在 6 个月后尝试了这个,它奏效了。从那时起唯一改变的是我的托管。我之前使用过 HostBig。教训- 不要依赖每月 1 美元的托管服务,它们是不可信的。