Facebook SDK:带有消息“DateTime::__construct()”的未捕获异常“异常”
Posted
技术标签:
【中文标题】Facebook SDK:带有消息“DateTime::__construct()”的未捕获异常“异常”【英文标题】:Facebook SDK: Uncaught exception 'Exception' with message 'DateTime::__construct() 【发布时间】:2016-02-27 23:06:49 【问题描述】:我将 facebook SDK 用于“使用 Facebook 登录”我正在使用以下代码。我有 2 个网站,一个带有 SSL 证书,一个带有非 SSL 证书。两者都有不同的托管服务提供商。现在的问题是,当我在非 ssl 网站上使用此代码时,它可以完美运行(我也有两个 facebook 应用程序供他们使用)。但是当我在带有 ssl 证书的网站上使用它时。它给出了以下错误:
致命错误:未捕获的异常 'Exception' 带有消息 '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/facebook/src /Facebook/Authentication/AccessToken.php 第 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>';
?>
【问题讨论】:
【参考方案1】:您遇到此问题的托管服务提供商上的 PHP 安装可能没有在 php.ini 中设置默认时区。
您可以在脚本顶部定义它:
date_default_timezone_set('UTC');
【讨论】:
以上是关于Facebook SDK:带有消息“DateTime::__construct()”的未捕获异常“异常”的主要内容,如果未能解决你的问题,请参考以下文章