Today's Question:  What are you most afraid of as a programmer?        GIVE A SHOUT

Technical Article => Web =>  PHP

Automatically post to Facebook from PHP script

  tips4php.net      2012-02-27 09:03:00      16,466    0    6

Facebook is currently on of the most important publishing and traffic generating sources for many websites. Manually Cross publishing content on your own site and Facebook seems like a lot of extra work.

This post guides you through the creation of a Facebook application that can automatically post messages and other types of content on your Facebook wall.

Getting started

Building a php script that automatically posts status updates on your wall requires the following steps:

  1. Download Facebook API
  2. Register a Facebook app
  3. Give the app rights to post on your wall
  4. Build the post to Facebook script

Get the Facebook API

First step is to download a general Facebook communication class from Glithub.

Download the files and upload the files to your site – e.g. to a folder called fb.

The file containing the Facebook class is “facebook.php”

Register a facebook app

Facebook only allows registrated apps to interact with Facebook accounts and pages. The next step is therefore to register your app.

You can register a new app on this page

To register a Facebook App you need to provide: Site name, Site url (with ending “/”) and the locality of your application.

Facebook appliction creation

If everything is OK, you get to a registration conformation screen, where you get the two important informations: App ID and App Secret that you need in the next steps.

Application registration confirmation facebook

Give the application the rights to access your wall

Next step is to build a small script that only has one purpose, identify itself with the new App ID and App Secret to your Facebook account, so you can allow it to directly communicate with your Facebook page.

If you skip this step, you need to manually allow the script to access your Facebook account each time it’s trying to post something, which isn’t that smart…

Simply insert your App ID and App Secret in this script below and  save the script as “fb_access.php” or something similar.

01<?
02require_once 'facebook.php';
03 
04$app_id = "<insert App ID";
05$app_secret = "<insert App Secret>";
06 
07$facebook = new Facebook(array(
08 'appId' => $app_id,
09 'secret' => $app_secret,
10 'cookie' => true
11));
12 
13if(is_null($facebook->getUser()))
14{
15 header("Location:{$facebook->getLoginUrl(array('req_perms' => 'user_status,publish_stream,user_photos'))}");
16 exit;
17}
18?>

Next step is to upload the script to the same directory on your server where you uploaded the “facebook.php” class. In this example we’ll call the script “fb_access.php”

Finally you need to upload the script to your server, and access the script from your web browser: http://<yoursite>/fb/fb_access.php.

Give facebook application access to account

Click the allow button, and you’re ready to build a script that posts to Facebook.

Depending on your Facebook configuration you might get an error message when accessing fb_access.php in your browser.

Facebook error

This typically means that you need to provide a url to the website connected in the application. Simply click the link “application settings editor” and provide: Site URL in the “Web site” tab, and try to load the fb_access.php file again in your browser.

Build the Post to Facebook script

Now that your application has access to your Facebook account, and we have the general facebook.php class, it’s very easy to build a small script that can post messages to your Facebook wall.

1<?
2require_once 'fb_access.php';
3 
4// add a status message
5$status = $facebook->api('/me/feed', 'POST', array('message' => 'this is a test...'));
6var_dump($status);
7?>

And that’s it. Try to access the script from your browser and check if a post is added to your Facebook wall.

Please notice that if you controls multiple Facebook pages, this script will post to the wall of your personal profile. If you want to post to a specific page, you need to replace “/me/” with the ID of the specific page.

Another important information is, that  you will get an error message if you try to post the same message twice. So remember to change the message, when you’re testing the script.

Next steps

This post guides you through the initial Facebook application registration, and basic script for automatically posting content to a Facebook wall.

Next step could be to integrate this script in your CMS system, so a new post is automatically added to Facebook every time you’re updating the content on your site.

This script is very basic and only adds a status message in plain text. However you can naturally also post images, links, and other informations. You can see more about the additional informations you can include here.

sOURCE:http://tips4php.net/2010/12/automatic-post-to-facebook-from-php-script/

PHP FACEBOOK API OAUTH AUTO POST

  SAVE AS PDF   MARK AS READ   MARK AS IMPORTANT

Share on Facebook  Share on Twitter  Share on Google+  Share on Weibo  Share on Reddit  Share on Digg  Share on Tumblr    Delicious

  RELATED


  0 COMMENT


No comment for this article.


  WRITE ARTICLE

Where is my bike?

By sonic0002