Facebook Template for PHPSDK Version 3

This this an update of my previous basic template, found here. It incorporates the new PHPSDK Ver 3. Note that when I include facebook.php, it is in a specific directory. Since different versions of the SDK will not necessarily work the same way with your apps, I am putting different versions in their own folder. That way, when a new SDK comes out, it can have its own folder. So existing applications will still work, because they will be using the older version.

I am documenting here a very basic template for facebook PHP applications. Its sole purpose is to manage the following:

Whether a user likes your application.
Whether a user likes your fanpage.

It will also provide links to your application profile page and fanpage.

Its primary use is to provide a todo list for users. Users who are not yet fans can be gently nudged in the right direction etc.

NB: you will have to do the nudging. All this framework does is provide the barest of bones as to what variable is being set where.

I have split the application into two programs:

index.php
app.php

app.php is the php application you actually want to run.
index.php loads the application assuming the user is logged on, and populates a few variables:

Your facebook : $fa_fbme
Your facebook name: $fa_fbname
Your facebook id: $fa_fbid
Flag for if you like the app: $fa_appliked
Flag for if you like a fanpage: $fa_fanpageliked

You will have to stipulate in index.php the app secret, fanpage id and url etc.

So, lets get the code going. This is index.php:

<?php


// This should point to wherever you have your php facebook api.
require_once ("../src/3.01/facebook.php");                                       
// id of any associated fan page.  
$cfanpageid = "XXXXXXXXXXX";
// URL of the above
$cfanpage = "http://www.facebook.com/pages/Face-Answers/142393235804950"; 
// Application ID
$cappid = "XXXXXXXXXXXX";
// Application Secret
$cappsecret = "XXXXXXXXXXXXXXXXXXXXXXXXXXX";
$facebook = new Facebook(array(
'appId' => $cappid, 
'secret' => $cappsecret,
'cookie' => true, 
'domain' => 'facebookanswers.co.uk'
));

$user = $facebook->getUser();

if (!$user) {
    $url = $facebook->getLoginUrl(array('scope'=> 'publish_stream,status_update','redirect_uri'=>'http://apps.facebook.com/template.3/'));
    echo "<p>Redirecting to permission request...</p>";
    echo "<script type=\"text/javascript\">top.location.href = '$url';</script>";
} else {

    try {
        $likes = $facebook->api('me/likes');
        $fa_fbme = $facebook->api('me');
        $fa_fbname = $fa_fbme['name'];
        $fa_fbid = $fa_fbme['id'];
        $fa_fanpageliked = false;
        $fa_appliked = false;

        foreach ($likes['data'] as $ilike) {
            if ($ilike['id'] == $cfanpageid) {$fa_fanpageliked = true;}
            if ($ilike['id'] == $cappid) {$fa_appliked = true; }
        }

require_once('app.php');

    }
    catch (FacebookApiException $e) {
        $url = $facebook->getLoginUrl(array('scope'=> 'publish_stream,status_update','redirect_uri'=>'http://apps.facebook.com/template.3/'));
        echo "Please wait for authorization…";
        echo "<script type=\"text/javascript\">top.location.href = '$url';</script>";
    }


}

Note, in the loginurl we have: (‘scope’=> ‘publish_stream,status_update’,'redirect_uri’=>’http://apps.facebook.com/icyouriq/’).
What you are doing is specifying the page that will get loaded once you have given permission.

And here is the app.php code.

<?php

echo "<h1>Facebook Answers Template for PHPSDK 3</h1>";
echo "<p>If you are reading this then you must be doing something right!</p><hr />";
echo "<p>Your name is: $fa_fbname</p>";
echo "<p>Your id is: $fa_fbid</p>";
echo "<p>Your Like App status is: ".($fa_appliked==true?"Yes":"No")."</p>";
if (!$fa_appliked)
{
    echo "<p><a href='http://www.facebook.com/apps/application.php?id=".$cappid." target='_top'>Go to App Profile Page</a></p>";

}
echo "<p>Your Like Fan Page status is: ".($fa_fanpageliked==true?"Yes":"No")."</p>";
if (!$fa_fanpageliked)
{
    echo "<p><a href='".$cfanpage."' target='_top'>Be a fan of Fan Page</a></p>";
}

echo "<hr />";

echo "<p><a href='' onclick='top.location.href=\"http://www.facebook.com/apps/application.php?id=".$cappid."\";return false;'>Go to App Profile Page</a></p>";
echo "<p><a href='' onclick='top.location.href=\"$cfanpage\";return false;'>Go to Fan Page</a></p>";


The useful thing to note there is the target=’_top’ in your anchor links. If you don’t use javascript, but have ordinary URLS pointing to your fan page, then Facebook will insert an ugly dialog informing the user that they are moving to another area of facebook. I used to use an onclick method here, to get round that, however, that would lead to a blank href, which is a not recommended. If you use the target, you don’t need the onclick method.

Anyway, thats the app. You can see it in action here:

Template PHPSDK V3 App

You can skip to the end and leave a response. Pinging is currently not allowed.

Leave a Reply

Opposite of up:

Powered by WordPress | Cheap at&t cell phones for sale | Thanks to Sprint online specials, Video Game Music and Car Insurance