passing custom parameters to facebook fan page tab - php

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

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);
}
?>

Post on facebook page by cron

I have read all the postings related to the title. But I have a very simple question. It will be very helpful if anyone can answer that.
What is me/account page in facebook?
I got many postings here such as
Posting to facebook company page with cron php server side
Everything is explained, but I know this sounds funny, but I stuck in the easiest step.
"When using Graph Explorer you would be required to navigate to /me/accounts end point,"
Please help me.
Please check the code
include 'includes/facebook.php';
$app_id = "XXXXXXXXXX";
$app_secret = "XXXXXXXXXXXXXX";
$page_id = "XXXXXXXXXXXX";
$my_url = "http://XXXXXXXXXXX.com";
$page_access_token = "XXXXXXXXXXXXXXX";
//Create the facebook object
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
//Write to the Page wall
try {
$attachment = array(
'access_token' => $page_access_token,
'message'=> "Hello World"
);
$result = $facebook->api('/639386542780904/feed', 'post', $attachment);
} catch(Exception $e) {
echo "error";
// ...
// mail($to, $subject, $message, $headers);
}
me/accounts is used to get a facebook page acces token, so that your app will post as that page. Elsewhere, your app will post as you on that page.

Returning user data using facebook php sdk

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'

Programmatically post a facebook page status using Cron with the PHP API

So I have been tearing my hair out trying to make this work. I have been all over stack overflow looking at existing questions and found this: Facebook: Post on Page as Page issue (access token / php) but it still doesn't seem to solve my problem.
I'm trying to post to my facebook page every day using a cron job. I am having problems with the authentication. Here's my code:
//Post to Facebook
//Variables
$app_id = "...";
$app_secret = "...";
$page_id = "...";
$my_url = "http://.../";
$access_token = "taken from page access token (developers.facebook.com/tools/explorer/)";
//Create the facebook object
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
//Get the access token using Facebook Graph API
//This gives permission to post to the wall
$token_url="https://graph.facebook.com/oauth/access_token?client_id=" . $app_id
. "&client_secret=" . $app_secret
. "&code=" . $access_token
. "&redirect_uri=" . $my_url;
$response = file_get_contents($token_url);
$params = null;
parse_str($response, $params);
$user_access_token = $params['access_token'];
//Get a list of pages and loop through
//If one matches one in the variables above, that's the one
//We're posting to
$attachment_1 = array(
'access_token' => $user_access_token
);
$result = $facebook->api("/me/accounts", $attachment_1);
foreach($result["data"] as $page) {
if($page["id"] == $page_id) {
$page_access_token = $page["access_token"];
break;
}
}
//Write to the Page wall
try {
$attachment = array(
'access_token' => $page_access_token,
'message'=> "Hello World"
);
$result = $facebook->api('/me/feed','POST', $attachment);
} catch(Exception $e) {
//Send me an email
...
mail($to, $subject, $message, $headers);
}
This works if I access the script in a browser (I assume it is using my facebook session then), but not when it is triggered by the cron job.
I would really appreciate any help. Thanks.
//Get the access token using Facebook Graph API
//This gives permission to post to the wall
If you already have a page access token, then this step is wrong at this point – it’s for exchanging the code that the Auth dialog passes back to your app for a user access token.
But since you already have your page access token, you can skip that step (everything from the above comments up to //Write to the Page wall).
All you have to do, is to use your page access token in your API call, instead of the user access token you are giving there right now.

Facebook AppRequests not showing?

I'm using the Facebook php-sdk and the batch request API to send AppRequests with the following code:
$facebook = new Facebook(array(
'appId' => $FB_APP_ID,
'secret' => $FB_APP_SECRET,
'cookie' => true
));
// get app token before it's overwritten
$FB_APP_TOKEN = $facebook->getAccessToken();
$res = $facebook->api('<USER ID>/apprequests', 'POST', array(
'message' => 'Test message..'
), $FB_APP_TOKEN);
This appears to work and returns the ID of the request, which I can then see has been stored when looking back with the graph explorer. My problem is that nothing appears on my test user's facebook account, no notification and no number appears next to the app name in the sidebar as described here.
Does anyone have any idea why this might be?
getAccessToken() sometimes don't return user access token. It only returns facebook application key. Use the following code to get the access token.
$token_url = "https://graph.facebook.com/oauth/access_token?" .
"client_id=" . $app_id .
"&client_secret=" . $app_secret .
"&grant_type=client_credentials";
$app_access_token = file_get_contents($token_url);
Turns out I was looking at the App's page instead of the App itself, app requests are working fine!

Categories