Friday, 14 March 2008

Facebook application: Rss notifier

<?php //create 1 hour life cookie to prevent duplicate news

feed story.







echo "Total Visits: ". $_SESSION['views'];


////////// session or cookie /////////////////

if (isset($_COOKIE["updatetime"])){



setcookie("updatetime", time(), time()+3600);




require_once 'facebook.php';

$appapikey = '3d3fabe3276d1dd50ddf78353c719af4';

$appsecret = 'fe4754d97d24392ba9e40105f0ea0a08';

$facebook = new Facebook($appapikey, $appsecret);

$user_id = $facebook->require_login();

// Greet the currently logged-in user!

echo "<p>Hello, <fb:name uid=\"$user_id\" useyou=\"false\"

/>!<br/>"." There are new messages on the YQLM group.</p>";



// Parse the Rss feed.

$doc = new DOMDocument();






$arr = array();

foreach ($doc->getElementsByTagName('entry') as $node) {

$itemRSS = array (

'author' => $node->getElementsByTagName


'email' => $node->getElementsByTagName


'date' => $node->getElementsByTagName


'link' => $node->getElementsByTagName('link')


'title' => $node->getElementsByTagName


'desc' => $node->getElementsByTagName


array_push($arr, $itemRSS);




// Show Rss contents in facebook canvas.

$dashbutton = <<<EndHereDoc

<fb:dashboard> <fb:create-button


id=11393681690">Add/Remove this application</fb:create-button>



echo $dashbutton;

for ($i=0;$i<10;$i++){

echo '<br/>'.'==================='.'<br/>';

$item = $arr[$i];

$author = ($item['author']=="")?$item['email']:$item


$content = '<b>'.$author.'</b><i> said on </i>'.$item

['date'].'<br/>'.'<a href="'.$item['link'].'">'.$item


echo $content;




// Generate short contents in users' profile file.

$profilecontent = '<a



for ($i=0;$i<2;$i++){

$pitem = $arr[$i];

$author = ($pitem['author']=="")?$pitem


$profilecontent =


<i> said on </i>'.$pitem['date'].'<br/><i>'.$pitem






// This is for fbml_setRefHandle

$fbml = <<<EndHereDoc





<fb:editor-button value="More messages"/>







<fb:editor-button value="More messages"/>






// */

$refinprofile = '<fb:ref handle="googlegroupsnotifier" />';





// This is for news feed/mini feed.

$title_template = "{actor} viewed the group";

$title_data = null;

$body_template = null;

$body_data = null;

$fitem = $arr[0];

$author = ($fitem['author']=="")?$fitem


$feedcontent = '<br/>'.$author.' said

<br/><b>'.$fitem['desc'].'</b><br/> in the topic of


$body_general = 'The latest post : <br/>'.$feedcontent;


//echo time()-$_COOKIE["updatetime"];

if (time()-$_COOKIE["updatetime"]>3600){

echo "<br/><i>A new news feed will be published in the

next 1 hour.</i>";






}catch(Exception $e) {

//this will clear cookies for your app and

redirect them to a login prompt

echo "<br/><br/><i>"."Update the news feed too

many times, no news will appear today."."</i>";

$facebook->set_user(null, null);




echo "<br/><i>No news feed will be published in the

next 1 hour.</i>";



Cross Z said...
This comment has been removed by the author.
Cross Z said...

setcookie() will not make the cookie available for current page, so use isset() to judge whether setcookie() correct will not work in same page loading.

Following are from php official documents:
setcookie() defines a cookie to be sent along with the rest of the HTTP headers. Like other headers, cookies must be sent before any output from your script (this is a protocol restriction). This requires that you place calls to this function prior to any output, including <html> and <head> tags as well as any whitespace.

Once the cookies have been set, they can be accessed on the next page load with the $_COOKIE or $HTTP_COOKIE_VARS arrays.

Return Values

If output exists prior to calling this function, setcookie() will fail and return FALSE. If setcookie() successfully runs, it will return TRUE. This does not indicate whether the user accepted the cookie.

Cross Z said...

The above PROTOCOL RESTRICTION is applied on the linux server. While if the server is built on Windows, it doesn't matter where the secookie() and session_start() appear in the entire code page.

My photo
London, United Kingdom

Facebook & Twitter