I have a little Facebook app, that posts some preloaded images. I changed the code structure, so i moved all the variables into config.php, so it would be more comfortable. But after that the app doesn't work and i really don't know why... After the permission dialog it realoads himself over and over... Can someone fing the bug?
This is the index.php:
<?php
require 'facebook.php';
require 'config.php';
$facebook = new Facebook(array(
'appId' => $appId,
'secret' => $appSecret,
'baseUrl' => $baseUrl,
'appBaseUrl' => $appBaseUrl,
'fileUpload' => 'true',
'cookie' => 'true'
));
if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) {
$baseUrl = str_replace('http','https',$baseUrl);
$fbPageURL = str_replace('http','https',$fbPageURL);
$fbPageAppURL = str_replace('http','https',$fbPageAppURL);
}else{
// not https
}
$signed_request = $facebook->getSignedRequest();
$page_id = $signed_request["page"]["id"];
$like_status = $signed_request["page"]["liked"];
if(!$like_status){
header("Location: notfan.php");
exit;
}
$user = $facebook->getUser();
$params = array(
scope => 'publish_stream,user_photos',
redirect_uri => $fbPageAppURL
);
$loginUrl = $facebook->getLoginUrl($params);
$app_data = $signed_request["app_data"];
?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<title><?php echo $appName; ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<style>
a:link {color:#f1effc;}
a:visited {color:#f1effc;}
a:hover {color:#19378d;}
a:active {color:#19378d;}
a:link {text-decoration: none}
</style>
</head>
<body>
<script type="text/javascript">
function NotInFacebookFrame() {
return top === self;
}
function ReferrerIsFacebookApp() {
if(document.referrer) {
return document.referrer.indexOf("apps.facebook.com") != -1;
}
return false;
}
if (NotInFacebookFrame() || ReferrerIsFacebookApp()) {
top.location.replace("<?= $fbPageAppURL ?>");
}
</script>
<?php if($user){
try {
$user_profile = $facebook->api('/me');
$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])) {
// the main app logic here
} else {
$user = null;
}
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
if ($user) {
// logged in user
} else {
// not logged in or not authenticated
echo '<script type="text/javascript">top.location.href = "'.$loginUrl .'";</script>';
}
?>
</body>
</html>
and the config.php:
<?php
$appName = 'the name';
$appId = '123456789';
$appSecret = '1324v3434v34v';
$baseUrl = 'http://www.domain.com/app-name/';
$appBaseUrl = 'http://apps.facebook.com/zzzzzzz/';
$fbPageURL = 'http://www.facebook.com/xxxxxxx';
$fbPageAppURL = $fbPageURL.'?sk=app_'.$appId;
?>
Thank you very much!!
As it turned out, it was a server configuration failure.
Wrap $user = $facebook->getUser(); in a try/catch and echo the exception that gets caught. That will tell you why the user is not getting logged on.
More details - after the comments below.
You check document referrer in your Javascript - but the actual referrer in my tests was "http://static.ak.facebook.com/platform/page_proxy.php?v=4" so this would fail that condition. This will causes a loop.
To check if you're in a canvas, just get the signed_request using the PHP SDK
$facebook->signed_request();
If this is empty then you're not in a canvas (or a page tab) so redirect to the canvas. Do that in PHP and not in Javascript.
Related
Am, building an application which would post comments on users birthday wish post, but,the problem is with comment graph api, the statement is executing once and the whole application is breaking up, I did debug with echos, and its the same, no code is executing after the one comment. please help
<?php
require 'src/facebook.php';
$facebook = new Facebook(array(
'appId' => '',
'secret' => ''
));
$app_url = "https://apps.facebook.com/appleappspace/";
$uid = $facebook->getUser();
?>
<html>
<head>
<body>
<?php
if(!$uid){
$params = array(
'scope' => 'user_birthday,user_friends,friends_birthday,friends_status,
read_stream,email,status_update,publish_actions',
'redirect_uri' => 'http://apps.facebook.com/appleappspace/'
);
$loginUrl = $facebook->getLoginUrl($params);
echo "<script type='text/javascript'> top.location.href = '$loginUrl';</script>";
}else if($uid){
try{
$strings = array("b'day","birthday","bday","hbd","bdae","Birthday");
echo "Successfully Logged In <br/>";
$yesterday = mktime(0,0,0,date("m"),date("d")-3,date("Y"));
$yesterday = date("Y-m-d",$yesterday);
$user_wishes = $facebook->api('/me/feed?since='.$yesterday,'GET');
$length_user_wishes = count($user_wishes['data']);
$string_length = count($strings);
for($i=0;$i<=$length_user_wishes;$i++){
for($j=0;$j<=$string_length;$j++){
if(strchr($user_wishes['data'][$i]['message'],$strings[$j]) !== FALSE){
$POST_ID = $user_wishes['data'][$i]['id'];
$comment = "Its not Working";
$facebook->api('/'.$POST_ID.'/likes','POST');
$facebook->api('/'.$POST_ID.'/comments','POST',array('message' => $comment));
// nothing executes after this statement, whole program breaks up.
}
}
}
} catch(FacebookApiException $e) {
$echo($e);
$uid = NULL;
}
}
?>
</body>
</head>
</html>
I use the following code to connect to the facebook via my site, but the facebook page is not loading.
The cook is like that
<?php
require 'src/facebook.php';
$facebook = new Facebook(array(
'appId' => '198516340340394',
'secret' => 'f11117b96e0996ecbf7d7f4919c0cf70',
'cookie' => true
));
$user = $facebook->getUser();
$user_profile = null;
if ($user) {
try {
$user_profile = $facebook->api('/me');
$facebook->api('/me/feed/', 'post', array(
'message' => 'I want to display this message on my wall'
));
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
if ($user_profile) {
$logoutUrl = $facebook->getLogoutUrl();
echo 'Logout';
} else {
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'publish_stream, read_friendlists',
));
echo 'Login';
}
?>
<html>
<head>
<title>PHP SDK</title>
</head>
<body>
</body>
</html>
and when I click on the login
If you click on that login here it does not load the facebook
what should I do in this case
You're outputting logoutUrl rather than loginUrl
Change:
echo 'Login';
to use
echo 'Login';
I'm trying to do login via FB. If I'm not logged in FB, everything works fine but if I'm logged in FB and I click on "login via facebook!" hyperlink the URL has changed on comething like this fb2.php?code=AQAlKrGXWS2bhiZ-ZK7UPiDRLdVMjR84jAbxS41kW3I5h5P... but I'm not logged and if I'm not login till I click second time. It's strange, isn't it? Could you someone help me? The source code is here.
<?php session_start();
define('APP_ID', 'my_api_id');
define('APP_SECRET', 'my_app_secret');
define('CANVAS_PAGE', 'http://apps.facebook.com/xxx/');
define('CANVAS_URL', 'http://localhost/xxx/');
require_once 'facebook.php';
$facebook = new Facebook( array('appId' => APP_ID, 'secret' => APP_SECRET, ));
$user = $facebook->getUser();
if(isset($user)) {
try {
xtended_permission)
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
$user = NULL;
}
}
if ($user) { $logoutUrl = $facebook->getLogoutUrl(array('next' =>
"http://metalradio.cz/metalRadioTesting/facebook-sdk/logout.php")); }
else { $loginUrl = $facebook->getLoginUrl( array(
'scope' => 'email' ) ); }
?>
<!doctype html>
<html>
<head>
<title>Facebook login</title>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
</head>
<body>
<?php
if(!is_null($user))
echo "Name: " . $user_profile['email'];
?>
<h2>Users authentification via FB</h2>
<?php if ($user):
?>
logout!
<?php else:?>
<div>
login via facebook!
</div>
<?php endif?>
</body>
</html>
I'm trying to write an app that will post images the user makes to a user's wall in facebook, it was working until about an hour ago now i'm getting a catch error of "An active access token must be used to query information about the current user.". hopefully someone has a quick answer :)
here is my starting PHP that gets the user and checks for the album
<?php
include 'src/facebook.php';
$facebook = new Facebook(array(
'appId' => ID,
'secret' => SECRET,
'cookie' => true
));
$me = null;
$thisPhoto = null;
$album_name = NAME;
$album_message = MESSAGE;
try {
$me = $facebook->api('/me');
$facebook->setFileUploadSupport(true);
// check if album with name exists...if not create it
$albums = $facebook->api('/me/albums');
foreach($albums['data'] as $album) {
if($album['name'] == $album_name) {
$album_uid = $album['id'];
}
}
if (!$album_uid) {
$album_details = array(
'message'=> $album_message,
'name'=> $album_name
);
$create_album = $facebook->api('/me/albums', 'post', $album_details);
$album_uid = $create_album['id'] . "MADE";
}
//echo "<!-- $album_uid -->\n";
}
catch (FacebookApiException $e) {
echo "<!-- " . $e->getMessage() . " -->\n";
}
?>
This is my main HTML (the above is called before the doctype)
<!doctype>
<html>
<head>
<meta charset=utf-8>
<title>'TITLE</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css" media="screen">
html, body { width:100%; background-color: #FFF;}
body { margin:0 auto; padding:0; width:760px}
#flashContent { width:100%; height:100%; }
</style>
</head>
<body>
<?php
//print_r($me);
//echo $facebook->getUser();
//echo $facebook->getAccessToken();
// START
if($me) {
// true makes print_r give human readable instead of just a screen dump
//echo '<pre>' . print_r($VARIABLE, true) . '</pre>';
$appName = NAME.SWF;
$appWidth = "760";
$appHeight = "640";
$appID = "stache_yerself";
$appAlt = "Square Shooters app - 'Stache Yerself";
// ECHOS out HTML used for the APP
}
else {
$loginUrl = $facebook->getLoginUrl(array('scope' => 'publish_stream,read_friendlists,user_photos'));
echo 'Login';
}
?>
</body>
</html>
The else above gives a link which is there; however, does nothing it just goes to an empty page. I reset the "secret" on the app's developer page but didn't help. I have no idea why it just stopped working (did facebook change their code again?). Even the login thing from the HTML section was working before and now...nothing. I'm stumped as to why this just stopped working.
Did you hit your API limit for your dev account?
You can do 600 queries in 600 seconds.
Also, this might be helpful
https://www.facebook.com/help/210701362295828/
I have a Facebook PHP SDK APP in development phase and i'm stuck on something...
The logic of the APP is this:
first the user have to like the page (app is on this) // works very well :)
after that comes the permissions request
if permission is granted: the app_data variable is pass through and the user recieves a picture.
app_data variable is necessary because, if the user clicks the app after permission granted again, it gets new picture automaticly. with the app_data don't.
my problem is this link:
$params = array(
scope => 'publish_stream,user_photos',
redirect_uri => 'http://www.facebook.com/XXXXX?sk=app_YYYYYY&app_data=1'
);
Accept
If i click on it, the app shows blank page, and nothing happening... :(
On direct call, that app runs without any error.
Can someone help me out?
// sorry for my english...
Thank you!
and the whole index.php is:
this is the whole index.php:
<?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>
<style>
a:link {color:#ffffff;}
a:visited {color:#ffffff;}
a:hover {color:#ececec;}
a:active {color:#1f1f1f;}
a:link {text-decoration: none}
</style>
</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)) {
$file = "1.jpg";
$message = 'bla bla';
$ret_obj = $facebook->api('/me/photos', 'POST', array(
'source' => '#' . $file,
'message' => $message,
)
);
echo "<img width=\"520px\" src=\"1.jpg\" />";
} else {
?>
accept</td>
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>
I've had this before when I was delving into the world of Facebook apps.
What I eventually done was for my redirect_uri I used a page on my domain, e.g. https://domain.com/facebook/redirect.php and in redirect.php I would have:
// user didn't give app permissions
if (isset($_GET['error_reason']))
{
header( 'Location: http://www.facebook.com/FANPAGE' ) ;
}
else
{
header( 'Location: http://www.facebook.com/XXXXX?sk=app_YYYYYY' ) ;
}