SearchRecent postsThe Random ToolboxDrupal 7 Setting up variables for use in a template (preprocess and process functions) Rendering Drupal 7 fields (the right way) Linux Secure Copy (scp) command Restrict Free Shipping in Magento Quickbooks Text Connection Ticket Cross-Site Request Forgery (CSRF) jQuery Events: MouseOver / MouseOut vs. MouseEnter / MouseLeave Monthly archive
|
Post to Facebook Wall/Timeline with the Facebook PHP SDK<?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) |