Facebook Connect- $User_Id = $Facebook->Getuser(); Always Returning 0

facebook-getUser() returning 0

Change this

$loginUrl = $facebook->getLoginUrl(array ( 
'display' => 'popup',
'redirect_uri' => 'http://localhost/demo'
));

to

$loginUrl = $facebook->getLoginUrl(array ( 
'display' => 'popup',
'redirect_uri' => 'http://localhost/demo/index.php'
));

And see if this works !!

Facebook PHP SDK $uid = $facebook-getUser() always returns 0

In window.fbAsyncInit I have set cookie and oauth to true and this worked!

cookie:true,    
oauth : true.

Make sure that In your php you also have

cookie' => true.

That will enable the php code to integrare with the JS.

Facebook login not working: getUser() always return 0

I had this problem too. I did this...

/**** FB APP DEETS  ****/
DEFINE ('APPID', '123');
DEFINE ('APPSECRET', 'xyz');
DEFINE ('ACCESS_TOKEN', 'biglongtokenthinginhere');

$config = array(
'appId' => APPID,
'secret' => APPSECRET,
);

$facebook = new Facebook($config);
$facebook->setAccessToken(ACCESS_TOKEN);

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

Then carry on with if($user) etc

So I believe you are missing the access token

To extend your access token, I used this. I added it before the if($user) and ran it once then commented it out. It displays a new access token on screen which you place in the script and it wont expire. Normally they only last for 2 weeks or something like that

 /*
* EXTEND TOKEN - NOW EXPIRES NEVER
*/

//$graphUrl = 'https://graph.facebook.com/oauth/access_token?client_id='.APPID .'&client_secret='.APPSECRET.'&grant_type=fb_exchange_token&fb_exchange_token='.ACCESS_TOKEN;
//
//$accessToken = @file_get_contents($graphUrl);
//parse_str($accessToken); //get the access_token param in the string and would be named $access_token
//if(!$access_token) $access_token = $accessToken; //if cannot be extended then just return the access token with 2 hours expiry
//echo $access_token;
//
//exit(__FILE__.__LINE__);

Why is Facebook PHP SDK getUser always returning 0?

The answer to my specific issue was that there were incompatibilities between the versions of the JS SDK and PHP SDK I was using and just upgrading them solved it.

The symptom of this issue is similar when it's caused by a variety of different things so you may do very well in scouting through the different answers available in this page.

Why does facebook $user_id = $facebook-getUser(); return 0 in-spite of being logged in?

The function _accessServer opens another request back to your server, sending the access token.

The function _loginPopup should open the Facebook login popup requesting the appropriate permissions for the user to "allow access" to your application.

The PHP application should then pass the access token back to the Facebook API

Facebook API : getUser() always returning 0

The first thing to check would be

require_once 'facebook-php-sdk/src/facebook.php';

Make sure it is relative to where your php code is rather than to the server structure.

To test this code I would probably just use the existing example provided by Facebook sdk, that way you get it working first by confidently knowing that it really works and can then tweak it as per your needed.
Just pasting it from this link
https://github.com/facebook/facebook-php-sdk/blob/master/examples/example.php

<?php
/**
* Copyright 2011 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

require '../src/facebook.php';

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

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

// We may or may not have this data based on whether the user is logged in.
//
// If we have a $user id here, it means we know the user is logged into
// Facebook, but we don't know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.

if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} 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();
} else {
$loginUrl = $facebook->getLoginUrl();
}

// This call will always work since we are fetching public data.
$naitik = $facebook->api('/naitik');

?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<title>php-sdk</title>
<style>
body {
font-family: 'Lucida Grande', Verdana, Arial, sans-serif;
}
h1 a {
text-decoration: none;
color: #3b5998;
}
h1 a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<h1>php-sdk</h1>

<?php if ($user): ?>
<a href="<?php echo $logoutUrl; ?>">Logout</a>
<?php else: ?>
<div>
Login using OAuth 2.0 handled by the PHP SDK:
<a href="<?php echo $loginUrl; ?>">Login with Facebook</a>
</div>
<?php endif ?>

<h3>PHP Session</h3>
<pre><?php print_r($_SESSION); ?></pre>

<?php if ($user): ?>
<h3>You</h3>
<img src="https://graph.facebook.com/<?php echo $user; ?>/picture">

<h3>Your User Object (/me)</h3>
<pre><?php print_r($user_profile); ?></pre>
<?php else: ?>
<strong><em>You are not Connected.</em></strong>
<?php endif ?>

<h3>Public profile of Naitik</h3>
<img src="https://graph.facebook.com/naitik/picture">
<?php echo $naitik['name']; ?>
</body>
</html>

facebook php sdk $this-getUser always returning 0

I had same problem before, after days f searching i solved it in two ways if you are sure about all configuration ... I think you have to solutions to try:
1- host your application on web hosting server and try it with the domain your will get.
2- use javascript sdk to get the access_token then pass it to php skd, like this:

              window.fbAsyncInit = function() {
FB.init({
appId : 'appId', // App ID
channelUrl : 'channel.html', // Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
// Additional init code here

FB.Event.subscribe('auth.login', function(response) {
var accessToken = response.authResponse.accessToken;
window.location.href = "http://page_to_read_process_the _request?"accessToken="+accessToken;
});

in php you only shoudl do like this:

$facebook->setAccessToken($accessToken);
$facebook->getUser();

if this solved your problem: please do not forget to vote :D



Related Topics



Leave a reply



Submit