I got a Facebook php sdk Retrieve friendlist problem. Here is my basic code...
<?php
require_once 'fb-sdk/src/facebook.php';
// Create our Application instance.
$facebook = new Facebook(array(
'appId' => 'xxxxxxx',
'secret' => 'xxxxxxxxxxxxxxxx',
'cookie' => true,
));
$accessToken = $facebook->getAccessToken();
$session = $facebook->getSession();
$uid = $facebook->getUser();
//echo "https://graph.facebook.com/".$uid."/friends/?access_token=".$accessToken;
$frnd = $facebook ->api('/me/friends?access_token='.$accessToken);
echo $frnd["data"][0]["name"];
?>
But it returns a peculiar output.

Where is the problem?
You dont have to add the access_token, when you query for the friends. The Facebook-Api takes care of that. This is my code, which works for me:
$facebook = new Facebook(array(
'appId' => 'xxxxxxxx',
'secret' => 'xxxxxxx',
'cookie' => true,
));
// $session is only != null, when you have the session-cookie, that is set by facebook, after the user logs in
$session = $facebook->getSession();
// you dont get a list of friends, but a list, which contains other friendlists
$friendsLists = $facebook->api('/me/friends');
// Save all Friends and FriendConnections
foreach ($friendsLists as $friends) {
foreach ($friends as $friend) {
// do something with the friend, but you only have id and name
$id = $friend['id'];
$name = $friend['name'];
}
}
 is BOM header:
See: http://en.wikipedia.org/wiki/Byte_order_mark
(you should encode your file as uff-8 without bom)
This means your code isn't outputting anything.
$friends = $facebook->api('me/friends');
//print_r($friends['data']);
print_r("Number of friends: ". count($friends['data']));
foreach ($friends['data'] as $key=>$friendList) {
echo "<br/>".$key." ".$friendList['name']."<img src='https://graph.facebook.com/".$friendList['id']."/picture' width='50' height='50' title='".$friendList['name']."' />";
}
//get user basic description using graph api
$friends = $facebook->api('me?fields=friends');
print_r("Number of friends: ". count($friends['friends']['data']));
foreach ($friends['friends']['data'] as $key=>$friendList) {
echo "<br/>".$key." ".$friendList['name']."<img src='https://graph.facebook.com/".$friendList['id']."/picture' width='50' height='50' title='".$friendList['name']."' />";
}
?>
Related
Im a totally facebook newbie developer.This is the sdk version that i use: click . This is the login part:
<?php
require '../src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'XXXXXXXXXXXXXXXX',
'secret' => 'XXXXXXXXXXXXXXXXXx',
'cookie' => true
));
?>
<?php
$loginUrl = $facebook->getLoginUrl(array (
'scope' => 'email',
'redirect_uri' => 'http://facebook.ebis-servicii.ro/mytest/test.php'
));
$user = $facebook->getUser();
echo 'Login Here ';
?>
And this is the part where I want to get some informations about the user:
<?php
require '../src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'XXXXXXXXXXXXX',
'secret' => 'XXXXXXXXXXXXX',
'cookie' => true
));
$user = $facebook->getUser();
if ($user) {
try{
$user_profile = $facebook->api('/me');
var_dump($user_profile);
$fbid = $user_profile['id']; // To Get Facebook ID
$fbuname = $user_profile['username']; // To Get Facebook Username
$fbfullname = $user_profile['name']; // To Get Facebook full name
$femail = $user_profile['email']; // To Get Facebook email ID
}catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
echo "FBID ".$fbid."<br>";
echo "Email ".$femail."<br>";
echo "Name ".$fbfullname."<br>";
echo "Username ".$fbuname."<br>";
echo "Facebook Picture:<br>";
//echo '<img src="https://graph.facebook.com/'.$_SESSION["USERNAME"].'>;
echo "<img src ='https://graph.facebook.com/".$fbuname."'>";
}
?>
This is the url link where you see the results. So my question is how do I get the username? In the result of the var dump there is no username .
Since v2.0, the username is not included in the result anymore, as you can see in the Facebook docs: https://developers.facebook.com/docs/graph-api/reference/v2.1/user
Else, the App Scoped IDs would be pointless, and they came with v2.0 too. Apps should not be able to get to the "real" profile of the users.
You should be able to use the App Scoped ID to show the user picture:
echo '<img src="https://graph.facebook.com/'.$fbid.'/picture?width=121&height=100" />';
Btw, i would suggest using the new PHP SDK (4.x) for new Apps.
I want to post on some content on my facebook's friends but my below code is not working.
I have set the permissions of publish_action publish_stream,status_update in my developer APP.
This is the below code:-
require_once 'src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'XXXXXXXXXXXXXX',
'secret' => 'XXXXXXXXXXX',
));
$user = $facebook->getUser();
$_var = $facebook->getAccessToken();
if ($user)
{
try
{
$user_profile = $facebook->api('/me');
$friends = $facebook->api('/me/friends');
foreach($friends['data'] as $friend) {
$facebook->api('/'.$friend['id'].'/feed', 'post', array(
'message' => 'just a test message',
));
}
}
catch(Exception $e){
echo "errorr";
}
}
Plz provide me some solutions
I'm trying to figure out the basics of the facebook php sdk. I'm using the pretty simple code below...
<?php
include 'facebook.php';
$app_id = "my_id";
$app_secret = "my_secret";
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
));
$userId = $facebook->getUser();
echo $userId;
$userInfo = $facebook->api("/$userId");
echo $userInfo['name'];
?>
When I ask for the $userId with
$userId = $facebook->getUser();
echo $userId;
I get the correct value. However when I try to take the next step and retrieve the logged in user's name with :
$userInfo = $facebook->api("/$userId");
echo $userInfo['name'];
nothing is returned. Does anyone see my error? Thanks!
'/$userId' will actually use the literal string /$userId. You probably want "/$userId" or '/me'
How can i check if the current facebook user like a facebook page?
I have this code,
require_once("/path/to/sdk/facebook.php");
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => 'APP_ID',
'secret' => 'APP_SECRET',
));
$user = $facebook->getUser();
if ($user) {
try {
$likes = $facebook->api("/me/likes/153649677989200");
if( !empty($likes['data']) )
echo "I like!";
else
echo "not a fan!";
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'user_likes'
));
}
But is not responding if the user like the page, please see the page here
Thanks!
You can use FQL
$isFan = $facebook->api(array(
"method" => "fql.query",
"query" => "SELECT uid FROM page_fan WHERE page_id = '<YOUR PAGE ID>' AND uid = '<UID>"
));
I don't know if you need to do this only using FQL, but there is another easier way:
require_once("/path/to/sdk/facebook.php");
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => 'APP_ID',
'secret' => 'APP_SECRET',
));
$signed_request = $facebook->getSignedRequest();
$like = $signed_request['page']['liked'];
$like will be empty or 1.
I like to check variables first, like this:
if(is_array($signed_request) && isset($signed_request['page']) {
if($signed_request['page']['liked']) {
// do whatever you want to do if the user likes the page
} else {
// do whatever you want to do if the user don't like the page yet
}
}
I am trying to retrieve user interests through the Facebook Graph API, using PHP SDK.
I have all the correct permissions, but my API call doesn't return anything. Here is my code:
$facebook = new Facebook(array(
'appId' => APP_ID,
'secret' => APP_SECRET,
));
$user_interests = $facebook->api('/'.$user_facebook_id.'/interests');
I get nothing. What am I missing?
edit: Actually, it returns this array:
$user_interests = { ["data"]=> array(0) { } }
Its works for me, (if user doesn't have any interests means the array will return nothing)
$facebook = new Facebook(array('appId' => 'APP_ID','secret' => 'SECRET_ID', ));
$user_fb = $facebook->getUser();
if ($user_fb)
{
$user_interest = $facebook->api('/me/interests');
for($i =0; $i < sizeof($user_interest[data]); $i++)
{
echo "Interest Name : ".$user_interest[data][$i]['name']."<br>";
echo "Interest Category : ".$user_interest[data][$i]['category']."<br>";
echo "Interest Id : ".$user_interest[data][$i]['id']."<br>";
echo "Interest Created Time : ".$user_interest[data][$i]['created_time']."<br>";
}
}
You just need "user_interests" permission and it will work fine