Friday, 14 March 2008

Facebook application: Rss notifier


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



feed story.

/*

session_start();

if(isset($_SESSION['views']))

  $_SESSION['views']=$_SESSION['views']+1;

else

  $_SESSION['views']=1;

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

//*/

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

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

}

else{

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

}



?>





<?php

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>";

?>



<?php

// Parse the Rss feed.

$doc = new DOMDocument();

$doc->load



('http://groups.google.co.uk/group/londonppl/feed/atom_v1_0_ms



gs.xml');

//$doc->load



('http://feeds.feedburner.com/YqlmLondonGoogleGroup');

$arr = array();

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

$itemRSS = array (

'author' => $node->getElementsByTagName



('name')->item(0)->nodeValue,

'email' => $node->getElementsByTagName



('email')->item(0)->nodeValue,

'date' => $node->getElementsByTagName



('updated')->item(0)->nodeValue,

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



->item(0)->getAttribute("href"),

'title' => $node->getElementsByTagName



('title')->item(0)->nodeValue,

'desc' => $node->getElementsByTagName



('summary')->item(0)->nodeValue);

array_push($arr, $itemRSS);

}

?>





<?php

// Show Rss contents in facebook canvas.

$dashbutton = <<<EndHereDoc

<fb:dashboard> <fb:create-button



href="http://www.facebook.com/apps/application.php?



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



</fb:dashboard>

EndHereDoc;

echo $dashbutton;



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

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

$item = $arr[$i];

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



['author'];

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



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



['title'].'</a><br/><li>'.$item['desc'].'</li>';

echo $content;

}



?>





<?php

// Generate short contents in users' profile file.

$profilecontent = '<a



href="http://apps.facebook.com/googlegroupsnotifier">YQLM



Group</a><br/>';

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

$pitem = $arr[$i];

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



['email']:$pitem['author'];

$profilecontent =



$profilecontent.'==================='.'<br/><b>'.$author.'</b>



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



['title'].'</i><br/><li>'.$pitem['desc'].'</li><br/>';

}

?>









<?php

///*

// This is for fbml_setRefHandle

$fbml = <<<EndHereDoc

<fb:wide>

$profilecontent

<fb:editor



action="http://apps.facebook.com/googlegroupsnotifier">

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

</fb:editor>

</fb:wide>

<fb:narrow>

$profilecontent

<fb:editor



action="http://apps.facebook.com/googlegroupsnotifier">

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

</fb:editor>

</fb:narrow>

EndHereDoc;



$facebook->api_client->fbml_setRefHandle



("googlegroupsnotifier",$fbml);

// */





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

$facebook->api_client->profile_setFBML



($refinprofile,$user_id);



?>





<?php



// 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



['email']:$fitem['author'];

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



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



'.'<i>'.$fitem['title'].'</i>';



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



//print_r($_COOKIE);

//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>";

try{

$facebook->api_client-



>feed_publishTemplatizedAction



($title_template,$title_data,$body_template,$body_data,$body_g



eneral);

}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);

}

}

else

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



next 1 hour.</i>";

?>








3 comments:

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

Note:
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
twitter.com/zhengxin

Facebook & Twitter