facebook graph api getUser returns 0 - facebook

I create new app and try to get user
require_once 'facebook.php';
$this->fb = new Facebook(array(
'appId' => APPID,
'secret' => APPSECRET,
'cookie' => true
));
$user = $this->fb->getUser();
I am using new sdk 3.2.2. On my testing environment its work until today. But now its always returns 0.
Can anybody help me?

Just based on that code there is no login handling for example getLoginUrl(), unless the user has logged in through a login flow, $user will always be zero.

Related

Get user's country at Facebook: getSignedRequest() results without 'user' field

Code:
$facebook = new Facebook(array(
'appId' => some_fb_app_id),
'secret' => some_fb_app_secret),
'cookie' => true,
));
$result = $facebook->getSignedRequest();
echo var_export($result,1);
Result:
array (
'algorithm' => 'HMAC-SHA256',
'code' => 'A.....l',
'issued_at' => 1354720771,
'user_id' => 'some_user_id',
)
I expect $result["user"]["country"] to be set. How can I fix this problem?
ps. by Facebook Graph API - how to get user country?
According to the facebook PHP SDK getSignedRequest() only gives you the signed request, just like the name suggests. User info is usually gotten through the Graph API, using calls like
$facebook->api('/me','GET');
which gives you the profile for the user associated with the access_token you are using.
Note that you will only ever get information that you have the permissions for, so if you haven't added a particular bit of info beyond the basics to your apps scope, you will not get it.

login from one domain and after that getting facebook object in another domain using php sdk 3.1.1

I am logining in and making the authentication processes from one domain (localhost)
$config = array(
appId' => $APP_ID,
'secret' => $APP_SECRET
);
$facebook = new Facebook($config);
$currentUserId = $facebook->getUser();
This works fine and i get an Access Token and a user id.
After that. from the browser i am making a post action to a php file on a remote domain
in the remote php i use the same code the same APP_ID and the same APP_SECRET
$config = array(
'appId' => $APP_ID,
'secret' => $APP_SECRET
);
$facebook = new Facebook($config);
$currentUserId = $facebook->getUser();
this does not! work and get $currentUserId = 0
and that is my problem
I can use (but dont want to):
$facebook->setAccessToken($at);
with the acsess token from that i got form the localhost domain it does work and i get the user id
In older sdk 2.0 I have seen this function setBaseDomain
Is there a way i can use it in 3.1.1?
here's what I do - so when you make the request to the other domain pass along the access_token when you make the API call do something like this -
$config = array(
'appId' => $APP_ID,
'secret' => $APP_SECRET
);
$facebook = new Facebook($config);
$args['access_token'] = $_POST['token'] // or $_GET[];
$me = $facebook->api('/me', 'get', $args);
print_r($me);
So what that does then is say to FB I know the request is coming from a different server, but I have a valid token, please accept this sacrifice to mark zuckerberg and let me do things. It will accept and you're free to do as you please :)

Facebook significant_other_id

I need a little help about a PHP FB app.
Matter is that I wan't to access the significant_other_id as $relationId.
First I get the authorization by user via link:
https://www.facebook.com/dialog/permissions.request?app_id=XXX&display=page&next=XLINK&response_type=code&perms=publish_stream,user_relationships
Then after authorization in php file I try this:
<?php
include('src/facebook.php');
$facebook = new Facebook(array(
'appId' => 'XX',
'secret' => 'XX',
'cookie' => true,
));
$user_profile = $facebook->api('/me','GET');
echo $user_profile['relationship_status']; //Prints ok, requested info
echo $user_profile['significant_other_id']; //Prints nothing
I wasn't able to get that id :S if anyone has experience on this your reply would be great.
Thanks.
According to the documentation, significant_other comes back as an object containing name and id, so try:
echo $user_profile['significant_other']['id'];

How to login with OFFLINE_ACCESS using the new Facebook PHP SDK 3.0.0?

with the old (2.x) SDK I used this to log someone with offline_access:
$session = array
(
'uid' => $userdata['fb_uid'],
'sig' => $userdata['fb_sig'],
'access_token' => $userdata['fb_access_token']
);
$facebook->setSession($session);
In the new SDK this function doesnt exist anymore. I think I need to login using:
setPersistentData($key, $value)
but this function is protected and I dont know what 'code' is? Do I need this to log the user in or not? And what's going on with 'sig'? Don't I need this anymore?
Hope someone already figured this out because the documentation really doesn't help!
With the Facebook PHP SDK v3 (see on github), it is pretty simple. To log someone with the offline_access permission, you ask it when your generate the login URL. Here is how you do that.
Get the offline access token
First you check if the user is logged in or not :
require "facebook.php";
$facebook = new Facebook(array(
'appId' => YOUR_APP_ID,
'secret' => YOUR_APP_SECRET,
));
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
// The access token we have is not valid
$user = null;
}
}
If he is not, you generate the "Login with Facebook" URL asking for the offline_access permission :
if (!$user) {
$args['scope'] = 'offline_access';
$loginUrl = $facebook->getLoginUrl($args);
}
And then display the link in your template :
<?php if (!$user): ?>
Login with Facebook
<?php endif ?>
Then you can retrieve the offline access token and store it. To get it, call :
$facebook->getAccessToken()
Use the offline access token
To use the offline access token when the user is not logged in :
require "facebook.php";
$facebook = new Facebook(array(
'appId' => YOUR_APP_ID,
'secret' => YOUR_APP_SECRET,
));
$facebook->setAccessToken("...");
And now you can make API calls for this user :
$user_profile = $facebook->api('/me');
Hope that helps !
With PHP SDK 2.0 (I guess), I just use it like
$data = $facebook->api( '/me', 'GET', array( 'access_token' => $userdata['fb_access_token'] ) );
This should work with the newer one to as it seems to be more of a clean approach than rather setting up sessions by ourself. Can you try?
Quentin's answer is pretty nice but incomplete, I think. It works nice, but I for example getUser() isn't working in that time because userId (which is getUser() returning) is cached.
I have created a new method to clear all caches and save it persistently.
public function setPersistentAccessToken($access_token) {
$this->setAccessToken($access_token);
$this->user = $this->getUserFromAccessToken();
$this->setPersistentData('user_id', $this->user);
$this->setPersistentData('access_token', $access_token);
return $this;
}

Extended Permissions on Facebook Graph API in PHP

I am trying to write an app using the most recent Facebook PHP SDK. However, any extended permissions I request are simply not requested, it always defaults to the basic request, and does not ask for any of the requested extended permissions.
Here is how I am requesting these permissions:
<?php
require_once("facebook.php");
$app_id = "xxx";
$app_secret = "xxx";
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
if(is_null($facebook->getUser())){
$params = array(‘scope’ => ‘user_status,publish_stream,user_photos’);
header("Location:{$facebook->getLoginUrl($params)}");
exit;
}
?>
This file is included at the beginning of any page on which my app is embedded. Any ideas as to why I don't get the extended permissions I want?
Make sure you are using quotes '. It seems that you copied this example from a page or something.
if(is_null($facebook->getUser())){
$params = array('req_perms' => 'user_status,publish_stream,user_photos');
header("Location:{$facebook->getLoginUrl($params)}");
exit;
}
UPDATE: use scope instead of req_perms with the new SDK.
You should pass the 'scope' parameter in a comma separated array.