Check If User Liked Fan Page - facebook

I have used the following code, and still use in older apps that I have created in Facebook, to hide content from users until they like the Fan page the app resideds on in a tab.
$request = $_REQUEST["signed_request"];
list($encoded_sig, $load) = explode('.', $request, 2);
$fbData = json_decode(base64_decode(strtr($load, '-_', '+/')), true);
if (!empty($fbData["page"]["liked"]))
{
//Show Content
}else{
//Hide Content
}
However when i come to use this code now, It does not work. Doe's anyone know why this still works in my older apps, but will not work when I create newer apps?
Thanks

The code does still work. It was an issue with the settings of the app, specifically the URL structure I had used.

Related

Disable installation of my Facebook App

Is there a way to prevent users from installing my facebook application to their pages, using the link below?
https://www.facebook.com/add.php?api_key=API_KEY&pages
Note: I do not want to disable my application, I just want to disable new installations.
The quickest way to achieve this is to parse the signed_request and compare the page information to a list of page ids you have authorised to use the app.
A very simple implementation would be along the lines of
<?php
$app_secret = 'APPSECRET';
$signed_request = parse_signed_request($_REQUEST['signed_request'], $app_secret);
$page_whitelist = array(PAGEID1, PAGEID2);
if (in_array($signed_request['page']['id'], $page_whitelist)) {
// do stuff
} else {
// output some error message
}
https://gist.github.com/4157347

Why I cannot get facebook like my page?

What I want to do is user must like my page first then only can go to my facebook apps, but I have no idea why I cannot detect it and I had var_dump my page liked, result is NULL
here is my code
$request = $_REQUEST["signed_request"];
list($encoded_sig, $load) = explode('.', $request, 2);
$fbData = json_decode(base64_decode(strtr($load, '-_', '+/')), true);
var_dump($fbData["page"]["liked"]);// output is NULL
if (!empty($fbData["page"]["liked"]))
{
echo "This content is for Fans only!";
} else {
echo "Please click on the Like button to view this tab!";
}
Any idea how to do it?
Have you made sure there is as signed_request parameter?
It only gets transmitted to your app once the page is initially loaded into the Facebook iframe – it won’t be there anymore once you start navigating inside your app.
Best thing to do IMHO is to parse the signed_request if not empty, and safe the results into your own session.
Btw., parsing it yourself is rather time-consuming – I’d suggest using the PHP SDK and use Facebook::getSignedRequest() – that method gives you all the data in decoded form, if there was a signd_request parameter.
So here’s what I usually do:
if($signed_request_data = $Facebook->getSignedRequest()) {
$_SESSION['signed_request_data'] = $signed_request_data;
}
You can call that code on every page – only if the method actually returns data, your session gets updated.
You need the permission user_likes to check if they liked your Facebook App but $fbData["page"]["liked"] is referring to your Facebook App Page which is seperate.
To edit your App Page go to App Settings > Advanced > Near the bottom you will see App Page
To use the permission user_likes to check see this SO Answer On Detecting if a User Likes a Facebook App

How to prompt user for access to user id in a simple iframe tab?

I know there are many Q&A on here about this but I can't seem to make sense of them because they don't show full code examples or are for doing something different than what I'm trying to do.
I have a simple facebook welcome-test tab. Not a canvas app. I want to be able to get the users id but have to make it prompt them for access to that info before it will show up in the signed request.
So what I'm looking for is a code example for a simple welcome tab. I don't need a login page or anything like that since this will be in the fan content page so they will have to be logged in already. I just want that dialog box to popup asking them for permission so I can get the userid.
Thanks
Edit:
I wanted to add that I just figured out my problem but it's not really an answer to this question. I figured out how to get the user id without asking the user for permission to access their profile. It was as easy as adding this code to the page and it works in a tab and canvas app the same way. getUser() by its self returns 0 but since the user is already logged in to be seeing this content, when getUser() returns 0, then the code simply gets the user id from the API method. Now that I finally figured this out it seems simple.
$session = $facebook->getUser();
if (!$session) {
$url = $facebook->getLoginUrl(array(
'canvas' => 1,
'fbconnect' => 0
));
echo "<script type='text/javascript'>top.location.href = '$url';</script>";
} else {
try {
$uid = $facebook->getUser();
$me = $facebook->api('/me');
$updated = date("l, F j, Y", strtotime($me['updated_time']));
echo "Hello " . $me['name'] . "<br />";
echo "You last updated your profile on " . $updated;
} catch (FacebookApiException $e) {
echo "Error:" . print_r($e, true);
}
}
I'd still like to know if there is a way to make the extended permissions dialog show for a tab app so it will be good to see solutions for php sdk.
you need to prompt them for allowing your application to pull data on the user
this can be done using the JS SDK's function FB.login(). you can have more details in the facebook documentation : http://developers.facebook.com/docs/reference/javascript/FB.login/
notice that the popup can be opened only on mouseclick or keyup events - otherwise popup blockers will block it.
you will also have to set a domain and site url to the application in the developers site, and init the facebook js library (the loading paragraph in - http://developers.facebook.com/docs/reference/javascript/)
<div onclick="login();">click here to login</div>
<script type="text/javascript">
function afterLogin(){
alert("user logged");
}
function login(){
FB.getLoginStatus(function(response){
if(response.status=="connected")
{
afterLogin();
} else {
FB.login(function(response){
if(response.status=="connected")
{
afterLogin();
}
});
}
});
}
</script>
I don't think facebook allows this... Facebook does not want a user's tab to be able to detect who is looking at it, and possibly displaying information to a user's friends that isn't displayed to the user.
I can be wrong :)

How Do I Create a Facebook App That I Can Add to a Page and Only Likers Will See

I have a Facebook page that I created and I want to create a contest app that only likers can see. I was able to do this in the past using FBML but ever since Facebook has stopped using FBML, I can't seem to do this with IFrame apps. Can anyone help me out?
Thanks!
Well, we can give you guidance. You can either develop the whole thing yourself or use a pre-built product like the one provided by Strutta (not free!).
Now how to develop a contest system is up to you, but how to make it available only to fans is easy. You just need to read the signed_request, look for the page parameter and check if the liked field is set or not. This is how to do it in PHP:
<?php
$signed_request = $_REQUEST["signed_request"];
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);
if (empty($data["page"]["liked"])) {
echo "You are not a fan!";
} else {
echo "Welcome back fan!";
}
?>
You may need to take a look at the Promotions Guidelines.

Facebook - hide content on website till "Like" button clicked

I'm using the wordpress plugin from http://www.sociable.es/facebook-wordpress-plugin-3-0/ on my blog, and I try to figure out, how they hide their download till I click the "Like" button on the post.
I tried:
<fb:fbml version="1.1">
<fb:visible-to-connection>HIDDEN CONTENT</fb:visible-to-connection>
</fb:fbml>
But it didn't work.
Is there a way with the Javascript SDK or any other solution?
Thanks a lot!
I think FBML has been done away with. Found this bit of code:
$request = $_REQUEST["signed_request"];
list($encoded_sig, $load) = explode('.', $request, 2);
$fbData = json_decode(base64_decode(strtr($load, '-_', '+/')), true);
if (!empty($fbData["page"]["liked"]))
{ ?>
You are a fan
<?php } else { ?>
You are not a fan
<?php }
//print_r($_REQUEST);
?>
However, the request variables are not being picked up by my iFrame.
Will update as i figure it out.
NB - would only works for the first time the user likes the page. you'd have to then save the session variable or something.
Here's a ready solution for Wordpress:
Like 2 Unlock for Wordpress