Like a Page in Facebook TAB and Give the Link - php

I am creating a Facebook Tab Application. People create their profile and each profile has a like button. The person who gets the most likes wins.
The problem we are facing now is how to give the URL to a user, so that the user can share the url (so that they can get more likes). Suppose I give the url fb.com/appname/id=12? Should I be able to get the details of user with id 12 in my Facebook Tab Application?
with the graph API?

As a solution for this you can use app_data parameter. You can pass additional parameters to a FB tab using app_data parameter in your url like this
https://www.facebook.com/pages/FB-App-Test13191195702111?sk=app_234567890&app_data=MY_CUSTOM_DATA
<?php
$data = array();
$signed_request = '';
$app_data = '';
if(isset($_REQUEST['signed_request'])) {
$signed_request = $_REQUEST['signed_request'];
$secret = YOUR_APP_SECRET
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
$sig = base64_decode(strtr($encoded_sig, '-_', '+/'));
$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);
if(isset($data['app_data'])) {
$app_data = $data['app_data'];
}
}
Use above php code to read your custom data in url to $app_data
$app_data will be equals to "MY_CUSTOM_DATA".

Related

Get fb user's gender via php

I have got an frame app on Facebook.
Now i need to get user's gender with php.
I have got the following code to get the user's id, how should i modify it to get gender?
$signed_request = $_REQUEST["signed_request"];
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), TRUE);
echo $data["user_id"];
$facebook->api('/me/friends', array('fields' => 'id,name,gender'));
This gives you the id,name, and gender of your friends

Store the right signed request after page like

I have a little Facebook app, which has a "like gate" at the beginning. The user have to like the page to use the app. The app itself has some subpages, so I have to store the signed request into $_SESSION variable if I want the subpages to work. My problem is that the signed request is only sent on app load and at this time the user hasn't liked the page yet. So if this time the signed request is saved, the $signed_request["page"]["liked"] will alway return FALSE... How can I reload the signed request?
FQL and $like_data = $facebook->api('/me/likes/PAGE-ID/'); isn't good, because I want the permission dialog AFTER the page like.
This is the code right now:
$page_id = $signed_request["page"]["id"];
$like_status = $signed_request["page"]["liked"];
if(!$like_status){
header("Location: notfan.php");
exit;
}else{
if (!isset($_SESSION["SR"]))
{
$_SESSION["SR"] = $_REQUEST["signed_request"];
}else{
$encoded_sig = null;
$payload = null;
list($encoded_sig, $payload) = explode('.', $_SESSION["SR"], 2);
$sig = base64_decode(strtr($encoded_sig, '-_', '+/'));
$signed_request = $data = json_decode(base64_decode(strtr($payload, '-_', '+/'), true));
$signed_request = objectToArray($signed_request);
}
}
Thank you very much!
just save the signed request every time you encounter it in the request: When the user likes the page, Facebook will do a page reload and resends the signed request
edit:
You're setting $like_status and checking it before you even know if you have a signed request and before you set the value of $like_status.
I would check the request parameter first, the session second, then fill the $like_status variable, then check the $like_status.
Something like this:
if( isset($_POST['signed_request']) )
{
$_SESSION["SR"] = $_REQUEST["signed_request"];
}
if (!isset($_SESSION["SR"]))
{
//shouldn't even happen now when page is opened through facebook,
//so you might want some error handling here
}
$encoded_sig = null;
$payload = null;
list($encoded_sig, $payload) = explode('.', $_SESSION["SR"], 2);
$sig = base64_decode(strtr($encoded_sig, '-_', '+/'));
$signed_request = $data = json_decode(base64_decode(strtr($payload, '-_', '+/'), true));
$signed_request = objectToArray($signed_request);
$page_id = $signed_request["page"]["id"];
$like_status = $signed_request["page"]["liked"];
if(!$like_status){
header("Location: notfan.php");
exit;
}

facebook php signed request and oauth redirection for mobile web

I have an issue with redirection uris, signed requests and mobile web Facebook apps and I just cant pin point where I am screwing this up.
I have a canvas app at example.com running an app that uses signed request to grab its users data. I decided to launch a mobile version so that my mobile users could use the app so I created a new directory example.com/m and made a mobile version.
At the moment the normal app is working perfectly, but the mobile app is stuck in an infinite loop once the user allows the oauth privileges. I have tried experimenting with different URLS but I haven't been able to figure it out. For the mobile the signed request is empty and I cant figure out why.
In my Facebook settings I have this set as the canvas url:
example.com
this set as the mobile web url
example.com/m
In my normal browser app I am using the following code to get the oath from Facebook:
$redir = "https://apps.facebook.com/example";
if(isset($_REQUEST["signed_request"]))
{
$signed_request = $_REQUEST["signed_request"];
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);
}
if (empty($data["user_id"]))
{
$fb -> go("https://www.facebook.com/dialog/oauth?client_id={$app["appid"]}&redirect_uri=$redir&scope={$app["permissions"]}&response_type=token");
exit;
} else
{
$fb -> access_token = "access_token=" . $data["oauth_token"];
}
This code is working fine, in my mobile version i have this code (I have tried a lot of different urls here and there but still cant get around the issue)
$redir = "https://apps.facebook.com/example/m";
if(isset($_REQUEST["signed_request"]))
{
$signed_request = $_REQUEST["signed_request"];
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);
}
if (empty($data["user_id"]))
{
$fb -> go("https://www.facebook.com/dialog/oauth?client_id={$app["appid"]}&redirect_uri=$redir&scope={$app["permissions"]}&response_type=token");
exit;
} else
{
$fb -> access_token = "access_token=" . $data["oauth_token"];
}

access user data inside the iframe application facebook

How can i get the user details from the iframe application ?
the problem is when i try to authenticate the user it authenticating and redirecting to the SITE not to facebook .
actually i am using signed_request to check user liked or not ,if liked i need to get the user details using the graph api [any other ways /javascript ?] so that i can save that on to database.
This is my current code
if (isset($_REQUEST['signed_request']))
{
$encoded_sig = null;
$payload = null;
list($encoded_sig, $payload) = explode('.', $_REQUEST['signed_request'], 2);
$sig = base64_decode(strtr($encoded_sig, '-_', '+/'));
$data = json_decode(base64_decode(strtr($payload, '-_', '+/'), true));
if($data->page->liked)
{
//liked
$questions = $this->functions->get_questions();
if($questions["status"] ==TRUE)
{
$questions["data"] = $questions["data"]->result_array();
$this->load->view("public/contest",$questions);
}
else
{
echo "FALSE";
}
}
else {
//not liked
$this->load->view("public/continue");
}
}
Thank you.
I would suggest using Facebook SDK. It allows you to get user id, and then query Graph API for some basic information. You can get more information about SDK and how to work with it here

Static FBML and FBML User ID and User Name in IFrame

I wanted to know , will this tag of Static FBML would work in IFrame anyway :-
fb : userlink uid="loggedinuser"
And my Second question is , Can We get User ID Through Cokkies stored rather using FBML as on iframe facebook wont allow us to access Users DATA.
So Using Firebug I found we Get the Users Id. and is stored in cookie.
I want the user to be Restricted for More than one time Access to my Iframe . So by getting The User ID
I would like to check , whether the user had registered or not. and if he had Registered. He cant Register Twice .
I am trying it by Using Cokkies , But due to Lack of knowledge about JAVASCRIPT , I am not getting How to execute it.
Put the following code in your iframe and you will get the logged in user id then check constraint on it .
<?php
function parse_signed_request($signed_request , $secret ) {
$signed_request = $signed_request ? $signed_request : $_REQUEST['signed_request'];
$secret = $secret ? $secret : your_app_secret;
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
// decode the data
$sig = base64_url_decode($encoded_sig);
$data = json_decode(base64_url_decode($payload), true);
if (strtoupper($data['algorithm']) !== 'HMAC-SHA256') {
error_log('Unknown algorithm. Expected HMAC-SHA256');
return null;
}
// check sig
$expected_sig = hash_hmac('sha256', $payload, $secret, $raw = true);
if ($sig !== $expected_sig) {
error_log('Bad Signed JSON signature!');
return null;
}
return $data;
}
function base64_url_decode($input) {
return base64_decode(strtr($input, '-_', '+/'));
}
$request=$_REQUEST['signed_request'];
$appsecret = 'your_app_secret_key';
$new = parse_signed_request($request , $appsecret );
echo $new['user_id'];

Categories