Getting user info after login for facebook application - facebook

I have written a small piece to get the user to login :
<?php
include_once ('facebook.php');
$api_key = 'xxxxxxxxxxxxx';
$secret = 'xxxxxxxxxxxxx';
global $facebook;
$facebook = new Facebook($api_key, $secret);
$facebook->require_frame();
#Ask user to login if not logged in
$is_tab = isset($_POST['fb_sig_in_profile_tab']);
if( !$is_tab ){
$uid = $facebook->require_login($required_permissions = 'email,status_update,offline_access');
}
else{
$uid = $facebook->get_profile_user();
}
I need to retrieve the users info once I have the uid. Unfortunately I cannot find the api calls for it and there is a no IDE for facebook :(. Can anyone point me to the api's or give me here. I want to get users username, city, zipcode.
I coudnt get this to work : facebook.Schema.user user = api.users.getInfo();
I think it is for the new API lib.
Thanks.
P.S: I am not using graph APIs, because I found them to be poorly documented and I got stuck with them for a while so decided to abandon them atleast for now.

You can get it like this:
$user_info = $facebook->api_client->users_getInfo($uid, "name, city, zipcode");
print_r($user_info);
More Info:
http://developers.facebook.com/docs/reference/rest/users.getInfo

Related

Integration of myBB with website

I want my website and my mybb board to be connected. I Googled and made the integration, and I'm able to get a username id, as long as I specify their ID, but nothing happens when I'm using the function as explained.
What I'm trying to do is verify a connection of one to the forums, via my website, and then show their name.
Here's my code, as well as my attempts to get a username:
PHP Code:
define('IN_MYBB', NULL);
require_once 'forums/global.php';
require_once 'forums/class.MyBBIntegrator.php';
$MyBBI = new MyBBIntegrator($mybb, $db, $cache, $plugins, $lang, $config);
$user = get_user($uid);
while ($forum_user = mysqli_fetch_array($user))
{
echo $forum_user['username'];
}
You could use:
define('IN_MYBB', NULL);
require_once 'forums/global.php';
if(isset($mybb->user['uid'])){
echo "User Found: ".$mybb->user['usernmae']."($mybb->user['uid'].")";
} else {
echo "No user found";
}
MyBBIntegrator isn't even required. But the above code will check if the user is logged in, and display the username and userid. If not it will display No User Found. Hopefully this helps.

Retrieving Facebook friends list

I am not able to get the Facebook friends list using the API. It was working fine earlier but not anymore. Here is the code I have used. Can someone help to validate the code. Thanks in advance.
<?php
require_once('PHPMailerAutoload.php');
require_once('PHPMailerConfig.php');
require_once("facebook/facebook.php");
$config = array();
$config['appId'] = '***************';
$config['secret'] = '*******************************';
$facebook = new Facebook($config);
$uid = $facebook->getUser();
if(!empty($_GET['error'])) {
echo "User cancelled the authentication with facebook";
echo "<script>
window.opener.location.href='merror.php';
window.close();
</script>";
exit;
}
if($uid) {
$url = $uid.'?fields=id,work,friends,email,picture.width(450).height(450)';
$user_profile = $facebook->api($url);
$friends = $facebook->api('/me/friends');
$d = '';
foreach($friends['data']as $val) {
$d .= $val['id'].',';
}
}
?>
Please read the docs before posting a question which has been answered here hundreds of times.
See
https://developers.facebook.com/docs/graph-api/reference/v2.3/user/friends#read
A user access token with user_friends permission is required to view the current person's friends.
This will only return any friends who have used (via Facebook Login) the app making the request.
If a friend of the person declines the user_friends permission, that friend will not show up in the friend list for this person.

Fetching FB user-id returns incorrect value ("1")

I have a very strange problem:
Sometimes (but not every time!) when I try to get the current user facebook ID, I get "1";
for example:
$config = array();
$config["appId"] = API_KEY;
$config["secret"] = SECRET;
$facebook= new Facebook($config);
$user=$facbook->getUser();
echo $user['id'];//returns 1;
Obviously - this it not the real user ID.
When I check if the access token is still good -I see that it is still valid. Also, even if I force the user to login again using:
FB.login(function(response) {
if (response.authResponse) {
top.location.href="<?php echo HOME_URL;?>"
}, {scope: 'publish_stream'});
Still getting the same result.
Any idea what could be the problem?
You should ONLY get an ID back from FB:
$user_id = $facbook->getUser();
echo $user_id;
You would later have to query the API for a user profile (which is what it looks like you are doing):
$user_profile = $facebook->api('/me');
Reference:
http://developers.facebook.com/docs/reference/php/facebook-api/

Facebook API how to get User Pages

I am using the following code to grab info for the Facebook user.
Is there a way to get the user Pages (if the user has one or more Facebook Pages) and the IDs of these pages?
$user = $_POST['uid']; //the returned user ID from Facebook
$key = $_POST['usid']; //the returned session ID from Facebook
$accesstoken = $_POST['accesstoken']; //the returned accesstoken from Facebook
$image = "http://graph.facebook.com/" .$user ."/picture?type=large"; //Facebook Profile Picture
$name = json_decode(file_get_contents('https://graph.facebook.com/' . $user))->name; //Name of the user account on Facebook
$link = json_decode(file_get_contents('https://graph.facebook.com/' . $user))->link; //Link of the user profile on Facebook
$gender = json_decode(file_get_contents('https://graph.facebook.com/' . $user))->gender; //Gender of the user on Facebook
$locale = json_decode(file_get_contents('https://graph.facebook.com/' . $user))->locale; //Location of the user on Facebook
$url = "https://graph.facebook.com/". $user ."/feed?limit=1&access_token=".$accesstoken; //We get the latest status update on the Facebook Wall
$json = file_get_contents($url);
$jsonData = json_decode($json);
foreach($jsonData->data as $val) {
if($val->from->id == $user) {
$statusmessage = $val->message; //Finally we got the latest status update if there is one on The Facebook Wall
break;
}
}
If you asking how to get additional user profiles, of a single user, based on their Facebook Id: the answer is you can't. Facebook mandates one profile per user. And while in real life a user can have multiple profiles, there is no reliable means of extracting that data from the graph.
However, if you're asking how to get the ids of pages the user admins then use:
https://graph.facebook.com/USER_ID/accounts
this will require a valid access token for the given user.

Facebook infinite redirect loop during authentication

I am getting an infinite loop in the URL redirect after a user either logs in or is already logged in. The page redirects to the login page if the user is not logged in as expected , but goes in the loop as soon as he enters the credentials.
Following is the code:
<?php
include_once ('facebook.php');
$api_key = 'xxxxxxxxxxxx';
$secret = 'xxxxxxxxxxxx';
global $facebook;
$facebook = new Facebook($api_key, $secret);
$facebook->require_frame();
$uid = $facebook->require_login($required_permissions = 'email,status_update,offline_access');
$facebook->api_client->users_hasAppPermission("offline_access",$uid);
#echo $uid;
# $facebook->api_client->users_setStatus("hello",$uid);
# echo "<p>Your status has been updated</p>";
?>
Interestingly this code was working before, but suddenly started giving me an infinite loop problem. There have been couple of discussion on facebook forum about this but no indication that is a bug or what is the workaround .
Any help would be highly appreciateed.
Thanks
I added the code to direct to login only if the user is not logged in else do not do that. This worked for me! .. Hope it helps.
$is_tab = isset($_POST['fb_sig_in_profile_tab']);
if( !$is_tab ){
$uid = $facebook->require_login($required_permissions = 'email,status_update ,offline_access');
}
else{
$uid = $facebook->get_profile_user();
}