Why does my Facebook app isn't loading pages? - php

I'm making an app on Facebook using WordPress and everything is fine, I'm on the front page of the app, but when I try to access a link to another page of my app in Mozilla Firefox shows a blank page and in Google Chrome stays on the front page.
Has anyone an idea of why this is happening? Has anyone encountered this behaviour?
My next code is used by me in the WordPress root index.php, not the front page index.php, to request permissions of the user before entering the app:
<?php
/**
* Front to the WordPress application. This file doesn't do anything, but loads
* wp-blog-header.php which does and tells WordPress to load the theme.
*
* #package WordPress
*/
/**
* Tells WordPress to load the WordPress theme and output it.
*
* #var bool
*/
session_start();
require 'src/facebook.php';
// LOGIN
$app_details = array('app_id' => 'secret',
'app_secret' => 'secret',
'page_link' => '//www.facebook.com/secret/'
);
$http = "http:";
if(isset($_SERVER['HTTPS']))
if (!empty($_SERVER['HTTPS'])) $http = "https:";
$facebook = new Facebook(array(
'appId' => $app_details['app_id'],
'secret' => $app_details['app_secret'],
'cookie' => true
));
$the_signed_request = $facebook->getSignedRequest();
if ($the_signed_request == null) {
header("Location: ".$http.$app_details['page_link']."app/".$app_details['app_id']);
}
if ( !isset($the_signed_request['user_id']) && !isset($the_signed_request['oauth_token']) ){
$loginUrl = $facebook->getLoginUrl(array('scope'=>'email'));
if (!isset($_SESSION['rejects'])) $_SESSION['refuzuri'] = 0;
else $_SESSION['rejects']++;
if ($_SESSION['rejects']>4) {
echo "This app works only if permissions are accepted.<br/>";
echo "To accept the permissions <a href='".$loginUrl."' target='_top'>click here</a>";
die();
}
echo( "<script>top.location.href='" . $loginUrl . "';</script>" );
}else{
try{
$access_token = $facebook->getAccessToken();
$user = $facebook->getUser();//userid
$userinfo = $facebook->api('/me?fields=id,name,email,gender,locale'); //for basic details
}catch (FacebookApiException $e) {
$user = null;
$userinfo = null;
}
$_SESSION['user']= $userinfo;
define('WP_USE_THEMES', true);
/** Loads the WordPress Environment and Template */
require( dirname( __FILE__ ) . '/wp-blog-header.php' );
}
?>

Related

facebook PHP SDK redirect_uri to page tab

im using the PHP SDK to get user to authorize my app. the thing that is a bit tricky is that i;m doing this inside a tab. the redirect uri will break the tab 'iframe' and just open as a normal app,i do not want this. how can i keep it inside the page tab iframe. below is my code;
<?php
include("utility.php");
include("db_setup.php");
require_once "base_facebook.php";
require("facebook.php");
$facebook = new Facebook(array(
'appId' => 'xxxxxx',
'secret' => 'xxxxxx',
));
$conn = connectUserDB();
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
//$user_profile = $facebook->api('/me?fields=name,id,email');
header( 'Location: submitted.php' ) ;
} catch (FacebookApiException $e) {
//print_r($e);
$user = null;
}
}
// $user is null : $user is either not logged in or the token is not valid
else if(!$user) {
$params = array(
'scope' => 'email',
'redirect_uri' => 'submitted.php'
);
$loginUrl = $facebook ->getLoginUrl($params);
echo '<script type="text/javascript">';
echo 'top.location.href = " '.$loginUrl.' " ; ';
echo '</script>';
}
?>
If you need to log in user without having him/her leave your tab, you could use javascript SDK (you can call function FB.login() which will open popup).
If you want to keep using php, on your "normal app" (the page that gets opened in iframe on Facebook) you can simply make a redirect back to your tab on Facebook. So that after user gets logged in and redirected to your page, you redirect him back to Facebook tab.

PHP Facebook API issue with business page

I have a Facebook app which posts news from a company website to the company's Facebook business page.
When I test it with my developer account's id or my private account id, it works great. But when I use the id of the business page, no news show up on the chronic.
This is my code:
<?php
require_once 'library/facebook.php';
$facebook = new Facebook(array(
'appId' => FACEBOOK_APP_ID,
'secret' => FACEBOOK_APP_SECRET,
'cookie' => true,
));
if(is_null($facebook->getUser())){
header("Location:{$facebook->getLoginUrl(array('req_perms' => 'user_status,publish_stream,user_photos,offline_access,publish_checkins'))}");
exit;
}
if($facebookpost){
try {
$facebook->setFileUploadSupport("http://" . $_SERVER['SERVER_NAME']);
$img = $sitepath.$gfxpath.$filename;
$arguments = array(
'source' => '#' . $img,
'message' => utf8_encode(html_entity_decode(strip_tags($text))),
);
$facebook->api("/".FACEBOOK_PROFILE_ID."/photos", 'post', $arguments);
} catch (FacebookApiException $e) {
print $e;
}
}
?>
The app shows up correctly in the profile setting (image)
are there some different settings for business pages?
You need to use the page's access token to post updates to your page. You can get the access token for the page by calling the following API endpoint:
/me/accounts, then look for your page ID / Name.

How do I update Facebook status from my own website?

I have my own site which I am creating a blog/news entries but I want these to upload directly to my Facebook page as I add them.
Is there a simple way to do this?
My code is something like this
<?php
$title = "$_Post['title'];
$article = "$_Post['article'];
// Upload sql and query code code
?>
Is there a code or api I can use to also update to my facebook page
You will need to sign up to become a facebook developer, create an application, and then using the PHP SDK you can do exactly this.
Developers site: http://developers.facebook.com/
PHP SDK: http://developers.facebook.com/docs/reference/php/
Example code:
http://www.masteringapi.com/tutorials/how-to-post-on-facebook-page-as-page-not-as-admin-user-using-php-sdk/31/
<?php
// This code is just a snippet of the example.php script
// from the PHP-SDK <http://github.com/facebook/php-sdk/blob/master/examples/example.php>
require '../src/facebook.php';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => 'app_id',
'secret' => 'app_secret',
));
// Get User ID
$user = $facebook->getUser();
if ($user) {
try {
$page_id = 'page_id';
$page_info = $facebook->api("/$page_id?fields=access_token");
if( !empty($page_info['access_token']) ) {
$args = array(
'access_token' => $page_info['access_token'],
'message' => "I'm a Page!"
);
$post_id = $facebook->api("/$page_id/feed","post",$args);
}
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
// Login or logout url will be needed depending on current user state.
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl(array('scope'=>'manage_pages,publish_stream'));
}
?>

Facebook login script not prompting facebook login window

Im having problems trying to implement a Facebook login script into my Zend website.
What I basically do here is
create My_Action_Helper_FBInitializer (extending Zend_Controller_Action_Helper_Abstract).This initializes facebook connection.
invoke My_Action_Helper_FBInitializer in the bootstrap (so to have it available to all my controllers)
Set my facebook app "Site URL" to : http://localhost/test/public/ ("Site domain" field left blank)
expecting to have a working login/logout button in my layout.phtml
ISSUE: the problem is that i'm never prompted with the facebook authentication window, and whenever I click on the Login button I see loaded the same page (my localhost/test/public//index/index) with 'facebook session parameters' appended to the url:
http://localhost/test/public/index/index/?session=%7B%22session_key ... [..]
here my files:
My action helper FBInitializer
class My_Action_Helper_FBInitializer extends Zend_Controller_Action_Helper_Abstract
{
public function init()
{
$controller=$this->getActionController();
$config=Zend_registry::get('config');
$controller->config=$config;
//initialize facebook
require_once('Fb/facebook.php');
$apiKey=$config->fb->apikey;
$appId=$config->fb->appid;
$secret=$config->fb->appid;
$facebook = new Facebook(array('appId' => $appId,'secret' => $secret,'cookie' => true));
$controller->session = $facebook->getSession();
$controller->fbme = NULL;
$controller->facebook=$facebook;
if (!empty($controller->session))
{
$controller->uid = $facebook->getUser();
$controller->fbme = $facebook->api('/me');
}
//
}
}
My Index controller
class IndexController extends Zend_Controller_Action
{
public function indexAction()
{
//login
$this->view->fbme=$this->fbme;
$this->view->fb=$this->facebook;
}
}
My layout extract
[..]
if($this->fbme!==NULL)
{
echo"<img align='middle' src='https://graph.facebook.com/{$this->fbme['id']}/picture'/>";
echo "Welcome ".$this->fbme['name'].'! ';
$logoutUrl = $this->fb->getLogoutUrl(array('next'=>'/index/logout'));
echo"<a href='{$logoutUrl}'>Logout</a>";
}
else
{
echo $this->fbme;
$loginUrl = $this->fb->getLoginUrl(array('req_perms' => 'email,read_stream,user_birthday,user_hometown,user_photos'));
echo "<a href='{$loginUrl}'>Login with Facebook!</a>";
}
[..]
I Dont know if Im wrong with either facebook app or Zend settings but I was testing a similar 100% WORKING script (but No zend!) that is:
$fbconfig['appid'] = "181****";
$fbconfig['api'] = "5c69*****";
$fbconfig['secret'] = "a2fc*****";
include_once "facebook.php";
// Create our Application instance.
$facebook = new Facebook(array('appId' => $fbconfig['appid'],'secret' => $fbconfig['secret'],'cookie' => true));
$session = $facebook->getSession();
$fbme = null;
// Session based graph API call.
if (!empty($session))
{
$uid = $facebook->getUser();
$fbme = $facebook->api('/me');
}
// login or logout url will be needed depending on current user state.
if ($fbme)
{
echo"<img src='https://graph.facebook.com/{$fbme['id']}/picture'/>";
echo $fbme['name'].' ';
$logoutUrl = $facebook->getLogoutUrl(array('next'=>'http://localhost/fb/logout.php'));
echo"<a href='{$logoutUrl}'>logout</a>";
}
else
{
$loginUrl = $facebook->getLoginUrl(array('req_perms' => 'email,read_stream,user_birthday,user_hometown,user_photos'));
echo"<a href='{$loginUrl}'>login</a>";
}
Thanks
Luca

class gets unset when including smarty and facebook php

I don't understand this:
I use the __autoload feature in php and this works:
include_once '../sys/core/init.inc.php';
$Intro = new Intro();
echo $Intro->ip();
exit();
init.inc.php loads the Intro class and the ip() function prints out the ip.
But this does not work
include_once '../sys/core/init.inc.php';
/*
* Smarty template engine
*/
include_once '../sys/core/smarty/Smarty.class.php';
$smarty = new Smarty;
$smarty->debugging = false;
$smarty->caching = false;
$smarty->cache_lifetime = 86400; // 24 timer
/*
* gettext translation
*/
if( ! defined('PROJECT_DIR')){ define('PROJECT_DIR', realpath('/var/www/v3/')); }
if( ! defined('LOCALE_DIR')){ define('LOCALE_DIR', PROJECT_DIR .'/sys/locale'); }
if( ! defined('DEFAULT_LOCALE')){ define('DEFAULT_LOCALE', 'no_NO'); }
include_once('../sys/core/gettext/gettext.inc');
$encoding = 'utf-8';
$locale = (isset($_COOKIE['lang']))? $_COOKIE['lang'] : DEFAULT_LOCALE;
T_setlocale(LC_MESSAGES, $locale);
$domain = 'messages';
T_bindtextdomain($domain, LOCALE_DIR);
T_bind_textdomain_codeset($domain, $encoding);
T_textdomain($domain);
/*
* facebook
*/
include_once '../sys/core/facebook/facebook.php';
// Create our Application instance (replace this with your appId and secret).
// Create our Application instance.
$facebook = new Facebook(array(
'appId' => 'xxx',
'secret' => 'xxx',
'cookie' => true,
));
$session = $facebook->getSession();
$me = null;
// Session based API call.
if ($session) {
try {
$uid = $facebook->getUser();
$me = $facebook->api('/me'); // me?fields=email
$email = $facebook->api('me?fields=email');
} catch (FacebookApiException $e) {
error_log($e);
}
}
// login or logout url will be needed depending on current user state.
if ($me) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl(
array('req_perms' => 'email, publish_stream')
);
}
$Intro = new Intro();
echo $Intro->ip();
exit();
Now I get Fatal error: Class 'Intro' not found in ... on line 92
I don't understand this because the only code I have added is required includes for facebook, gettext and smarty. Before the includes I can call classes, but not after I have added the includes. This makes no sense to be. Can someone explain.
Try to call your class directly after the include:
include_once '../sys/core/init.inc.php';
$Intro = new Intro();
/*
* Smarty template engine
*/
include_once '../sys/core/smarty/Smarty.class.php';
$smarty = new Smarty;
$smarty->debugging = false;
$smarty->caching = false;
$smarty->cache_lifetime = 86400; // 24 timer
/*
* gettext translation
*/
if( ! defined('PROJECT_DIR')){ define('PROJECT_DIR', realpath('/var/www/v3/')); }
if( ! defined('LOCALE_DIR')){ define('LOCALE_DIR', PROJECT_DIR .'/sys/locale'); }
if( ! defined('DEFAULT_LOCALE')){ define('DEFAULT_LOCALE', 'no_NO'); }
include_once('../sys/core/gettext/gettext.inc');
$encoding = 'utf-8';
$locale = (isset($_COOKIE['lang']))? $_COOKIE['lang'] : DEFAULT_LOCALE;
T_setlocale(LC_MESSAGES, $locale);
$domain = 'messages';
T_bindtextdomain($domain, LOCALE_DIR);
T_bind_textdomain_codeset($domain, $encoding);
T_textdomain($domain);
/*
* facebook
*/
include_once '../sys/core/facebook/facebook.php';
// Create our Application instance (replace this with your appId and secret).
// Create our Application instance.
$facebook = new Facebook(array(
'appId' => 'xxx',
'secret' => 'xxx',
'cookie' => true,
));
$session = $facebook->getSession();
$me = null;
// Session based API call.
if ($session) {
try {
$uid = $facebook->getUser();
$me = $facebook->api('/me'); // me?fields=email
$email = $facebook->api('me?fields=email');
} catch (FacebookApiException $e) {
error_log($e);
}
}
// login or logout url will be needed depending on current user state.
if ($me) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl(
array('req_perms' => 'email, publish_stream')
);
}
echo $Intro->ip();
exit();
Shouldn't work either, but may be a good start for debugging. However, it might be useful to post your Intro class.
This is a simple guess, i haven't really used these librairies (except for Smarty) in a while, but it might be that one of the libraries (Facebook or Gettext) also define a __autoload function that gets called instead of your own.
You can display what is used by PHP for autoloading by calling var_dump(spl_autoload_functions()).
Also, your autoload function might be using a constant or variable getting overwritten by the includes.

Categories