Display Facebook tab content for different user languages - php

I'm creating an app for a client that requires French content to be displayed to French speaking Facebook users. If the user is not French speaking, English content should be displayed. This needs to work in conjunction with the fan-gating.
This is what I have, however it's not working. The fan gate is, but the French content is not displaying (yes, I changed my language in my Facebook settings to French).
To summarize, if the user is French speaking, show fan and non-fan French content, else show default English content.
Here is the code:
<?php
require 'facebook.php';
$app_id = "XXX";
$app_secret = "XXX";
$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"];
$languageFR = $signed_request["user"]["fr_CA"];
if ($like_status) {
if ($languageFR) {
include("fans_french.php");
}
else {
include("fans_english.php");
}
}
else {
if ($languageFR) {
include("nonfans_french.php");
}
else {
include("nonfans_english.php");
}
}
?>

Shouldn't your line about the language be:
$languageFR = ($locale == "fr_CA");
Or even better, shouldn't you be looking at ["user"]["languages"], and not the locale?

(Posted solution on behalf of the question author to move it to the answer space).
Here is an updated and working script:
<?php
require 'facebook.php';
$app_id = "XXX";
$app_secret = "XXX";
$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"];
$languageFR = ($locale == "fr_CA");
if ($like_status) {
if ($languageFR) {
include("fans_french.php");
}
else {
include("fans_english.php");
}
}
else {
if ($languageFR) {
include("nonfans_french.php");
}
else {
include("nonfans_english.php");
}
}
?>

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 automatically on the wall of user (facebook App)

i want for my app to post on a user's wall on behalf (for advertising e.g.). I think about creating a php script, which i will execute with a cron job on my server every week.
I have the userids in my database.
Now i want that the script gets an userid and then posts on the wall of the user. (of course if the user has got still installed the app and granted the publish stream permission)
is it possible to create a script that triggers this?
$post = $facebook->api("/$user1/feed","POST",$params); or
$post = $facebook->api("/$user2/feed","POST",$params); etc...?
Thank for advise
<?php
require 'src/facebook.php';
$app_id = 'yourappid';
$app_secret = 'yourappsecret';
$app_namespace = 'appname';
$app_url = 'https://apps.facebook.com/' . $app_namespace . '/';
$scope = 'email,publish_actions';
// Init the Facebook SDK
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
));
// Get the current user
$user = $facebook->getUser();
// If the user has not installed the app, redirect them to the Auth Dialog
if (!$user) {
$loginUrl = $facebook->getLoginUrl(array(
'scope' => $scope,
'redirect_uri' => $app_url,
));
print('<script> top.location.href=\'' . $loginUrl . '\'</script>');
}
else {
try {
$params = array(
'message' => "your message",
'name' => "hello world",
'description' => "hello world",
'link' => "hello world",
'picture' => "hello world",
);
$post = $facebook->api("/$user/feed","POST",$params);
echo "";
}
catch (FacebookApiException $e) {
$result = $e->getResult();
}
}
?>
To solve your problem , I will share my code with you .
this worked for me .
You need just to to write some loop while to select all users id from your database .
<?php
//// publish as status
//// publish post to users
require_once("facebook.php");
$app_id = "xxxxxxxxxxx";
$app_secret = "xxxxxxxxxxxxxx";
$my_url = "http://t.xxxxx.net/facebook/publish.php/"; // refer number
///
$config = array();
$config['appId'] = 'xxxxxxxx';
$config['secret'] = 'xxxxxxxxxxxx';
$config['fileUpload'] = false; // optional
$facebook = new Facebook($config);
///
////
function getAccessToken() {
if ($this->accessToken !== null) {
return $this->accessToken;
}
$this->setAccessToken($this->getApplicationAccessToken());
$user_access_token = $this->getUserAccessToken();
if ($user_access_token) {
$this->setAccessToken($user_access_token);
}
return $this->accessToken;
}
function getApplicationAccessToken() {
return $this->appId.'|'.$this->appSecret;
}
/////////////////////////////// Update status Function
//xxxxxxxx
$session = $facebook->getUser();
// New SDK
$facebook->api ( array(
'method' => 'users.setStatus',
'status' => 'Hi
this new status by my app
',
'uid' => '21511',/// user_id
'session'=>$session,
) );
?>

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

facebook-php-sdk: Why $name variable does not return nothing?

require 'facebook.php';
$app_id ="11111111111111111";
$app_secret = "11111111111111111111111111";
$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"];
$name = $signed_request["user"]["name"];
According to the facebook docs, the user property is
A JSON object containing the locale string, country string and the age object (containing the min and max number range of the age) for the current user.
Note the lack of any mention of a name in there.

A "1" output with Facebook php-sdk

I can't figure it out and it's hard to Google:
My iframe produces a "1" just before the
but I can't figure out why and where it comes from.
http://www.quepasa.in-town.nl/facebook/
My code:
require 'fb-sdk/src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'xxxxxxx',
'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx',
));
$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 ($like_status) {
echo include("page-revealed.php");} else {echo include("page-signup.php");}
The revealed and signup have not more then this and temporary deleting everything didn't help.
Does anyone know where this is coming from?
This is because you are doing an "echo" on an include. Remove the echo and just have it as
if ($like_status) {
include("page-revealed.php");} else { include("page-signup.php");}
And you should be good.
you don't need to echo include, just include basically
Just do:
include("http://www.mysite.net/script.php");
Or:
echo file_get_contents("http://www.mysite.net/script.php");

Categories