Return to Snippet

Revision: 33557
at October 14, 2010 09:43 by chrislorenz


Updated Code
<?php
//This is my skeleton template for a PHP SDK canvas app, use it if you find it useful.
//Lately I use iframe canvas apps so I used a javascript page redirect instead of fbjs ajax with require_login();
 
//Don't forget to change path to PHP SDK if necessary
require_once 'facebook.php';
// Create our Application instance. Don't forget to change to your AppId and Secret
 
$facebook = new Facebook(array(  
'appId'  => 'XXXXXXXXXXXXX',  
'secret' => 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',  
'cookie' => true,));
 
// Uncomment to debug post fields
// print_r($_REQUEST);
 
// Setting canvas to 1 and fbconnect to 0 fixes issues with being redirected back to your base domain instead of back to the canvas app on auth
$loginUrl = $facebook->getLoginUrl($params = array('canvas' => 1, 'fbconnect' => 0, 'req_perms' => 'insert required perms here'));

$session = $facebook->getSession();
$me = null;
 
if ($session) {
	try {
		$me = $facebook->api('/me');
		//If user has authed application, render a logout link.
		//echo '<a href="'.$facebook->getLogoutUrl().'">Logout</a>';
	} catch(FacebookApiException $e){
		//Uncomment the line below to have a user click to authorize the app instead of requesting permissions on page load.
		//echo '<a href="'.$loginUrl.'">Login</a>';
		//Request permissions on page load if app is not authed & cannot request user information.
		echo "<script type='text/javascript'>top.location.href = '".$loginUrl."';</script>";
		error_log($e);
		}
	} else {
	//Request permissions on page load if app is not authed & or session is invalid.
	echo "<script type='text/javascript'>top.location.href = '".$loginUrl."';</script>";
}

// If you can hit user on the Graph API, do some stuff
if($me) {
	
}


 
?>

Revision: 33556
at October 14, 2010 09:35 by chrislorenz


Updated Code
<?php
//This is my skeleton template for a PHP SDK canvas app, use it if you find it useful.
//Lately I use iframe canvas apps so I used a javascript page redirect instead of fbjs ajax with require_login();
 
//Don't forget to change path to PHP SDK if necessary
require_once 'facebook.php';
// Create our Application instance. Don't forget to change to your AppId and Secret
 
$facebook = new Facebook(array(  
'appId'  => 'XXXXXXXXXXXXX',  
'secret' => 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',  
'cookie' => true,));
 
// Uncomment to debug post fields// print_r($_REQUEST);
 
// Setting canvas to 1 and fbconnect to 0 fixes issues with being redirected back to your base domain instead of back to the canvas app on auth
$loginUrl = $facebook->getLoginUrl($params = array('canvas' => 1, 'fbconnect' => 0, 'req_perms' => 'insert required perms here'));

$session = $facebook->getSession();
$me = null;
 
if ($session) {
	try {
		$me = $facebook->api('/me');
		//If user has authed application, render a logout link.
		//echo '<a href="'.$facebook->getLogoutUrl().'">Logout</a>';
	} catch(FacebookApiException $e){
		//Uncomment the line below to have a user click to authorize the app instead of requesting permissions on page load.
		//echo '<a href="'.$loginUrl.'">Login</a>';
		//Request permissions on page load if app is not authed & cannot request user information.
		echo "<script type='text/javascript'>top.location.href = '".$loginUrl."';</script>";
		error_log($e);
		}
	} else {
	//Request permissions on page load if app is not authed & or session is invalid.
	echo "<script type='text/javascript'>top.location.href = '".$loginUrl."';</script>";
}

// If you can hit user on the Graph API, do some stuff
if($me) {
	
}


 
?>

Revision: 33555
at October 10, 2010 09:34 by chrislorenz


Initial Code
<?php
//This is my skeleton template for a PHP SDK canvas app, use it if you find it useful.
//Lately I use iframe canvas apps so I used a javascript page redirect instead of fbjs ajax with require_login();
 
//Don't forget to change path to PHP SDK if necessary
require_once 'facebook.php'; 
// Create our Application instance. Don't forget to change to your AppId and Secret
 
$facebook = new Facebook(array(  
'appId'  => 'XXXXXXXXXXXXX',  
'secret' => 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',  
'cookie' => true,));
 
// Uncomment to debug post fields// print_r($_REQUEST);
 
$loginUrl = $facebook->getLoginUrl(array(    'req_perms' => '*Perms Here*'));
$session = $facebook->getSession();
$me = null;
 
if ($session) {
	try {
		$me = $facebook->api('/me');
		//If user has authed application, render a logout link.
		//echo '<a href="'.$facebook->getLogoutUrl().'">Logout</a>';
		print_r($me);
	} catch(FacebookApiException $e){
		//Uncomment the line below to have a user click to authorize the app instead of requesting permissions on page load.
		//echo '<a href="'.$loginUrl.'">Login</a>';
		//Request permissions on page load if app is not authed & cannot request user information.
		echo "<script type='text/javascript'>top.location.href = '".$loginUrl."';</script>";
		error_log($e);
		}
	} else {
	//Request permissions on page load if app is not authed & or session is invalid.
	echo "<script type='text/javascript'>top.location.href = '".$loginUrl."';</script>";
	exit;
}
 
?>

Initial URL


Initial Description
This is just a quick and dirty template that I use to begin writing my apps. If you know of a better way to approach various portions of this code I would love to hear it. It\'s assuming you are using the latest PHP SDK and is positioned around Iframe based application (though it works with FBML, I would just use AJAX calls to request auth as it\\\\\\\'s a little more seamless).

Initial Title
Facebook PHP SDK Starter Code

Initial Tags
php, facebook

Initial Language
PHP