Post to Facebook Wall/Timeline with the Facebook PHP SDK

  • strict warning: Non-static method view::load() should not be called statically in /home1/nate01/public_html/sites/all/modules/views/views.module on line 1113.
  • strict warning: Declaration of views_handler_field::query() should be compatible with views_handler::query($group_by = false) in /home1/nate01/public_html/sites/all/modules/views/handlers/views_handler_field.inc on line 1147.
  • strict warning: Declaration of views_handler_argument::options_validate() should be compatible with views_handler::options_validate($form, &$form_state) in /home1/nate01/public_html/sites/all/modules/views/handlers/views_handler_argument.inc on line 917.
  • strict warning: Declaration of views_handler_argument::query() should be compatible with views_handler::query($group_by = false) in /home1/nate01/public_html/sites/all/modules/views/handlers/views_handler_argument.inc on line 917.
  • strict warning: Declaration of views_handler_sort::options_validate() should be compatible with views_handler::options_validate($form, &$form_state) in /home1/nate01/public_html/sites/all/modules/views/handlers/views_handler_sort.inc on line 165.
  • strict warning: Declaration of views_handler_sort::options_submit() should be compatible with views_handler::options_submit($form, &$form_state) in /home1/nate01/public_html/sites/all/modules/views/handlers/views_handler_sort.inc on line 165.
  • strict warning: Declaration of views_handler_sort::query() should be compatible with views_handler::query($group_by = false) in /home1/nate01/public_html/sites/all/modules/views/handlers/views_handler_sort.inc on line 165.
  • strict warning: Declaration of views_handler_filter::options_validate() should be compatible with views_handler::options_validate($form, &$form_state) in /home1/nate01/public_html/sites/all/modules/views/handlers/views_handler_filter.inc on line 587.
  • strict warning: Declaration of views_handler_filter::query() should be compatible with views_handler::query($group_by = false) in /home1/nate01/public_html/sites/all/modules/views/handlers/views_handler_filter.inc on line 587.
  • strict warning: Declaration of views_plugin_query::options_submit() should be compatible with views_plugin::options_submit($form, &$form_state) in /home1/nate01/public_html/sites/all/modules/views/plugins/views_plugin_query.inc on line 169.
  • strict warning: Declaration of views_plugin_row::options_validate() should be compatible with views_plugin::options_validate(&$form, &$form_state) in /home1/nate01/public_html/sites/all/modules/views/plugins/views_plugin_row.inc on line 136.
  • strict warning: Non-static method view::load() should not be called statically in /home1/nate01/public_html/sites/all/modules/views/views.module on line 1113.
  • strict warning: Declaration of views_plugin_pager_none::post_execute() should be compatible with views_plugin_pager::post_execute(&$result) in /home1/nate01/public_html/sites/all/modules/views/plugins/views_plugin_pager_none.inc on line 69.
<?php
 
 require_once '../facebook/facebook_sdk/src/facebook.php';
 
// configuration
 $appid = [APP ID];
 $appsecret = [SECRET APP KEY];
 $msg = 'Some message would go here.';
 $title = 'Some title would go here';
 $uri = 'http://nathanbolin.com';
 $desc = 'Some detailed message that shows beneath the title. You can include a decent amount of text here.';
 $pic = 'http://www.nathanbolin.com/images/fb_icon.png';
 $action_name = 'Action Link in the Footer of the Post';
 $action_link = 'http://www.nathanbolin.com';
 
$facebook = new Facebook(array(
 'appId' => $appid,
 'secret' => $appsecret,
 'cookie' => false,
 ));
 
 
$user = $facebook->getUser();
 
// Contact Facebook and get token
 if ($user) {
	 // you're logged in, and we'll get user acces token for posting on the wall
	 try {
	 	$accessToken = $facebook->getAccessToken();
		 if (!empty( $accessToken )) {
				 $attachment = array(
					 'access_token' => $accessToken,
					 'message' => $msg,
					 'name' => $title,
					 'link' => $uri,
					 'description' => $desc,
					 'picture'=>$pic,
					 'actions' => json_encode(array('name' => $action_name,'link' => $action_link))
			 	  );
 
				$status = $facebook->api("/me/feed", "post", $attachment);
	 	 } else {
	 			$status = 'No access token recieved';
	 	 }
 	 } catch (FacebookApiException $e) {
 				error_log($e);
 				$user = null;
	 }
 } else {
	 // you're not logged in, the application will try to log in to get a access token
	 header("Location:{$facebook->getLoginUrl(array('scope' => 'photo_upload,user_status,publish_stream,user_photos,manage_pages'))}");
 }
 
print_r( $status );
 
 
 
?>

First step is to create a folder on your webserver for the Facebook application. For this example, name the folder "facebook"

In the facebook folder create a subfolder named "facebook_sdk"

Download the Facebook PHP SDK from GitHub, unzip the files and upload the files to the "facebook_sdk" folder on your server.

The Facebook SDK files can be downloaded from here


The script is divided into three parts:

Configuration where you insert your Facebook SDK information, and configeres which content to post on Facebook

Facebook authentication and posting if a valid access token is avalilable

Facebook authentication token download if no valid token is available (and then the script proceeds to step 2)