简体   繁体   中英

Trouble with Facebook Api login $user is not returning properly

When I run this code, and my cookies have been cleared and I am not logged into facebook. It directs me to facebook and I log in but when it brings me back to my page it is the same, where as it should be show me profile pic and so on...

I seem to have narrowed down the problem of the return statement of the $user because it returns a 0. I have stared at this code for a long time and I havent found what I am doing wrong.

What do i need to change to get it so that When i return from logging in from facebook it will show the profile pic and so on...

<?php

require_once 'libs/facebook.php';
require 'connections/connection.php';

// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
            'appId' => 'xxxxxxxxxxxxx',
            'secret' => 'xxxxxxxxxxxxxxxx',
        ));

// Get User ID
$user = $facebook->getUser();
echo $user;

if ($user) {
    try {
        // Proceed knowing you have a logged in user who's authenticated.
        $user_profile = $facebook->api('/me');
        echo $user;
    } catch (FacebookApiException $e) {
        error_log($e);
        $user = null;
    }
}

// Login or logout url will be needed depending on current user state.
if ($user) {
    //$logoutUrl = $facebook->getLogoutUrl();
    $logoutUrl = $facebook->getLogoutUrl(array('next' => ($fbconfig['baseurl'] . 'logout.php')));
} else {
    $statusUrl = $facebook->getLoginStatusUrl();
    $loginUrl = $facebook->getLoginUrl(array(
        'scope' => 'user_about_me',
        'scope' => 'read_friendlists'
            ));
}
?>   

Not to worry my friend, you are not doing anything wrong. I'm having same issue with my DEMO application which was running alright till yesterday.

I'm sure you are having issue with getting access token as well. I think issue is with PHP SDK only.So probably will be fixed in some time.

SOLUTION : This worked for me after trying for many solutions for this issue.

In base_facebook.php file, find the makeRequest() method and check for following Line.

$opts = self::$CURL_OPTS;  

Immediately following it, add this line

$opts[CURLOPT_SSL_VERIFYPEER] = false;  

More details can be found here - http://net.tutsplus.com/tutorials/php/how-to-authenticate-your-users-with-facebook-connect/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM