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' ) ;
}
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 am using the following code to post on facebook from my page but the lonin page is not loading
you can see the page
When I click on the following link the facebook required page is not loading
The code is look 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>
Please help me in this case and please give me the detail answer, and be with me
There are a couple problems with what you're doing here.
First it's always best to have valid markup ie doctype and html tags your page is missing. but more importantly... this
echo 'Login';
is wrong. the markup is just as it looks I think you're expecting a link. Change it to
echo 'Login;
and try again. the reason is php does not recognize variables in a single quoted string.
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 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.
I have the following script which will allow a user to login to my website using their facebook account, request the appropriate permissions, and if accepted, it will post a message on a wall:
<?php
require 'facebook.php';
$facebook = new Facebook(array(
'appId' => 'removed for security reasons',
'secret' => 'removed for security reasons',
'cookie' => true,
));
$session = $facebook->getSession();
if ($session) {
if (isset($_GET[id])) {
$post = $facebook->api("/" . $_GET['id'] . "/feed", "POST", array('message' => 'Hello!'));
echo 'A message has been posted on your friends wall';
} else {
$friends = $facebook->api('/me/friends');
foreach ($friends as $key=>$value) {
echo 'You have ' . count($value) . ' friends<br />';
foreach ($value as $fkey=>$fvalue) {
echo 'friend id = ' . $fvalue[id] . ' - friend name = ' . $fvalue[name] . ' - post message<br />';
}
}
}
} else {
$loginUrl = $facebook->getLoginUrl(array(
'req_perms' => 'publish_stream',
'next' => 'http://'.$_SERVER['SERVER_NAME'].'/stage0.php',
'cancel_url' => 'http://'.$_SERVER['SERVER_NAME'].'/cancel.php',
));
header('Location: '.$loginUrl);
}
?>
I then have the following script to log the user out:
<?php
error_reporting( E_ALL | E_STRICT );
ini_set('display_errors', 1);
?>
<?php
session_start();
?>
<html>
<head>
<title></title>
<?php
$_SESSION = array();
session_destroy();
?>
<meta http-equiv="refresh" content="0;index.php">
</head>
<body>
<h1>logout</h1>
</body>
</html>
This clears the sessions I created, but it does not log them out of facebook at all. How do I clear the sessions I create and also log them out of facebook?
Recommended way to log out a user from both your application and Facebook, is to call the logout feature of the javascript SDK.
logout
check : http://developers.facebook.com/docs/reference/javascript/FB.logout/
/* Use access token i.e. $_SESSION['facebookAccessToken']*/
function logoutUser($sessionKey)
{
include_once 'facebook.php';
$facebook = new Facebook( array('appId'=>FACEBOOK_APP_ID , 'secret'=>FACEBOOK_APP_SEC , 'cookie' => true));
$logoutUrl = $facebook->getLogoutUrl(array('next' => BASE_URL , 'session_key' => $sessionKey));
return $logoutUrl;
}
$access_array = explode('|' , $_SESSION['facebookAccessToken']);
$sessionKey = $access_array[1];
$redirectUrl = logoutUser($sessionKey);
header('Location: '.$redirectUrl );
Use this as your logout URL:
Logout
And this for your code:
if(isset($_GET['action']) && $_GET['action'] === 'logout'){
$facebook->destroySession();
}
This will log the user out of the app, rather than facebook itself, logging out of facebook altogether, which is not recommended, as it will distress some users, add this to your session where if($session) { is
$logoutUrl = $facebook->getLogoutUrl();
then for your logout URL, use:
Logout
Facebook Oauth Logout