1:the code to greet the user, ask for his permission and store his session data so that we can use a cronjob with his session data afterwards.
<?php $db_server = "localhost";
$db_username = "username";
$db_password = "password";
$db_name = "databasename";
#go to line 85, the script actually starts there
mysql_connect($db_server,$db_username,$db_password);
mysql_select_db($db_name);
#you have to create a database to store session values.
#if you do not know what columns there should be look at line 76 to see column names.
#make them all varchars
# Now lets load the FB GRAPH API
require './facebook.php';
// Create our Application instance.
global $facebook;
$facebook = new Facebook(array( 'appId' => '121036530138',
'secret' => '9bbec378147064',
'cookie' => false,));
# Lets set up the permissions we need and set the login url in case we need it.
$par['req_perms'] = "friends_about_me,friends_education_history,friends_likes, friends_interests,friends_location,friends_religion_politics, friends_work_history,publish_stream,friends_activities, friends_events, friends_hometown,friends_location ,user_interests,user_likes,user_events, user_about_me,user_status,user_work_history,read_requests, read_stream,offline_access,user_religion_politics,email,user_groups";
$loginUrl = $facebook->getLoginUrl($par);
function save_session($session){
global $facebook;
# OK lets go to the database and see if we have a session stored
$sid=mysql_query("Select access_token from facebook_user WHERE uid =".$session['uid']); $session_id=mysql_fetch_row($sid);
if (is_array($session_id)) {
# We have a stored session, but is it valid?
echo "
We have a session, but is it valid?";
try {
$attachment = array('access_token' => $session_id[0]);
$ret_code=$facebook->api('/me', 'GET', $attachment);
}
catch (Exception $e) {
# We don't have a good session so
echo "
our old session is not valid, let's delete saved invalid session data
";
$res = mysql_query("delete from facebook_user WHERE uid =".$session['uid']);
#save new good session
#to see what is our session data: print_r($session);
if (is_array($session)) {
$sql="insert into facebook_user (session_key,uid,expires,secret,access_token,sig)
VALUES ('".$session['session_key']."','".$session['uid']."','". $session['expires']."','".
$session['secret'] ."','" . $session['access_token']."','". $session['sig']."');";
$res = mysql_query($sql);
return $session['access_token'];
}
# this should never ever happen
echo "
Something is terribly wrong: Our old session was bad,
and now we cannot get the new session";
return;
}
echo "
Our old stored session is valid
";
return $session_id[0];
}
else {
echo "
no stored session, this means the user never subscribed to our application before.
";
# let's store the session
$session = $facebook->getSession();
if (is_array($session)) {
# Yes we have a session! so lets store it!
$sql="insert into facebook_user (session_key,uid,expires,secret,access_token,sig)
VALUES ('".$session['session_key']."','".$session['uid']."','". $session['expires']."','". $session['secret'] ."','".
$session['access_token']."','". $session['sig']."');";
$res = mysql_query($sql);
return $session['access_token'];
}
}
}
#this is the first meaningful line of this script.
$session = $facebook->getSession();
# Is the user already subscribed to our application?
if ( is_null($session) ) {
# no he is not
#send him to permissions page
header( "Location: $loginUrl" );
}
else {
#yes, he is already subscribed, or subscribed just now
#in case he just subscribed now, save his session information
$access_token=save_session($session);
echo "
everything is ok";
# write your code here to do something afterwards
}
?>
error
Warning: session_start() [function.session-start]: Cannot send session
cache limiter - headers already sent (output started at
/home/content/28/9687528/html/ss/src/indexx.php:1) in
/home/content/28/9687528/html/ss/src/facebook.php on line 49
Fatal error: Call to undefined method Facebook::getSession() in
/home/content/28/9687528/html/ss/src/indexx.php on line 86
2:A cronjob template that reads the stored session of a user from database, uses his session data to work on his behalf, like reading status posts or publishing posts etc.
<?php
$db_server = "localhost";
$db_username = "username";
$db_password = "pass";
$db_name = "database";
# Lets connect to the Database and set up the table
$link = mysql_connect($db_server,$db_username,$db_password);
mysql_select_db($db_name);
# Now lets load the FB GRAPH API
require './facebook.php';
// Create our Application instance.
global $facebook;
$facebook = new Facebook(array(
'appId' => 'appid',
'secret' => 'secret',
'cookie' => false,
));
function get_check_session($uidCheck){
global $facebook;
# This function basically checks for a stored session and if we have one it returns it
# OK lets go to the database and see if we have a session stored
$sid=mysql_query("Select access_token from facebook_user WHERE uid =".$uidCheck);
$session_id=mysql_fetch_row($sid);
if (is_array($session_id)) {
# We have a session
# but, is it valid?
try {
$attachment = array('access_token' => $session_id[0],);
$ret_code=$facebook->api('/me', 'GET', $attachment);
}
catch (Exception $e) {
# We don't have a good session so
echo "
User ".$uidCheck." removed the application, or there is some other access problem.
";
# let's delete stored data
$res = mysql_query("delete from facebook_user where WHERE uid =".$uidCheck);
return;
}
return $session_id[0];
}
else
{ # "no stored session";
echo "
error:newsFeedcrontab.php No stored sessions. This should not have happened
";
}
}
# get all users that have given us offline access
$users = getUsers();
foreach($users as $user){
# now for each user, check if they are still subscribed to our application
echo "
Checking user".$user;
$access_token=get_check_session($user);
# If we've not got an access_token we actually need to login.
# but in the crontab, we just log the error, there is no way we can find the user to give us permission here.
if ( is_null($access_token) ) {
echo "
error: newsFeedcrontab.php There is no access token for the user ".$user."
";
}
else {
#we are going to read the newsfeed of user. There are user's friends' posts in this newsfeed
try{
$attachment = array('access_token' => $access_token);
$result=$facebook->api('/me/home', 'GET', $attachment);
}catch(Exception $e){
echo "
error: newsfeedcrontab.php, cannot get feed of ".$user.$e;
}
#do something with the result here
#but what does the result look like?
#go to http://developers.facebook.com/docs/reference/api/user/ and click on the "home" link under connections
#we can also read the home of user. Home is the wall of the user who has given us offline access.
try{
$attachment = array('access_token' => $access_token);
$result=$facebook->api('/me/feed', 'GET', $attachment);
}catch(Exception $e){
echo "
error: newsfeedcrontab.php, cannot get wall of ".$user.$e;
}
#do something with the result here
# #but what does the result look like?
#go to http://developers.facebook.com/docs/reference/api/user/ and click on the "feed" link under connections
}
}
function getUsers(){
$sql = "SELECT distinct(uid) from facebook_user Where 1";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)){
$rows [] = $row['uid'];
}
print_r($rows);
return $rows;
}
mysql_close($link);
?>
error
Warning: session_start() [function.session-start]: Cannot send session
cache limiter - headers already sent (output started at
/home/content/28/9687528/html/ss/src/cron.php:1) in
/home/content/28/9687528/html/ss/src/facebook.php on line 49
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL
result resource in /home/content/28/9687528/html/ss/src/cron.php on
line 110
Warning: Invalid argument supplied for foreach() in
/home/content/28/9687528/html/ss/src/cron.php on line 64