我实施了一个登录按钮,用于检索用户的姓名和电子邮件等数据。似乎正在发生的主要问题是我得到了一个没有找到对象的屏幕,其中包含消息"
在此服务器上找不到请求的网址。
引荐页面上的链接似乎是错误的或过时的#34;在我登录之后。我将我的facebook应用程序中的url设置为localhost / Event.php,但是在这个大的代码放在url之后它不会加载。这只是因为我在本地主机而不是实际服务器上运行它吗?非常感谢任何帮助。
我的代码:
require( __DIR__.'/Facebook/autoload.php' );
$fb = new Facebook\Facebook([
'app_id' => 'my id',
'app_secret' => 'my secret',
'default_graph_version' => 'v2.3',
]);
$helper = $fb->getRedirectLoginHelper();
$permissions = ['email']; // optional
try {
if (isset($_SESSION['facebook_access_token'])) {
$accessToken = $_SESSION['facebook_access_token'];
} else {
$accessToken = $helper->getAccessToken();
}
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
if (isset($accessToken)) {
if (isset($_SESSION['facebook_access_token'])) {
$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
} else {
// getting short-lived access token
$_SESSION['facebook_access_token'] = (string) $accessToken;
// OAuth 2.0 client handler
$oAuth2Client = $fb->getOAuth2Client();
// Exchanges a short-lived access token for a long-lived one
$longLivedAccessToken = $oAuth2Client-
>getLongLivedAccessToken($_SESSION['facebook_access_token']);
$_SESSION['facebook_access_token'] = (string) $longLivedAccessToken;
// setting default access token to be used in script
$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
}
// redirect the user back to the same page if it has "code" GET variable
if (isset($_GET['code'])) {
header('Location: ./');
}
// getting basic info about user
try {
$profile_request = $fb->get('/me?fields=name,first_name,last_name,email');
$profile = $profile_request->getGraphNode()->asArray();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
session_destroy();
// redirecting user back to app login page
header("Location: ./");
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
$name = $profile['first_name'];
$email = $profile['email'];
$eventid = $_GET['id'];
// printing $profile array on the screen which holds the basic info about user
$sql = "INSERT INTO attendees (email, eventid, name) VALUES ('$email', '$eventid', '$name')";
$result = mysqli_query($conn, $sql);
// Now you can redirect to another page and use the access token from $_SESSION['facebook_access_token']
} else {
// replace your website URL same as added in the developers.facebook.com/apps e.g. if you used http instead of https and you used non-www version or www version of your website then you must add the same here
$loginUrl = $helper->getLoginUrl('http://localhost/Events.php', $permissions);
}