我使用facebook SDK“登录Facebook”我使用以下代码。我有2个网站,一个是SSL证书,一个是非SSL证书。两者都有不同的托管服务商现在问题是当我在非ssl网站上使用这个代码时,它工作得很完美(我还有两个facebook应用程序)。但是当我在网站上使用ssl证书时。它给出了以下错误:
致命错误:消息'DateTime :: __ construct()的未捕获异常'异常':依赖系统的时区设置是不安全的。您必需使用date.timezone设置或date_default_timezone_set()函数。如果您使用了这些方法中的任何一种并且仍然收到此警告,则很可能拼错了时区标识符。我们现在选择了时区'UTC',但请设置date.timezone来选择您的时区。在/home/mediahyp/public_html/facebook/src/Facebook/Authentication/AccessToken.php:156堆栈跟踪:#0 /home/mediahyp/public_html/facebook/src/Facebook/Authentication/AccessToken.php(156):DateTime- > __ construct()#1 /home/mediahyp/public_html/facebook/src/Facebook/Authentication/AccessToken.php(57):Facebook \ Authentication \ AccessToken-> setExpiresAtFromTimeStamp(1453623903)#2 / home / mediahyp / public_html / facebook / src / Facebook / Authentication / OAuth2Client.php(247):Facebook \ Authentication \ AccessToken-> __ construct('CAALR8BH5vloBAP ...',1453623903)#3 / home / mediahyp / public_ in / home / mediahyp / public_html /第156行的facebook / src / Facebook / Authentication / AccessToken.php
以下是我正在使用的代码:
<?php
session_start();
require_once __DIR__ . '/src/Facebook/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => 'app_id',
'app_secret' => 'app_secret',
'default_graph_version' => 'v2.4',
]);
$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 {
$response = $fb->get('/me?fields=name,first_name,last_name,email, gender,relationship_status');
$profile = $response->getGraphUser();
} 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;
}
// printing $profile array on the screen which holds the basic info about user
echo $profile->getName();
echo "<br>";
echo $profile->getEmail();
echo "<br>";
echo $profile->getID();
echo "<br>";
echo $profile->getGender();
echo "<br>";
echo $profile->getrelationship_status();
// 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('https://www.website.com/', $permissions);
echo '<a href="' . $loginUrl . '">Log in with Facebook!</a>';
}
?>
答案 0 :(得分:3)
您遇到此问题的托管服务提供商上的PHP安装可能没有在php.ini中设置默认时区。
您可以在脚本顶部定义它:
date_default_timezone_set('UTC');