Auto post to facebook

PHP 10 responses so far

Social websites like facebook plays a vital role in increasing traffic for any website.So posting events of your website like new registration,new scheme etc on your facebook fan page will let others know about it and eventually increase traffic and income.

The Facebook Graph Api allows websites to interact with Facebook users. In order to post status updates, we first need to create a facebook app.This is how an app is creating on facebook.

Go to the Developers area of Facebook and click Apps->Create New App.In the popup enter app name, click continue and submit after filling captcha.Your new app is created and you will be directed to the new app page.Please note down App id and App secret,shown at the top of the page.It will be required a little later for integration.Fill up the details and click “App on Facebook”  tab and in the Canvas URL and Secure Canvas URL enter a URL to a script on your web server. In this script put the following:


Now we need to set up permissions for the app to post on our behalf. Load the following URL in your browser:’redirect_uri=redirect_url’scope=manage_pages,offline_access,publish_stream
app_id = Application ID (The one which we saved above)
redirect_url = Canvas URL (Facebook Integration Tab)

This will give your app full access to your feed and your pages. The offline_access priviledge will make it so we don’t have to re-authenticate every time we want to post.

After you confirm the app access to your account it should redirect back to your canvas URL script. The canvas URL script should output the GET array which should have a “code” variable. Take the code value and plug it into this URL:’redirect_uri=redirect_url’client_secret=app_secret&code=code_string
app_id = Application ID
redirect_url = Canvas URL (Facebook Integration Tab)
code_string = code from last URL response

This will output an access token variable. This token will allow us to post to our feed.

please download Facebook PHP SDK.

Now we should write php code using all parameters got so far.Please use a separate php page for this.

// Include facebook class (make sure your path is correct)

// Create our Application instance (replace this with your appId and secret).

$facebook = new Facebook(array(
 'appId' => 'app_id',
 'secret' => 'app_secret',
 'cookie' => true,

//$token is the access token from the URL above

$post = array('access_token' => $token, 'message' =>'any message','name' => 'name','description'=> 'description','picture' => imageurl,'link'=>'http://yourlink','caption' =>'caption');
$res = $this->tasks->api('/me/feed','POST',$post);
} catch (Exception $e){
echo $e->getMessage();

This will post a message to your feed. Remember to replace the app_id and app_secret with the info from your app.

You may be an admin of a fan page and want to post as the fan page and not yourself. To do this we just need to take one extra step and get a different access token. Load the following URL:

Where token is the access token from above. This will output a new access token for each of the pages you have admin access to. Use this token to post as your fan page instead of yourself and replace “me” to “page_username_id” in the above url: $res = $this->tasks->api(‘/me/feed’,’POST’,$post);   other things remain same.

Similarly you can post to your friends feed .For this again just replace “me” with “friend_username_id” in the above $res url.

Recently in 2012 Facebook has decided to remove the “offline_access” permission and has introduced a concept called long-lived access tokens which last a maximum of 60 days.

In order to extend the access token you need to make the following request with your short lived access token:

where EXISTING_ACCESS_TOKEN is what we used above. So once we get another token by using this url we need to replace previous token with this token in the above script.

The following two tabs change content below.
I am a core php developer and web designer working as a freelancer in elance,odesk etc.

Latest posts by Mudit (see all)

  • This has been a great feature for our website. I am sure that this code will help others as well.

  • I am a core Css developer and web designer working as a freelancer in elance,odesk etc.

  • Good….. This article is very useful for social media or Facebook to continuously send data.


    Why do you people always do the same, like sharing incomplete code?
    What is the $this reference in the code above, it doesn’t reference an object, what is that?

  • Really Facebook is very helpful to get a strong traffic on your website.

  • taoufiqaitali

    same issus :s

  • taoufiqaitali

    to make this work change

  • SajjadRaja

    Someone can help me to add autopost from my website to myfacebook page, im willing to pay this job, can send me msg on Thankx

  • subs4free

    Great tutorial..!! I followed your instructions and I succeeded in posting links to my fan page.
    Could you help me to tweak your code in order to be able to make posts with embedded video??
    The videos will be streamed from my server via flowplayer, so the source of the embedded video will be a SWF file.

    In the past I was doing that via RSS Grafitti, which is a facebook app, but lately that doesn't work.. 🙁

  • Le-Yo

    Hey, do you still need help with this? I am available if you still do.

    le-yo [at] le-yo [dot] com