$facebook->api('/me') is returning NULL - php

I am trying to use Facebook connect in Yii framework.
The problem is I want to access user's email address and
$facebook->api('/me') is returning NULL.
How to fix that problem?
Here is my code:
<?php
Yii::import("ext.fconnect.*");
$app_id = "xxx";
$app_secret = "xxxx";
Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYPEER] = false;
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
));
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
$user_profile = $e;
$user = NULL;
}
}
if ($user) {
$d["logoutUrl"] = $facebook->getLogoutUrl(array(
'fblogout' => 'true',
'next' => $this->createAbsoluteUrl("main/signout")
));
$d["user_info"] = $facebook->api('/' . $user);
} else {
$d["loginUrl"] = $facebook->getLoginUrl(array(
'scope' => 'email, read_stream, publish_stream, user_birthday, user_location, user_work_history, user_hometown, user_photos',
'redirect_uri' => $this->request->hostInfo . $this->request->url,
));
}
$d["user"] = $user;
$d["userprofile"] = $user_profile;

The line:
$d["user_info"] = $facebook->api('/' . $user);
Looks wrong to me. I don’t know what data type $user would be; I’d just use $facebook->api('/me'); and that will give you the profile of the currently logged in and authenticated user.

Related

How to get Facebook user ID, post images on user's wall after changes?

Years ago I've developed apps for Facebook, but now seems not working old method to get user's information (id, name, etc) and post data on user's wall.
I've used like this, but for now It returns me blank screen:
require_once('FacebookAPI/Facebook.php');
$facebook = new Facebook(array(
'appId' => 'xxxxx',
'secret' => 'xxxxx',
));
$user = $facebook->getUser();
if ($user)
{
try
{
#Code below to post data on user's wall
# Photo Caption
$photoCaption = 'Just used app';
$imageUrl = ('http://example.com'); // Example URL
$link = ('http://www.facebook.com/page/app_xxxx?ref=ts');
# Post Data for Photos API
$post_data = array(
'message' => $photoCaption,
'link' => $link,
'caption' => 'Caption text'
);
$apiResponse = $facebook->api('/me/feed', 'POST', $post_data);
?>
#Code below to show app
<html>
<body bgcolor="#ffffff">
<center>
// link to my app
</center>
</body>
</html>
// Code below to get user's information (name, id)
<?php
$user_profile = $facebook->api('/me');
$coded = $_REQUEST['code'];
$name = $user_profile['name'];
$id = $user_profile['id'];
session_start();
$_SESSION['name'] = $name;
$_SESSION['id'] = $id;
}
catch (FacebookApiException $e)
{
$user = null;
error_log($e);
}
}
else
{ # Code below to get access from user
$redirectUri = 'http://www.facebook.com/mypage/app_xxxx?ref=ts';
$loginUrl = $facebook->getLoginUrl( array(
'scope' => 'publish_stream,photo_upload',
'redirect_uri' => $redirectUri
));
echo("<script>top.location.href = '" . $loginUrl . "';</script>");
}
Could someone help me out to get successfully user's name, id?
You can try this code. Hope you have downloaded latest php-sdk, you have appid,app des
include_once "php-sdk/src/facebook.php";
$app_id = 'your app id';
$facebook = new Facebook(array(
'appId' => 'your app id',
'secret' => 'your app secret'
));
$user = $facebook->getUser();
if ($user) {
$me = $facebook->api('/me');
$name= $me['name'];
$fid=$me['id'];
}
else
{
$loginUrl = "http://www.facebook.com/dialog/oauth?client_id=" .
$app_id . "&redirect_uri=" . urlencode($app_url) . "&scope=email";
echo("<script> top.location.href='" . $loginUrl . "'</script>");
}

Facebook re-authentication

I'm using PHP SDK to authentication and requesting permissions.
$facebook = new Facebook(array(
'appId' => '7xxx349xx',
'secret' => 'x0b360034c9exxxx4f',
'cookie' => true
));
$params = array(
'scope' => 'publish_actions, read_friendlists',
'redirect_uri' => 'example.com',
);
$user = $facebook->getUser();
$usertoken = $facebook->getAccessToken();
if ($user) {
try {
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
header("Location: xxx.php");
}
if(!$user) {
//$statusUrl = $facebook->getLoginStatusUrl();
$loginUrl = $facebook->getLoginUrl($params);
}
It works well but if user delete app in privacy settings and try to login again I have errors:
CSRF state token does not match one provided.
OR
OAuthException: Error validating access token: The user has not authorized application [APPID]
How to fix it? I need additional code?
Thanks for help

Php Facebook login script ends up in a redirect loop and does not login

This code always runs the last else part and redirects to the login URL provided by facebook api. If i print the $user variable, it just shows 0 everytime.
$facebook = new Facebook(array(
'appId' => 'app id',
'secret' => 'app secret',
));
$user = $facebook->getUser();
$loginUrl = $facebook->getLoginUrl();
if ($user) {
try {
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
$_SESSION['logouturl'] = $logoutUrl;
$_SESSION['user'] = $user_profile['name'];
$_SESSION['id'] = $user_profile['id'];
$_SESSION['type'] = 'facebook';
header('Location: index.php');
} else
{
$loginUrl = $facebook->getLoginUrl();
header('Location: '.$loginUrl);
}
?>
$facebook = new Facebook(array(
'appId' => 'xx',
'secret' => 'xx',
'scope' => 'read_stream, email'
));
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$params = array(
'scope' => 'read_stream, email'
);
$loginUrl = $facebook->getLoginUrl($params);
make sure your app setting on Facebook is good, rest is looking fine and if you have minor problem in you app setting on Facebook developer console it wont work specially domain name and domain url settings

Not getting any information from Facebook login

I'm implementing Facebook login on my website, it's working without error but ultimately I'm not able to get any information of a user.
I've following code.
<?php
include 'library.php';
include 'facebook.php';
$app_id = "XXXXXXXXX";
$app_secret = "XXXXXXXXX";
$site_url = "XXXXXXXXX";
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
));
$user = $facebook->getUser();
if($user){
try{
$user_profile = $facebook->api('/me');
}catch(FacebookApiException $e){
$user = NULL;
}
}
if($user){
$logoutUrl = $facebook->getLogoutUrl();
}else{
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'email',
'redirect_uri' => $site_url,
));
}
if($user){
Echo "Email : " . $user_profile['email'];
}
$user_fbid = $fbuser;
$user_email = $user_profile["email"];
$user_fname = $user_profile["first_name"];
$user_image = "https://graph.facebook.com/".$user_fbid."/picture?type=large";
echo $loginUrl = $facebook->getLoginUrl( array('scope' => 'email,read_stream'));
What's wrong with this? Can anyone point out? I've gone though a number of questions but none of them seemed to work for me & I finally ended up posting a question myself.
EDIT
When asking for login, it displays permissions correctly like email, friend list, your likes etc. So basically it's asking correctly & even though I approve, information is not coming up. Tried with a couple of users.
EDIT
Try use this code, and work from there ... That's how I figured it out
<HTML>
<BODY>
<?php
require_once("fb/facebook.php");
$config = array(
'appId' => 'XXXXX',
'secret' => 'XXXXXXX',
'fileUpload' => false, // optional
'allowSignedRequest' => false, // optional, but should be set to false for non-canvas apps
);
$facebook = new Facebook($config);
$uid = $facebook->getUser();
$loginParams = array('scope' => 'email');
$loginUrl = $facebook->getLoginUrl($loginParams);
$logoutParams = array('next' => 'http://fb_logouturi');
$logoutUrl = $facebook->getLogoutUrl($logoutParams);
echo $uid . '</br>';
echo 'Login:' . $loginUrl . '</br>';
echo 'Logout:' . $logoutUrl . '</br>';
if($uid) {
// We have a user ID, so probably a logged in user.
// If not, we'll get an exception, which we handle below.
try {
$user_profile = $facebook->api('/me','GET');
echo "</br>First Name: " . $user_profile['first_name'];
echo "</br>Last Name: " . $user_profile['last_name'];
echo "</br>Email: " . $user_profile['email'];
echo "</br>Gender: " . $user_profile['gender'];
echo "</br><img src='http://graph.facebook.com/" . $uid ."/picture?type=large'>";
} catch(FacebookApiException $e) {
// If the user is logged out, you can have a
// user ID even though the access token is invalid.
// In this case, we'll get an exception, so we'll
// just ask the user to login again here.
$login_url = $facebook->getLoginUrl();
echo 'Please login.';
error_log($e->getType());
error_log($e->getMessage());
}
} else {
// No user, print a link for the user to login
$login_url = $facebook->getLoginUrl();
echo 'Please login.';
}
?>
</BODY>
</HTML>

Php with Facebook SDK don't load page

I use this code to take information about people friends with Facebook
:
require 'facebook-php-sdk-master/src/facebook.php';
$app_id = 'xxxxxxx';
$app_secret = 'xxxxxxxx';
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
));
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('me/friends');
print_r($user_profile);
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
} else {
$params = array(
'scope' => 'user_birthday',
);
$loginUrl = $facebook->getLoginUrl($params);
echo("<script> top.location.href='" . $loginUrl . "'</script>");
}
?>
But now, a lot of time, when i try to load this , i see that in the url the param "code" start to change a lot of time and the page don't load. What kind of problem is that?
$user is false in your code for some reason (always). May be the facebook user didn't accept your app. $facebook->getLoginUrl($params); redirects to the request uri, with $user false the request uri redirects to $loginUrl. This cause an endless loop.

Categories