Advertisement
Guest User

fb post example

a guest
Mar 15th, 2013
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.81 KB | None | 0 0
  1. require_once('lib/fb/facebook.php');
  2.  
  3.   $config = array(
  4.     'appId' => 'XXX',
  5.     'secret' => 'XXX',
  6.   );
  7.  
  8.   $facebook = new Facebook($config);
  9.   $user_id=$facebook->getUser();
  10.  
  11.     if($user_id) {
  12.  
  13.       // We have a user ID, so probably a logged in user.
  14.       // If not, we'll get an exception, which we handle below.
  15.       try {
  16.         $ret_obj = $facebook->api('/me/feed', 'POST',
  17.                                     array(
  18.                                       'link' => $link,
  19.                                       'message' => $title
  20.                                  ));
  21.         $output.= 'Article successfully submitted to Facebook!<br/> Post ID: ' . $ret_obj['id'];
  22.  
  23.       } catch(FacebookApiException $e) {
  24.         // If the user is logged out, you can have a
  25.         // user ID even though the access token is invalid.
  26.         // In this case, we'll get an exception, so we'll
  27.         // just ask the user to login again here.
  28.         $login_url = $facebook->getLoginUrl( array(
  29.                        'scope' => 'publish_stream'
  30.                        ));
  31.         $output.= 'Please <a href="' . $login_url . '">login.</a>';
  32.         error_log($e->getType());
  33.         error_log($e->getMessage());
  34.       }  
  35.       // Give the user a logout link
  36.       //echo '<br /><a href="' . $facebook->getLogoutUrl() . '">logout</a>';
  37.     } else {
  38.  
  39.       // No user, so print a link for the user to login
  40.       // To post to a user's wall, we need publish_stream permission
  41.       // We'll use the current URL as the redirect_uri, so we don't
  42.       // need to specify it here.
  43.       $login_url = $facebook->getLoginUrl( array( 'scope' => 'publish_stream' ) );
  44.       $output.= 'Please <a href="' . $login_url . '">login.</a>';
  45.  
  46.     }
  47.    
  48.    
  49. }
  50. else {$output.= "An error has occurred";}
  51.  
  52. echo $output;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement