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

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.

Related

Trying to post to facebook page with app but get "The user hasn't authorized the application to perform this action"

I am trying to write a script that will post an update to a facebook page timeline. I created an app and created a key for the app. I am using the code below:
require_once HOME_PATH . '/include/facebook/facebook.php';
$facebook = new Facebook(array(
'appId' => FACEBOOK_APP_ID,
'secret' => FACEBOOK_APP_SECRET,
'cookie' => true,
'scope' => 'publish_stream',
));
$user_id = $facebook->getUser();
$access_token = $facebook->getAccessToken();
$attachment = array(
'access_token' => $access_token,
'message' => 'this is my message',
'name' => 'name',
'link' => ROOT_URL . $blog_data['url'] . '.htm',
'caption' => $blog_data['title'],
'description' => $blog_data['title'],
);
if ($image = get_first_image($blog_data['body'])) {
$attachment['picture'] = $image;
}
$facebook->api(FACEBOOK_BLOG_PAGE . '/feed', 'post', $attachment);
i think this key is connected to the app and not to my user, so it sounds like i would have to grant the permission within facebook, but i have seen some other posts telling me that i need to do this in code. The examples aren't very clear.
I know similar questions have been asked but i haven't seen any clear answers yet. Can anyone please clarify this?
If you want to get user_id by this piece of code: $user_id = $facebook->getUser();
The first thing you need, is to login an user through a URL returned by $facebook->getLoginUrl(array('scope' => array('perm', 'perm2'))).
Also, notice, where I put permissions, which I require. Not into the Facebook class constructor, but into the method getLoginUrl.
And lastly, for posting into a page, you need publish_actions permission (not publish_stream).

facebook graph api getUser returns 0

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.

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'];

Facebook PHP SDK query returns an empty array

my problem is when I try to get the pic_square and pic_big from the user connected to my application and I get an empty array. This is the code:
$facebook = new Facebook(array(
'appId' => FBAPPID,
'secret' => FBSECRETNUMBER,
'cookie' => true,));
$user_info = $facebook->api(array(
'method' => 'fql.query',
'query' => "SELECT pic_square, pic_big FROM user WHERE uid = me()",
'callback' => ''
));
During the login process I run the same code and it works, but later when I try to implement a function to synch the user picture with FB pic I get no error and no data.
Any idea what is the problem?
Thanks a lot.
My guess is that later the facebook session doesn't exist so you don't have an access token to call the FQL query.
Anyway, why are you using FQL to get those pictures? You can always get them like this:
http://graph.facebook.com/me/picture?type=large or
http://graph.facebook.com/me/picture?type=square
Or you can use the user id to get permanent links to the images:
http://graph.facebook.com/USER_ID_HERE/picture?type=square
Good luck

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.