Facebook SDK:未捕获异常 'Exception' 消息 'DateTime::__construct()
Facebook SDK: Uncaught exception 'Exception' with message 'DateTime::__construct()
我正在为 "Login with Facebook" 使用 facebook SDK 我正在使用以下代码。我有 2 个网站,一个带有 SSL 证书,一个带有非 SSL 证书。两者都有不同的托管服务提供商。现在的问题是当我在非 ssl 网站上使用此代码时它工作完美(我也有两个 facebook 应用程序)。但是当我在带有 ssl 证书的网站上使用它时。它给出以下错误:
Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct(): It is not safe to rely on the system's timezone settings. You are required to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone.' in /home/mediahyp/public_html/facebook/src/Facebook/Authentication/AccessToken.php:156 Stack trace: #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/facebook/src/Facebook/Authentication/AccessToken.php on line 156
这是我为此使用的代码:
<?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>';
}
?>
您 运行 在托管服务提供商上安装的 PHP 可能没有在 php.ini 中设置默认时区。
您可以在脚本顶部定义它:
date_default_timezone_set('UTC');
我正在为 "Login with Facebook" 使用 facebook SDK 我正在使用以下代码。我有 2 个网站,一个带有 SSL 证书,一个带有非 SSL 证书。两者都有不同的托管服务提供商。现在的问题是当我在非 ssl 网站上使用此代码时它工作完美(我也有两个 facebook 应用程序)。但是当我在带有 ssl 证书的网站上使用它时。它给出以下错误:
Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct(): It is not safe to rely on the system's timezone settings. You are required to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone.' in /home/mediahyp/public_html/facebook/src/Facebook/Authentication/AccessToken.php:156 Stack trace: #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/facebook/src/Facebook/Authentication/AccessToken.php on line 156
这是我为此使用的代码:
<?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>';
}
?>
您 运行 在托管服务提供商上安装的 PHP 可能没有在 php.ini 中设置默认时区。
您可以在脚本顶部定义它:
date_default_timezone_set('UTC');