Abdullah
Abdullah

Reputation: 175

Facebook SDK: Uncaught exception 'Exception' with message 'DateTime::__construct()

I am using facebook SDK for "Login with Facebook" I am using the following code. I have 2 website one with SSL certificate one with non SSL certificate. Both have different hosting provider. Now the problem is when I use this code on non-ssl website it work perfect ( I have also two facebook apps for them ). But when I use this on website with ssl certificate. It gives the following error:

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

Here is the code I am using for it:

<?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>';
}

?>

Upvotes: 1

Views: 1865

Answers (1)

oroboto
oroboto

Reputation: 226

The PHP installation on the hosting provider on which you're running into this problem probably hasn't set a default timezone in php.ini.

You can define it at the top of your script:

date_default_timezone_set('UTC');

Upvotes: 3

Related Questions