Extracting friend's list using Facebook PHP SDK - php

I am stuck with this issue since awhile now. I am trying to retrieve my friends list (who are using this app, ofcourse). The SDK version is 3.2.3. The code does pull out information from my profile, but does not pull anything from my friends profiles. Though i am using the scope permission parameter with my login. Following is my index.php file :
require_once('facebook.php');
$config = array(
'appId' => 'xxxx',
'secret' => 'xxxx'
//'allowSignedRequest' => false
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
<html>
<head></head>
<body>
if($user_id) {
try {
$user_profile = $facebook->api('/me','GET');
echo "<pre>";
print_r($user_profile); //prints my profile
echo "</pre>";
foreach ($user_profile["data"] as $friend) {
echo $friend['id'];
echo $friend['name'];
}
echo '<br><a href=' . $facebook->destroySession() . '>Logout</a>';
} catch(FacebookApiException $e) {
}
} else {
$params = array(
'scope' => 'public_profile, user_friends, email',
);
$login_url = $facebook->getLoginUrl($params);
echo 'Please login.';
}
</body>
</html>
How can i just print my friend's list? (those friends whose permission i have!)

Change the
$user_profile = $facebook->api('/me','GET');
to
$user_profile = $facebook->api('/me?fields=id,first_name,last_name,friends','GET');
for example.

Related

facebook php sdk not working

i used two codes and its not working the first one :
<?php
require_once('fb/facebook.php');
$config = array(
'appId' => 'xxxxx',
'secret' => 'xxxxx',
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
?>
<html>
<head></head>
<body>
<?php
function render_login($facebook) {
$canvas_page = 'http://fbbost.eb2a.com/';
// HERE YOU ASK THE USER TO ACCEPT YOUR APP AND SPECIFY THE PERMISSIONS NEEDED BY
$login_url = $facebook->getLoginUrl(array('scope'=>'email,user_photos,friends_photos', 'redirect_uri'=>$canvas_page));
echo 'Please login.';
}
if($user_id) {
try {
$user_profile = $facebook->api('/me','GET');
echo "Name: " . $user_profile['name'];
} catch(FacebookApiException $e) {
render_login($facebook);
echo "1";
error_log($e->getType());
error_log($e->getMessage());
}
} else {
render_login($facebook);
echo "2";
}
?>
</body>
</html>
and the second one :
<html>
<head>
<title>NNN</title>
</head>
<body>
<?php
include "fb/facebook.php";
$facebook=new Facebook(array(
'appId' => 'xxxxx',
'secret' => 'xxxxx',
'cookie' => true
));
$session=$facebook->getUser();
$me=null;
if($session){
try{
$me=$facbook->api('/me');
print_r($me);
}
catch (FacebookApiException $e){
echo $e->getMessage();
}
}
if($me){
$logoutUrl=$facbook->getLogoutUrl();
echo "<a href='$logoutUrl'>Logout</a>";
}
else{
$loginUrl=$facebook->getLoginUrl(array(
'scope' => 'publish_stream,read_friendlists'
));
echo "<a href='$loginUrl'>Login</a>";
}
?>
</body>
</html>
both codes return the same error when i login :
App Not Setup: The developers of this app have not set up this app properly for Facebook Login.
I think your app is not public. You need to set it to public to use it with any Facebook account other than the developer's own account. Go to "Status and Review" and make the app public.
Besides, check that you have properly setup the app domain.

How to get user profile info using access token in php-sdk

I want to get user profile using access token that i have already generated, and if the access token is invalid it shoud display error. anyone can help to do this ? i want php code to handle this. i am waiting....
Since you already have the access token, the simplest and clean way would be-
$user_details = "https://graph.facebook.com/me?access_token=" .$access_token;
$response = file_get_contents($user_details);
$response = json_decode($response);
print_r($response);
First check if the user is logged in
$facebook = new Facebook(array(
'appId' => 'AppIDHere',
'secret' => 'AppSecretHere',
));
// See if there is a user from a cookie
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>';
$user = null;
}
}
If your access token is not null, fetch user data
<?php if ($user): ?>
Profile Picture: <img src="https://graph.facebook.com/<?php echo $user; ?>/picture">
User Id: <?php echo $user_profile['id'];?>
Name: <?php echo $user_profile['name'];?>
First Name: <?php echo $user_profile['first_name'];?>
Last Name: <?php echo $user_profile['last_name'];?>
Link: <?php echo $user_profile['link'];?>
Gender: <?php echo $user_profile['gender'];?>
Username: <?php echo $user_profile['username'];?>
<?php else: ?>
<strong>You are not Logged In.</strong>
<?php endif ?>
The user variables can be got from Facebook Graph API
If You want to get user_email and other info then you can use this code....
$facebook = new Facebook(array(
'appId' => 'AppIDHere',
'secret' => 'AppSecretHere',
));
// See if there is a user from a cookie
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me?fields=email,name,id,gender');
} catch (FacebookApiException $e) {
echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>';
$user = null;
}
}
I used facebook graph Api
$facebook = new Facebook(array('appId' => $fbconfig['appid'],'secret' =>
$fbconfig['secret'],'cookie' => true,));
$session = $facebook->getUser();
if($session)
{
try{
$uid = $session;
$fbme = $facebook->api('/me');
$access_token = $facebook->getAccessToken();
// FQL : to fetch the email id
$fql = "SELECT email FROM user where uid='".$uid."'";
$response = $facebook->api(array('method' =>
'fql.query','query' =>$fql,));
$user_email = $response[0][email];
// Friend COunt.
$friend_res = $facebook->api('/me/friends');
$friend_count = count($friend_res[data]);
//likes
$likes = $facebook->api('/me/likes');
$fb_like = $likes[data];
// Getting more user personal data
$param = array(
'method' => 'users.getinfo',
'uids' => $uid,
'fields' => 'name,sex,pic,current_location,profile_url,email,birthday',
'callback'=> ''
);
$userInfo = $facebook->api($param);
}catch(FacebookApiException $e){
}
}

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>

Facebook Php SDK instantiate

my problem is:
I downloaded Facebook Php SDK from: https://github.com/facebook/facebook-php-sdk
I put all in a folder an created my index in this way:
myAppFolder:
src
index.php
In my index.php I tried this code:
require_once("src/facebook.php");
$facebook = new Facebook(array('appId' => 666, 'secret' => 616));
die("why not zoidberg?");
But my app doesn't die and doesn't return anything, some ideas?
Next I tried this code, but only the first echo is displayed:
<?php
echo "This is visible";
try{
require_once "src/facebook.php";
$facebook = new Facebook(array(
'appId' => '666',
'secret' => '616',
));
}catch(ErrorException $e){
echo error_reporting(E_ALL);
die(var_dump($e));
}
die("This is not visible");
My output is:
This is visible
This code works perfectly for me:
index.php
<?
require_once('src/facebook.php');
$config = array(
'appId' => 'myappId',
'secret' => 'mysepratecode',
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
?>
<html>
<head></head>
<body>
<?
if($user_id) {
// 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 "Name: " . $user_profile['name'];
} 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>
Resolved, I didn't have Json and Curl installed ;)

Create Auto-POST by Facebook-SDK

I want to enable Facebook Auto Post in my website.
first I created an app, then follow the facebook docs I used the facebook-sdk for PHP, I inserted APP-ID and APP-SECRET, create LOGIN-URL and... to my script (everything like the facebook docs), but I still have problem!!
The problem is:
for first time when user visit my page, he see the login link. when he click on it he will redirect to facebook dialog page for allowing app activities. after this, when facebook redirect user to my canvas page, he see the login link again!! (It seems the getUser() function not worked correctly in my script!).
base on facebook guides the user must see the user profile details... but still the login link is visible.
how can I fix this problem...?
<?php
require_once("libs/facebook.php");
$config = array(
'appId' => 'XXXX',
'secret' => 'XXXX'
);
$fbConnect = new Facebook($config);
$user_id = $fbConnect->getUser();
if($user_id)
{
try {
$userProfile = $fbConnect->api('/me', 'GET');
echo "Name: " . $userProfile['name'];
} catch (FacebookApiException $e) {
$loginUrl = $fbConnect->getLoginUrl();
echo "<a href='" . $loginUrl . "'>LOGIN 2</a>";
}
}
else
{
$loginUrl = $fbConnect->getLoginUrl(array( 'scope' => 'publish_stream' ));
echo "<a href='" . $loginUrl . "'>LOGIN 1</a>";
}
?>
User always see "LOGIN 1"! it means the $user_id is always null (before and after app allowing activities)!! after app allowing (when user for first time click on loginUrl link) I have 'stat' and 'code' in my url query string! but still "LOGIN 1" is visible!
I guess you want to publish a message like "Hey guys, I am now using Aref's superawsome Facebook app now", right?
This can be accomplished with the following lines :
<?php
require_once("/FBAPI/src/facebook.php");
$config = array();
$config['appId'] = 'your_app_id';
$config['secret'] = 'your_app_secret';
$facebook = new Facebook($config);
$user = $facebook->getUser();
if(!$user){
$loginUrl = $facebook->getLoginUrl(array('scope'=>'publish_stream', 'redirect_uri'=>'http://www.example.com'));
}
if($user){
try{
$user_profile = $facebook->api('/me');
$access_token = $facebook->getAccessToken();
$vars = array(
'caption' => 'Aref\'s Facebook Application',
'message' => 'Hey guys, I am now using Aref\'s superawsome Facebook App :D',
'name' => 'Test',
'link' => 'http://www.example.com',
'description' => 'Aref\'s Facebook Canvas App',
'picture' => 'http://fbrell.com/f8.jpg'
);
$result = $facebook->api('/me/feed', 'post', $vars);
if($result){
echo "Post was set";
}
else{
echo "Error!";
}
}
catch(FacebookApiException $e){
error_log($e);
$user = NULL;
}
}
else{
echo '<img src="img/login.png"/>';
}
?>

Categories