I have a problem on Facebook App.
I want to make a fan-gate when any one enter the app url, Ex. apps.facebook.com/zoomcompetition/
I can't make it on the tab because it display blank.
but it works on the app url so I want to add fan-gate on the app appoint to Facebook Page.
This is the code
<?php
require 'src/facebook.php';
//require 'fbconfig.php';
require 'functions.php';
// Create An instance of our Facebook Application.
$facebook = new Facebook(array(
'appId' => ***************,
'secret' => **************,
));
// Get the app User ID
$user = $facebook->getUser();
if ($user) {
try {
// If the user is authenticated and logged-in
$user_profile = $facebook->api('/me');
//var_dump($user_profile);
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
// If the user is authenticated then generate the variable for the logout URL
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
?>
<!-- Insert Logged in HTML here -->
Logout
<?php
//Always place this code at the top of the Page
session_start();
if (!isset($_SESSION['id'])) {
// Redirection to login page facebook
header("location: index.php");
}
echo ' Welcome '.$_SESSION['username']; // or whatever you want your user to see.
$varri = "http://www.*****.ps";
?>
<iframe src=<?php print $varri; ?> scrolling="auto" width=100% height=100% frameborder="0"> </iframe>
<?php
$user = $facebook->getUser();
if ($user) {
$ret_obj = $facebook->api('/me/feed', 'POST',
array(
'link' => 'https://www.************.com',
'picture' => 'http://www.*******************.jpg',
'caption' => "أهلا وسهلا!",
'message' => ' Welcome '.$_SESSION['username'],
'uid'=> $facebook->getUser()
));
}
?>
<?php
} else {
$loginUrl = $facebook->getLoginUrl();
}
if (!empty($user_profile )) {
$username = $user_profile['name'];
$uid = $user_profile['id'];
$email = $user_profile['email'];
$user = new User();
$userdata = $user->checkUser($uid, 'facebook', $username,$email,$twitter_otoken,$twitter_otoken_secret);
if(!empty($userdata)){
session_start();
$_SESSION['id'] = $userdata['id'];
$_SESSION['oauth_id'] = $uid;
$_SESSION['username'] = $userdata['username'];
$_SESSION['email'] = $email;
$_SESSION['oauth_provider'] = $userdata['oauth_provider'];
//header("Location: home.php");
?>
<!--<!DOCTYPE>
<HTML>
<head>
<script>
function run(){
window.location.href = '/facebook/home.php';
}
</script>
</head>
<body onLoad="run()">
</body>
</html> -->
<?php
}
} else {
die("There was an error.");
}
}
else {
// Generate a session if there is none.
$login_url = $facebook->getLoginUrl(array( 'scope' => ' read_stream, publish_stream, email'));
?>
<!DOCTYPE>
<HTML>
<head>
<script>
function run(){
window.location.href = '<?php echo $login_url ?>';
}
</script>
</head>
<body onLoad="run()">
</body>
</html>
<?php
}
?>
Can you help me to solve one of the following:
What do I need to make it not display "Blank" on the tab?
2- How can I make a fan-gate for specific Facebook Pae if any one go to App
url directly?
What do I need to make it not display "Blank" on the tab?
Generate some output instead of nothing, I’d guess …?
How can I make a fan-gate for specific Facebook Pae if any one go to App url directly?
You can redirect from the canvas page to a specific page/app combination via JavaScript (top.location.href = "FB-Address of your page/app id combo"). And to check whether you’re on the canvas page, the simplest way to do so is give it an extra GET parameter in the canvas page address, which you then can evaluate in your code.
Related
i have created a facebook app and it works well for new users if they start the app with the direct server link (https://www.mydomain.com/app/index.php) and use the login button.
But if they try to use the app within facebook (https://apps.facebook.com/myapp) as a canvas app(for the first time) then they see the "landing" page of my app and the log in button. But if they click on it, an errormessage appears, that the content of the iframe couldn't be displayed. If they start the app in a new tab, then the login process works. (see the error below, sorry it's german, maybe you are familiar with this type of error message?)
It would be desirable if the new user could use the login link in the canvas app.
How can this be achieved? My index.php looks like this: (i am using the facebook SDK)
<?php
require_once('php-sdk/facebook.php');
$config = array(
'appId' => 'YOUR_APP_ID',
'secret' => 'YOUR_APP_SECRET',
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
?>
<html>
<head></head>
<body>
<?php
if($user_id) {
try {
$user_profile = $facebook->api('/me','GET');
echo "Name: " . $user_profile['name'];
} catch(FacebookApiException $e) {
$login_url = $facebook->getLoginUrl();
echo 'Please login.';
error_log($e->getType());
error_log($e->getMessage());
}
} else {
$login_url = $facebook->getLoginUrl();
echo 'Please login.';
}
?>
</body>
</html>
i have created a facebook app and it works well for new users if they start the app with the direct server link (https://www.mydomain.com/app/index.php) and use the login button.
But if they try to use the app within facebook (https://apps.facebook.com/myapp) as a canvas app(for the first time) then they see the "landing" page of my app and the log in button. But if they click on it, an errormessage appears, that the content of the iframe couldn't be displayed. If they start the app in a new tab, then the login process works. (see the error below, sorry it's german, maybe you are familiar with this type of error message?)
It would be desirable if the new user could use the login link in the canvas app.
How can this be achieved? My index.php looks like this: (i am using the facebook SDK)
<?php
require_once('php-sdk/facebook.php');
$config = array(
'appId' => 'YOUR_APP_ID',
'secret' => 'YOUR_APP_SECRET',
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
?>
<html>
<head></head>
<body>
<?php
if($user_id) {
try {
$user_profile = $facebook->api('/me','GET');
echo "Name: " . $user_profile['name'];
} catch(FacebookApiException $e) {
$login_url = $facebook->getLoginUrl();
echo 'Please login.';
error_log($e->getType());
error_log($e->getMessage());
}
} else {
$login_url = $facebook->getLoginUrl();
echo 'Please login.';
}
?>
</body>
</html>
I want to change my code so, that first it will check if the user has liked the page. After this (if yes) a question will come up, to accept the terms and if he clicks on the accept button the Facebook permissions request dialog will come up. How can i do this?
The problem is that everytime a user clicks on the app link the $user variable is always false, but he is logged in...
<?php
require 'facebook.php';
$facebook = new Facebook(array(
'appId' => 'xxxxx',
'secret' => 'yyyyy',
'baseUrl' => 'http://hosting.address/',
'appBaseUrl' => 'http://apps.facebook.com/app-name/',
'fileUpload' => 'true',
));
// Get User ID
$user = $facebook->getUser();
$params = array(
scope => 'publish_stream,user_photos',
redirect_uri => 'http://www.facebook.com/xxxxx?sk=app_yyyyy&app_data=1'
);
$signed_request = $facebook->getSignedRequest();
$app_data = $signed_request["app_data"];
?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<title>the title</title>
</head>
<body>
<?php if ($user){
try {
$user_profile = $facebook->api('/me');
$likes = $facebook->api("/me/likes/123123123"); //page ID
if( !empty($likes['data']) ){
$scope = 'publish_stream,user_photos';
$scope_params = explode(',',$scope);
$permissions = $facebook->api("/me/permissions");
if( array_key_exists('publish_stream', $permissions['data'][0]) && array_key_exists('user_photos', $permissions['data'][0]) && isset($app_data)) {
} else {
echo "accept</td>";
echo "deny</td>";
}
}else{
echo "<img width=\"520px\" src=\"no-fan.jpg\" />";
}
} catch (FacebookApiException $e) {
echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>';
$user = null;
}
}
if ($user) {
} else {
$loginUrl = $facebook->getLoginUrl($params);
echo '<script type="text/javascript">top.location.href = "'.$loginUrl .'";</script>';
}
?>
</body>
</html>
Try following this excellent example:
http://www.jelecos.com/default/blog/11-04-18/Facebook_PHP_SDK_Fangate_Creation.aspx
Then on the fanpage.php have your code to ask them to log in.
just as the picture I attached, it shows 1 app requests, but there is no apps listed there.
the apprequests sent from the graph api using javascript sdk
Any App Reqeusts you set with you app will need to be deleted by your app. Sample from blog shows how to concatenate the request_id and user_id in order to delete the outstanding requests.
Refer to http://developers.facebook.com/blog/post/569/
<?php
require_once('php-sdk/facebook.php');
$config = array(
'appId' => 'YOUR_APP_ID',
'secret' => 'YOUR_APP_SECRET',
);
$facebook = new Facebook($config);
?>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:fb="https://www.facebook.com/2008/fbml">
<head>
<title>Deleting Requests Example</title>
</head>
<body>
<?php
$user_id = $facebook->getUser();
if ($user_id) {
//Get the Request Ids
$request_ids = explode(',', $_REQUEST['request_ids']);
//Construct full Request_Id
function build_full_request_id($request_id, $user_id) {
return $request_id . '_' . $user_id;
}
//For each Request construct full Request_id and Delete
foreach ($request_ids as $request_id) {
echo ("reqeust_id=".$request_id."<br>");
$full_request_id =
build_full_request_id($request_id,$user_id);
echo ("full_reqeust_id=".$full_request_id."<br>");
try {
$delete_success =
$facebook->api("/$full_request_id",'DELETE');
if ($delete_success) {
echo "Successfully deleted " . $full_request_id;
}
else {
echo "Delete failed".$full_request_id;
}
}
catch (FacebookApiException $e) {
echo "error";
}
}
}
//User TOS if user has not authenticated your App
else if (!isset($_REQUEST['error'])){
$params = array(
'redirect_uri' => 'http://localhost/~namitag/requests.php'
);
$loginUrl = $facebook->getLoginUrl($params);
echo
'<script>window.top.location.href="'.$loginUrl.'";</script>';
}
else {
echo ("user denied permission"); }
?>
</body>
</html>
I'm very new to facebook platform.
I developed an iframe app. which after allowing permsiions, opens the application on my server and not in facebook iframe.
I hope i'm clear. I'm using $user = $facebook->require_login().
I read somewhere that adding $facebook->require_frame() would help but it didn't help.
The page reloads with new access token on each reload.
Someoe please help me.
Thanks in advance
Can you post some of the code you're using for the redirect?
If you use $facebook->GetLoginUrl to construct the Auth URL, the code should look like this -
$loginUrl = $facebook->getLoginUrl(array('canvas' => 1,
'fbconnect' => 0,
'req_perms' => 'permissions_here',
'next' => $canvas_base_url . 'index.php',
'cancel_url' => $canvas_base_url
));
In the Array, define your post authentication URL in 'next'. You should build it using the URL structure of http://apps.facebook.com/CANVASURL/yourpostauthpage.php.
In my snippet, I've created a variable ($canvas_base_url) to store my base URL.
my code is
<?
$api_key = 'xxxxxxxxxxxx';
$secret = 'xxxxxxxxxxxxx';
$appcallbackurl = 'http://www.phonecurry.com/fbapp/';
include_once 'facebook.php';
$facebook = new Facebook($api_key, $secret);
$user = $facebook->require_login();
try
{
if(!$facebook->api_client->users_isAppUser())
{
?>
<script language="JavaScript">
<!--
window.location = "http://www.facebook.com/login.php?v=1.0&api_key=xxxxxxxxxxxxx&next=http%3A%2F%2Fapps.facebook.com%2Fmycanvasapp%2F&canvas&req_perms=publish_stream";
</script>
<?php
}
}
catch (Exception $ex)
{
$facebook->set_user(null, null);
$facebook->redirect($appcallbackurl);
}
$has_permission = $facebook->api_client->users_hasAppPermission("publish_stream");
if(!$has_permission)
{
?>
<script language="JavaScript">
<!--
window.location = "http://www.facebook.com/login.php?v=1.0&api_key=xxxxxxx&next=http%3A%2F%2Fapps.facebook.com%2Fmycanvasapp%2F&canvas&req_perms=publish_stream";
</script>
<?php
}
?>