Returning user data using facebook php sdk - php

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'

Related

Facebook Fangate

I am trying to make a simple fangate Facebook app, but i get stuck.
The page simply doesnt show - its blank.
I found a code on internet and implemented it (also downloaded facebook sdk from github)
The Facebook Page URL (http) and Secure (https) are correct.
If I link index.php the page is blank, if I link the prelike.php or reveal.php the content is shown.
Does anyone know where might be the problem?
link to the facebook app :
https://www.facebook.com/pages/Testna-stran/136388943218474?id=136388943218474&sk=app_744205652262463
<?php
require 'facebook.php';
$app_id = "xxxxxxxxxxxxxx";
$app_secret = "xxxxxxxxxxxxxxxxxxx";
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
$signed_request = $facebook->getSignedRequest();
$page_id = $signed_request["page"]["id"];
$page_admin = $signed_request["page"]["admin"];
$like_status = $signed_request["page"]["liked"];
$country = $signed_request["user"]["country"];
$locale = $signed_request["user"]["locale"];
// The following statement does a test of $like_status and chooses which page to display to the visitor
if ($like_status) {$a = file_get_contents("reveal.php");
echo ($a);
}
else {$a = file_get_contents("prelike.php");
echo ($a);
}
?>

How to get fan-gate implemented for php Facebook SDK

I am trying to create a facebook fangate tab.
i am using this code
<?php
// create the Facebook Graph SDK object
require_once('facebook.php');
$facebook = new Facebook(array(
'appId'=>'xxx', // replace with your value
'secret'=>'xxxxx' // replace with your value
));
$signedRequest = $facebook->getSignedRequest();
// Inspect the signed request
if($signedRequest['page']['liked'] == 1){
echo 'pageid ' + $signed_request["page"]["id"];
include('like.php');
} else {
echo 'pageid ' + $signed_request["page"]["id"];
include('noLike.php');
}
?>
But it dosnt seem to work, the page id displays as zero. it always displays the nolike.php file.
I have also tried this code
$app_id = "xxx";
$app_secret = "xxxx";
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
$signed_request = $facebook->getSignedRequest();
$like_status = $signed_request["page"]["liked"];
?>
<?php
if($like_status){
include('like.php');
}else{
include('noLike.php');
}
?>
This also only shows the noLike.php file
here is a like to the page
http://www.facebook.com/pages/BigDawg-Labs/166258126793251?sk=app_280260608708912
Do you have any ideas why i cannot $signed_request["page"]["liked"] always evaluates to false

Reveal tab index.php code that used to work doesn't now. How do I fix?

I've used this code for dozens of reveal tabs and now they've stopped working. I've found a fix for the https pages, but that fix doesn't work on http pages. Please see the code below and any specific help would be great.
<?php
require 'facebook.php';
$app_id = "APP_ID";
$app_secret = "APP_SECRET";
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
$signed_request = $facebook->getSignedRequest();
$page_id = $signed_request["page"]["id"];
$page_admin = $signed_request["page"]["admin"];
$like_status = $signed_request["page"]["liked"];
$country = $signed_request["user"]["country"];
$locale = $signed_request["user"]["locale"];
// If a fan is on your page
if ($like_status) {
$a = file_get_contents("yourlikepage.html");
echo ($a);
} else {
// If a non-fan is on your page
$a = file_get_contents("yournolikepage.html");
echo ($a);
}
?>
I am by no means a coder. Please be specific in your response with actual code fixes as otherwise it's likely your answer, as awesome as it'll be, will soar over my limited brain.
Thanks!
As one limited brain to another, I just wanted to double check that you are actually entering in your App ID and App Secret. I hope this doesn't seem like a stupid question - you'd be surprised!

passing custom parameters to facebook fan page tab

I'm unable to get custom parameters send to my facebook fan page tab.
I'm using php and is passing like this:
http://www.facebook.com/pages/{page-name}/?sk=APP_ID&pass=1
but I'm unable to read the parameter pass
Sreejith
Facebook passes in your data as part of the signed_Request data.
Here is how you would retrieve it using PHP:
<?php
require 'facebook.php';
$app_id = "YOUR APP ID HERE";
$app_secret = "YOUR SECRET KEY HERE";
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
// THE MAGIC SAUCE
$signed_request = $facebook->getSignedRequest();
$page_id = $signed_request["page"]["id"];
$like_status = $signed_request["page"]["liked"];
// HERE IS A STRING OF YOUR APP DATA.
$app_data = $signed_request["app_data"];
echo '$app_data = '.$app_data;
?>
This example requires the Facebook PHP api and will write your app_data into the browser window where you can marvel in all its glory.
Oh wow, I didn't know about the app_data parameter.Here's another method for whatever it's worth:
http://iamskwerl.com/tech/2011/11/passing-query-variables-to-facebook-fan-page-tabs/
There is a solution available although I have not tried it but hope it works
http://ikorolchuk.blogspot.com/2011/03/facebook-fan-page-pass-parameter-to.html

Facebook php sdk Retrieve friendlist issue

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']."' />";
}
?>

Categories