dlopezgonzalez
dlopezgonzalez

Reputation: 4297

Stuck using facebook php sdk. GetUser always returns 0

I have got this code to login with the user if there is not anyone.

index.php, located in http://DOMAIN.COM/PATH.

Here, if the user id returned is 0, the login url is taken from facebook instance and a redirection is made to that url to login with a facebook user. Then, after the authorize action, the next url iss "http://DOMAIN.COM/PATH/check.php" where the "getUser" method is again executed, however, the user id is 0 again.

`

$fbPermissions = 'read_stream, publish_stream, user_birthday, user_location, user_work_history, user_hometown, user_photos';
$site_url   = "http://DOMAIN.COM/PATH/check.php";
$facebook = new Facebook(array(  
            'appId'  => 'MY_ID',  
            'secret' => 'MY_SECRET_ID',   
            'cookie' => true  
            ));  

$user = $facebook->getUser(); 

if($user) { 

    try{  
            echo "user id insid if=".$user;
            $user = $facebook->api('/me');  
    } catch (Exception $e){}  

} else {  
    $loginUrl = $facebook->getLoginUrl(array('scope'=>$fbPermissions,'redirect_uri'=>$site_url));
    header('location:'.$loginUrl.'');
} 
?>

Code of check.php

<?php
include_once("facebook.php");

$fbPermissions = 'read_stream, publish_stream, user_birthday, user_location, user_work_history, user_hometown, user_photos';
$facebook = new Facebook(array(  
            'appId'  => 'MY_ID',  
            'secret' => 'MY_SECRET_ID',  
            'cookie' => true  
            ));  

$user = $facebook->getUser(); 

if($user) { 

    try{  
            echo "user id insid if=".$user;
            $user = $facebook->api('/me');  
    } catch (Exception $e){}  

} else {  
    echo "No user";
} 
?>

Thus, in options in my app of facebook developers page, I have the " Site URL" option with this value "http://DOMAIN.COM/PATH/". I have had to create a Website platform in "configuration" section.

I am working in a internet hosting not in localhost.

Which can be the problem? I am really stuck

Upvotes: 1

Views: 5101

Answers (5)

Shailendra2014
Shailendra2014

Reputation: 819

simple understandable code of SDK 4.0 integration with PHP is here https://github.com/ssgaur/Facebook-Login-PHP-SDK-v4

Upvotes: 0

user1299518
user1299518

Reputation:

A little late but i just solved my similar problem (->getUser() = 0).

First, i'm using the Facebook SDK 3.x.x (that requires PHP 5.2) and not the latest Facebook SDK 4.0 (that requires PHP 5.4). Till now i've just used/included 2 files of the SDK (facebook.php and base_facebook.php) and, for some reason, ignored the 3rd certificate file named fb_ca_chain_bundle.crt.

The result was that while i logged-in normally (through ->getLoginUrl() and a custom JS popup window), facebook api was always given me =0 on ->getUser() call, even if i was able to surf to facebook and verify i was truly connected through my own popup.

That until i decided to look over my apache logs and to find that, astonishingly descriptive error of 'Invalid or no certificate authority found, using bundled information'. I quickly searched facebook SDK code and found that message is related to the above .crt file.

After i included the file in the same folder as the 2 above, my ->getUser() call was able to return my facebook account information.

Upvotes: 0

SMSM
SMSM

Reputation: 1519

Its Happen to me and when i check my Apache log i found this :

enter image description here

And i found its an SSL problems when communicate with Facebook via PHP SDK (using Curl). You have to set CURL_OPTS "CURLOPT_SSL_VERIFYPEER" to "false" .

Ex:

facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYPEER] = false; 

Upvotes: 0

Stephanus Budiwijaya
Stephanus Budiwijaya

Reputation: 137

What is your PHP version? Is it 5.4.x? If so then you need to change the PHP SDK code a little bit.

I have had the same problem with you and I found this article really help me a a lot. facebook-php-sdk-fixing-getuser-on-php-5-4-x

Upvotes: 0

dlopezgonzalez
dlopezgonzalez

Reputation: 4297

Ok. The code is correct. The problem was one o several options of the facebook app. I am not sure what was the option that I changed, I put the probably options of my app in order to help this common problem.

Web Site App.

Stream post URL security => Off Native or desktop app? => Off

Hope its helps, this problems is really common how it's seen if you search with this topic.

Upvotes: 1

Related Questions