During the past few hours I've been working on a "web app" for our hostesses. Its purpose is to login a facebook user and send him to next page where are like boxes with sign out button. This part is done, however I would like to make the process faster by inserting a likejacking script, so the user doesn't have to do anything but to log in and log out.
But I have one problem - how to check if the user is Fan or not (because if he already liked the page he will unlike it)? I've tried a solution which I'm using on my FB pages to fan gate, but it is not working outside facebook.
Here is the code I was trying to use:
<?php
require 'facebook.php';
$facebook = new Facebook(array(
'appId' => '0000000',
'secret' => '1654adadadada',
'cookie' => true
));
?>
<div id="fb-root"></div>
<?php
$signed_request = $facebook->getSignedRequest();
if ($signed_request['page']['liked'])
{
echo header('Location: liked.html');
}
else
{
echo header('Location: notliked.html');
}
your can get the data from FB graph.
https://graph.facebook.com/me/likes/PAGE_ID_TO_CHECK?access_token=XYZ
you will not be able to get this data without the token.
Related
I am using PHP sdk for Facebook.
require_once 'facebook-sdk/facebook.php';
session_start();
$facebook = new Facebook(array(
'appId' => 'app_id',
'secret' => 'app_secret',
'cookie' => true,
));
$facebook->getUser();
if($user){
// do stuff
}else{
//redirect to app login
}
After this piece of code there is link on my page that points to this same page with a query string.
something like Continue
But when I click this link $user is "0". This happens only şn Safari and IE. When I check cookies set by app doesnt exist in these browsers.
Any ideas?
Since the user id won't change, is it a problem if you send it via GET?
Continue
and then at the top of your script and after getting the $user the way you are doing:
if($_GET['step'] == 2){
$user = $_GET['id'];
}
If you really need to keep the cookies you can add this at the top of your script, it must be the first line.
header('P3P: CP="CAO COR CURa ADMa DEVa OUR IND ONL COM DEM PRE"');
Also you might want to check this questions
Iframe Facebook application and cookies [Internet Explorer]
Session Lost on IE Facebook App iFrame
wich are related to the header solution.
I've been working on a way to determine if a user likes a particular page so a tab on that page can be fangated or not. I didn't want to prompt the user for authorization for user_likes, so I avoided the JS SDK and used the PHP SDK:
<?php
require 'src/facebook.php';
$app_id = "...";
$app_secret = "...";
$facebook = new Facebook(array('appId' => $app_id, 'secret' => $app_secret, 'cookie' => true));
$signed_request = $facebook->getSignedRequest();
$like_status = $signed_request["page"]["liked"];
/* testing response */
if ($like_status) {
/* liked content */
} else {
/* not liked content */
}
?>
My problem is signed_request is passed only when the code is on the FB tab--if I hit the PHP page outside of FB, I get nothing. I wondered if there's a way to get this user info outside of Facebook.com.
You can try saving the signed_request in a session or cookie so you can use it until it expires. After which, the user will have to come through the tab again to renew it. Naturally, if you are planning to use cookies, you should keep the signed_request decoded so no one can find and use the access_token from the cookie.
Sounds like this isn't really feasible outside of the Facebook tab. We've begun deploying our tabs in PHP in order to pull this data without an action on the user's part.
Thanks!
In my facebook App, I am submitting the form to the same page.
After submitted, I will check the user is logged in by getUser(). If a user id can be got, POST data will be stored to database.
The below code works in Chrome, Firefox, Safari in Both Mac and PC.
However, when I first submit the form in IE 8, getUser() will return 0. Then I submit the form again, the user ID can be got.
I have not idea why the user ID cannot be got at the first submission. Can anyone help?
Thanks!
<?php
$facebook = new Facebook(array(
'appId' => FACEBOOK_APP_ID,
'secret' => FACEBOOK_SECRET_KEY,
));
// Get User ID
$user = $facebook->getUser();
?>
<form action="" method="post" id="form1" name="form1">
...
</form>
I don't see the rest of code, but if you ask is you has $user, or not you have to request autentification or login. there is something like this:
if(!$user) make the login request
else continuos with your code.
sees the sdk php code or javascript library for Facebook in the Facebook developer side.
other problem is possible that you have and error while you try to get the user, and its possible you have to do a
try{
$user = $facebook->getUser();
}catch (exception *e){
//is there is and error you have to login again to get the users data
}
I hoper this will be useful for you
I have created a Page Tab Facebook App where I want to display different content depending on the user being a fan or not. (Also called fan gate, landing page or reveal page)
For this I'm using the PHP SDK, in specific the following code:
<?php
require 'src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'APP_ID',
'secret' => 'APP_SECRET',
'cookie' => true,
));
?>
And in the content:
<?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"])) {?>
Thank you for liking our Page!
<?php } else { ?>
You haven't liked our page yet..
<?php };
// Debugging Code
echo "REQUEST:<br>";
print_r($_REQUEST);
echo "<br>GET:<br>";
print_r($_GET);
echo "<br>POST:<br>";
print_r($_POST);
?>
This works when I'm logged in with my Facebook User and when I use it on my own Page.
Meaning the signed_request is present in both $_POST and $_REQUEST.
However, when I test it with another user, there is no signed_request value in those variables..
Notes:
- I already took a look at my URL Settings in the App Configuration (300 Redirect and stuff) but this looks fine, and like I said with my User it's working..
- the signed_request is not just empty, it doesn't even exist.
Does anybody have similar issues?
I would not mind using the Javascript SDK instead of PHP if it works then, but I'm not very experienced in Javascript.
EDIT:
As i found out you always have to have a non-secure (http) and a secure (https) URL.
Even if you enter a secure URL as the standard URL, facebook will contact it using http and will then get redirected (depends on server configuration) to the https, which makes you lose your signed_request.
I just had a similar problem. In my case the problem was that in the app config i had not put a slash at the end of the tab URL which was referencing a directory (with an index.php in it). So i got a redirect to the same URL with a slash at the end and lost the $_POST this way.
signed_request is never passed via GET but POST. $_REQUEST contain data according to configuration in php.ini (request_order or variables_order)
Since you are using PHP-SDK it's better to use it for signed_request retrieval:
$signed_request = $facebook->getSignedRequest();
$liked = $signed_request['page']['liked'];
For applications running in Page Tab signed_request is always passed, so if you not get it ensure there is no redirections that omit POST data being passed.
I'm trying to replicate the same functionality as someone else has already achieved on this page here:
http://www.facebook.com/PowerPhotoUploader?sk=app_152884604799537
am not bothered about the forced like fangate part, can do that no probs.
I need to achieve this without requesting any user perms the same way they have
I've got close, but not quite right yet.
Have created a test album on this page: http://www.facebook.com/pages/Demo-Album-Upload/227979503931257?sk=app_153866511376478
The code I have is uploading my specified image, however it is ignoring the album id I input and instead, uploading to an album on my own profile.
Code so far is:
<?php
$app_id = "XXXXXXXXXXX";
$app_secret = "XXXXXXXXXXXX";
require 'facebook.php';
$facebook = new Facebook(array( 'appId' => $app_id, 'secret' => $app_secret, 'cookie' => true, 'fileUpload' => true,));
$signed_request = $facebook->getSignedRequest();
//print_r ($signed_request);
$page_id = $signed_request["page"]["id"];
//Upload To Page Album
$facebook->setFileUploadSupport(true);
$album_id ='59125';
$file_path ='image2.gif';
$args = array('message' => 'Photo Caption');
$args['image'] = '#' . realpath($file_path);
$data = $facebook->api('/'.$album_id.'/photos', 'post', $args);
print_r($data);
?>
Have already read through a lot of forum material, have set the filuploadsupport, set file upload to true, but most of the info I can find so far reuires perms & access token, however sample above has managed to achieve with neither - any thoughts?
Regards Tony
Tony the trick here is that you only need one access token, not a new one for each user. The idea is that the user is not posting to the page, you are posting to the page, so you do not need an access token from the user. However since this is a secure call to Facebook you still need to provide an access token that has the ability to publish to the page.
The simplest route to get an access token that you can use would be to manually give the application the manage_pages and offline_access permissions for your account. Then just grab the access token for for your account and use it for all calls.