Facebook Fan Gate - Displaying the 'Reveal" Content - php

I'm trying to add a fan gate to my page, and I can get the "pre-like" content to show, but after I like the page - the post-like content isn't showing up. It shows the same pre-like content, regardless of whether I like the page or not.
This is the code I'm using in my index.php file...
require 'facebook.php';
$app_id = "myappid";
$app_secret = "myappsecret";
$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"];
// If a fan is on your page
if ($like_status) {
$a = file_get_contents("dolike.html");
echo ($a);
} else {
// If a non-fan is on your page
$a = file_get_contents("dontlike.html");
echo ($a);
}
?>
I've looked at half-dozen examples, and they're all essentially the same (with a few variants, some use images over html, some use the html in the same page), but none of them show the post-like content.
I removed the app id and secret from the code, though I do have them and have been using them.
Any help'd be awesome.

<?php
require 'facebook.php';
$app_id = "myappid";
$app_secret = "myappsecret";
$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"];
// If a fan is on your page
if ($like_status == 1) {
$a = file_get_contents("dolike.html");
echo ($a);
} else {
// If a non-fan is on your page
$a = file_get_contents("dontlike.html");
echo ($a);
}
?>
Try this :)
EDIT:
Or try this version without the FB PHP-SDK, this is the solution I use for fangating so i don't need the user the whole PHP-SDK
<?php
$app_secret="xxxxxxxxxxxxx";
$data = parse_signed_request($_REQUEST['signed_request'], $app_secret);
$page_data=$data['page'];
function parse_signed_request($signed_request, $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, '-_', '+/'));
}
if($page_data['liked'] == "1"){
// Fan Content
}else{
// No-Fan Content
}
?>

Related

Error Class facebook\HttpClients\HttpClient Factory' not found in line Facebook/Facebook.php on line 148

I need some help, I am trying to implement facebook login in my website and right from the
get-go I am getting an error.
Error Class facebook\HttpClients\HttpClient Factory' not found
What does that even mean?
my code so far is pretty simple
in page one which for the example I called config.php, I write this little bit of code.
Page 1) config.php .
page 2) login.php
What am I doing wrong.. I am doing this on my own so I am pretty confused. Any help would be very much apprecitated.
I am not an expert. First time I am messing with facebook API so I apologize in advance.
require("vendor/autoload.php");
require_once $_SERVER["DOCUMENT_ROOT" ] ."/main_folder/vendor/facebook/graph-sdk/src/Facebook/autoload.php";
$facebook = new \Facebook\Facebook([
'app_id' => '****************',
'app_secret' => '*****************************',
'default_graph_version' => 'v6.0'
]);
require_once("../main_folder/config.php");
$facebook_output = '';
$facebook = $this->facebook;
$facebook_helper = $facebook->getRedirectLoginHelper();
if(isset($_GET['code']))
{
if(isset($_SESSION['access_token']))
{
$access_token = $_SESSION['access_token'];
}
else
{
$access_token = $facebook_helper->getAccessToken();
$_SESSION['access_token'] = $access_token;
$facebook->setDefaultAccessToken($_SESSION['access_token']);
}
$graph_response = $facebook->get("/me?fields=name,email", $access_token);
$facebook_user_info = $graph_response->getGraphUser();
if(!empty($facebook_user_info['id']))
{
$_SESSION['user_image'] = 'http://graph.facebook.com/'.$facebook_user_info['id']. '/picture';
}
if(!empty($facebook_user_info['name']))
{
$_SESSION['user'] = $facebook_user_info['name'];
}
if(!empty($facebook_user_info['email']))
{
$_SESSION['user_email_address'] = $facebook_user_info['email'];
}
}
else
{
$facebook_permissions = ['email'];
$facebook_login_url = $facebook_helper->getLoginUrl('https://www.stolve.com/stolve-master/login.php', $facebook_permissions);
$facebook_login_url = '<div align="center"><img src="php-login-with-facebook.gif alt="facebook logo"/></div>';
}
vendor dir must be in same dir as config.php and login.php
config.php
<?php
require_once __DIR__ . "/vendor/autoload.php";
$facebook = new Facebook\Facebook ([
'app_id' => '****************',
'app_secret' => '*****************************',
'default_graph_version' => 'v6.0'
]);
login.php
<?php
require_once "./config.php";
$facebook_output = '';
$facebook_helper = $facebook->getRedirectLoginHelper();
if(isset($_GET['code']))
{
if(isset($_SESSION['access_token']))
{
$access_token = $_SESSION['access_token'];
}
else
{
$access_token = $facebook_helper->getAccessToken();
$_SESSION['access_token'] = $access_token;
$facebook->setDefaultAccessToken($_SESSION['access_token']);
}
$graph_response = $facebook->get("/me?fields=name,email", $access_token);
$facebook_user_info = $graph_response->getGraphUser();
if(!empty($facebook_user_info['id']))
{
$_SESSION['user_image'] = 'http://graph.facebook.com/'.$facebook_user_info['id']. '/picture';
}
if(!empty($facebook_user_info['name']))
{
$_SESSION['user'] = $facebook_user_info['name'];
}
if(!empty($facebook_user_info['email']))
{
$_SESSION['user_email_address'] = $facebook_user_info['email'];
}
}
else
{
$facebook_permissions = ['email'];
$facebook_login_url = $facebook_helper->getLoginUrl('https://www.stolve.com/stolve-master/login.php', $facebook_permissions);
$facebook_login_url = '<div align="center"><img src="php-login-with-facebook.gif alt="facebook logo"/></div>';
}

access_token issue in facebook lib for codeigniter

i build facebook login in mysite with facebook library used by codeigniter framework
the login works good but in the end i see this error
ERROR - 2013-07-29 06:51:51 --> Severity: Notice --> Undefined index: access_token /home/dtworks/public_html/amd/xta2/application/libraries/facebook.php 32
libraries/facebook.php
function get_facebook_cookie() {
$CI = & get_instance();
$app_id = $CI->config->item('facebook_app_id');
$application_secret = $CI->config->item('facebook_app_secret');
if (isset($_COOKIE['fbsr_' . $app_id])) {
list($encoded_sig, $payload) = explode('.', $_COOKIE['fbsr_' . $app_id], 2);
$sig = base64_decode(strtr($encoded_sig, '-_', '+/'));
$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);
if (strtoupper($data['algorithm']) !== 'HMAC-SHA256') {
return null;
}
$expected_sig = hash_hmac('sha256', $payload, $application_secret, $raw = true);
if ($sig !== $expected_sig) {
return null;
}
$token_url = "https://graph.facebook.com/oauth/access_token?"
. "client_id=" . $app_id . "&client_secret=" . $application_secret . "&redirect_uri=" . "&code=" . $data['code'];
$response = #file_get_contents($token_url);
$params = null;
parse_str($response, $params);
$data['session_key'] = $data['code'];
$data['access_token'] = $params['access_token']; // line 32
return $data;
} else {
return null;
}
}
controllers/auth_other
// this function to signin with facebook
function fb_signin() {
// load facebook library
$this->load->library('facebook'); // this has been loaded in autoload.php
// get the facebook user and save in the session
$fb_user = $this->facebook->getUser();
if (isset($fb_user)) {
$this->session->set_userdata('facebook_id', $fb_user['id']);
$user = $this->user_model->get_user_by_sm(array('facebook_id' => $fb_user['id']), 'facebook_id');
if (sizeof($user) == 0) {
redirect('auth_other/fill_user_info', 'refresh');
} else {
// simulate what happens in the tank auth
$this->session->set_userdata(array('user_id' => $user[0]->id, 'username' => $user[0]->username,
'status' => ($user[0]->activated == 1) ? STATUS_ACTIVATED : STATUS_NOT_ACTIVATED));
// $this->tank_auth->clear_login_attempts($user[0]->email); //can't run this when doing FB
$this->users->update_login_info($user[0]->id, $this->config->item('login_record_ip', 'tank_auth'), $this->config->item('login_record_time', 'tank_auth'));
redirect('auth', 'refresh');
}
} else {
echo 'cannot find the Facebook user';
}
}

App Redirect to Facebook Home Page

This is My Facebook Application Source
<?php
require 'facebook.php';
require 'config.php';
if (isset($_GET['code'])){
header("Location: " . $canvasPage);
exit;
}
$fb = new Facebook(array(
'appId' => 'xxx',
'secret' => 'xxxx',
'cookie' => true,
'fileUpload' => true,
));
$me = null;
$user = $fb->getUser();
if($user) {
try {
$me = $fb->api('/me');
} catch(FacebookApiException $e) {
error_log($e);
}
}
//edit the permissions needed
$permsneeded='publish_stream,user_photos';
if ($me){}
else {
$loginUrl = $fb->getLoginUrl(array(
'scope' => $permsneeded,
));
echo "
<script type='text/javascript'>
window.top.location.href = '$loginUrl';
</script>
";
exit;
}
if(isset($_GET['signed_request'])) {
$fb_args="signed_request=". $_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"]))
{
echo("");
}
$access_token = $data["oauth_token"];
try {
$my = $fb->api('/me');
}catch(FacebookApiException $e) {
error_log($e);
}
$nam = $my['name'];
$images = array(
0 => 'celeb/1.jpg',
1 => 'celeb/1.jpg',
3 => 'celeb/1.jpg',
4 => 'celeb/1.jpg',
5 => 'celeb/1.jpg',
6 => 'celeb/1.jpg',
);
$image = $images[ rand(0,(count($images)-1)) ];
Some other php code
?>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:og="http://opengraphprotocol.org/schema/" >
My Code .
</html>
i got 2 Problems .
It is redirect to Facebook Home Page . See Demo : https://apps.facebook.com/246435698815637/
this is "$image = $images[ rand(0,(count($images)-1)) ];" is not working some time . it is didn't get image for imagecreatefromjpeg
Try like this and please avoid the blank statements.
if(!($me))
{
echo '<script> top.location.href="'.$facebook->getLoginUrl(array(
'req_perms' => $permsneeded,
'next' =>'https://apps.facebook.com/246435698815637/',
)). '";
</script>';
exit();
}
and second one is correct,it should work,but i don't get your point about imagecreatefromjpeg as you don't give any information about it.Paste your code where you have use that function.

$signed_request = $facebook->getSignedRequest();

im try to make a fan gate content for my facebook fan page by singed_request. The code work perfectly but i don't want redirect in iframe, i need redirect after the user click LIKE to another page. How i can remove iframe and insert a redirect?
this is my code:
<?php
require_once('facebook.php');
$app_id = "id";
$app_secret = "secret";
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
$signed_request = $facebook->getSignedRequest();
function parsePageSignedRequest() { 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)); return $data; } >return false; } if($signed_request = parsePageSignedRequest()) { if($signed_request->page->liked) { echo "<iframe allowtransparency=\"true\" frameborder=\"0\" SCROLLING=\"YES\" style=\"width: 800px; height: 1000px;\" src=\"http://onlyimagination.com/dm3theme2\" id=\"any_name\" name=\"anyname\"><iframe>";
} else { echo "<img src=\"http://www.onlyimagination.com/facebook/crazyvideo/img.jpg\" width=\"582\" height=\"487\">"; } }
?>
This will only work if don't have any output before your code
if ($signed_request = parsePageSignedRequest()) {
if ($signed_request->page->liked) {
header('Location: http://onlyimagination.com/dm3theme2');
} else {
echo "<img src=\"http://www.onlyimagination.com/facebook/crazyvideo/img.jpg\" width=\"582\" height=\"487\">";
}
}

Facebook Check whether user has liked the page or not

When i check whether use has liked my page or not.My app is getting permissions from the user and taking him to my domain page with blank page.
When i remove the code with which i'm using to check like or not,it is working correctly.
This is the code,i'm using to check like or not
$signed_request = $facebook->getSignedRequest();
$liked = $signed_request['page']['liked'];
if ( $liked ) :
else :
endif;
Is this code correct or not?
I do it like that:
$signed_request = $_REQUEST['signed_request'];
function parsePageSignedRequest() {
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));
return $data;
}
return false;
}
if($signed_request = parsePageSignedRequest()) {
if($signed_request->page->liked) {
echo "FAN";
} else {
echo "NO FAN";
}
}

Categories