Facebook Post to Wall

Post to Facebook user's wall is easy! First you need to create a new app on Facebook. If you do not now how to do it, read this.

Facebook Post to Wall script

This script posts a link to user's wall.
Replace YOUR_APP_ID and YOUR_APP_SECRET to your App ID and App Secret  in the following code and save it as index.php.

<?php
  // Remember to copy files from the SDK's src/ directory to a
  // directory in your application on the server, such as src/
  require_once('src/facebook.php');

  $config = array(
    'appId' => 'YOUR_APP_ID',
    'secret' => 'YOUR_APP_SECRET',
  );

  $facebook = new Facebook($config);
  $user_id = $facebook->getUser();
?>

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Facebook Post to Wall</title>
</head>
<body>


<?php
if($user_id) {

	// We have a user ID, so probably a logged in user.
	// If not, we'll get an exception, which we handle below.
	try {
		$ret_obj = $facebook->api('/me/feed', 'POST',
									array(
									'link' => 'www.1stwebmagazine.com/facebook-post-to-wall',
									'message' => 'Posting with the PHP SDK!'
								));
		echo '<pre>Post ID: ' . $ret_obj['id'] . '</pre>';

		// Give the user a logout link 
		echo '<br /><a href="' . $facebook->getLogoutUrl() . '">logout from Facebook.</a>';
	} catch(FacebookApiException $e) {
        // If the user is logged out, you can have a 
        // user ID even though the access token is invalid.
        // In this case, we'll get an exception, so we'll
        // just ask the user to login again here.
        $login_url = $facebook->getLoginUrl( array(
				'scope' => 'publish_stream'
				)); 
        echo 'Please <a href="' . $login_url . '">login.</a>';
        error_log($e->getType());
        error_log($e->getMessage());
	}   
} else {

    // No user, so print a link for the user to login
    // To post to a user's wall, we need publish_stream permission
    // We'll use the current URL as the redirect_uri, so we don't
    // need to specify it here.
    $login_url = $facebook->getLoginUrl( array( 'scope' => 'publish_stream' ) );
    echo 'Please <a href="' . $login_url . '">login.</a>';
} 

?>  
  
</body>
</html>

Copy index.php and the src directory from the PHP SDK to the that folder on your website what you entered earlier for your app.

 

Demo »


Go Daddy


Related articles

Email Validation

Email Validation

08/02/2013

Learn how to validate email on client side with HTML5, Javascript and on server side with PHP.

Generate Thumbnail on the Fly with PHP

Generate Thumbnail on the Fly with PHP

04/02/2013

In a previous artice I already showed you how to generate square thumbnails with PHP. Now I show you how to keep the original ratio of the image and how to control the alignment of the square image.

Facebook Upload Photo to User′s Profile

Facebook Upload Photo to User′s Profile

31/01/2013

Learn how to upload a photo to Facebook user′s profile with PHP SDK via Graph API!


Leave a comment

We welcome any comment from you! Please keep in mind that comments are moderated and rel='nofollow' is in use. So, please do not use a domain as your name or a spammy keyword, or your comment will be deleted.

Name: *

E-mail: *

Message: *

Click on the apple *


Find us on Facebook

Tags

Get online with the world's Best - GoDaddy.com!

Back to Top