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\">";
}
}
Related
i'm using the code below to fetch information from facebook but don't know how to get the image link (iknow $name = $test[0]; and $email = $test[4];)
define('FACEBOOK_APP_ID', '331805196916042');
define('FACEBOOK_SECRET', '9fc1c6714fb4b4dfba5acb780714ea60');
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 ($_REQUEST) {
$response = parse_signed_request($_REQUEST['signed_request'],
FACEBOOK_SECRET);
} else {
echo '$_REQUEST is empty';
}
foreach($response as $res){
foreach($res as $val){
$test[] = $val;
}
}
foreach($test[3] as $address){
$address1[] = $address;
}
if(strstr($address1[0],",")){
$country = end(explode(",",$address1[0]));
$add = explode(",",$address1[0]);
//echo $add[0]." ".$add[1]." ".$add[2];
}
and here's the iframe of facebook
<iframe src="https://www.facebook.com/plugins/registration?client_id=331805196916042&redirect_uri=http://www.mawk3y.net/news2/data.php&source=fb&fields=name,birthday,gender,location,email,first_name,last_name"
scrolling="auto"
frameborder="no"
style="border:none"
allowTransparency="true"
width="100%"
height="530">
</iframe>
The picture is accessible by the following url:
http://graph.facebook.com/[user-login]/picture
For instance for Mark Zuckerberg it will be:
http://graph.facebook.com/zuck/picture
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';
}
}
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.
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";
}
}
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
}
?>