How to post to a Facebook page with PHP

I would like to post to my own Facebook page's wall from my website using PHP.

I have the following:

  • Facebook Application with AppID, AppSecret, ApiKey
  • Facebook Page with PageID
  • my own Facebook account - I'm the admin and the creator of the application and page mentioned above.

E.g. I write a blog post, and I'd like to get the name, the short description and a picture on my Facebook page's wall. Or I would like to publish some articles on the Facebook page every day automatically as a cron job.

Could you provide a step-by-step tutorial how to accomplish this?

I've read this article about Facebook Login: but I still don't know what to write in my code.


This is how I send a request for an App Access Token:

$url = ''

The response is similar to this (fake symbols):


What access_token is it? Application Access Token? How to get a User Access Token?

I tried to use the access token from here: but I got the following error message:

An active access token must be used to query information about the current user

So how should I obtain the right access token?


How can I get the right Facebook tokens in my application without any client interaction? I'm the admin and the creator of the Facebook Application and the Facebook Page.


Step by step

  1. Authenticate a user that is a page admin (yourself)
  2. Request an extended access token (to get a 60 day variety as offline_access is gone). See
  3. Call Graph API me/accounts and search thru the resulting list to find the page you're interested in
  4. Take the page access token from the page and start using that for the calls to post
  5. It might be possible to get an extended access token for a page access token like described in step 2, please try and let us know if that can be done for page access token too.

You can experiment with the above at

Happy Coding!


For getting an access token without dialogs for any user, you can use to get an access token.


  1. Request For manage_pages permission ( Allow this process ) :
  2. Get Access token from URL : If the administrator allows this permission. You should be redirected to URL below:


    Use Access token in the URL and you should get this:

  3. Check Access token using Graph API:

    Connection will return information and access token for each page.

  4. Implement it in your code: You can use App access token while call a Facebook Graph API method.

Update: If you want use API method in Facebook SDK, DEPRECATED REST API or FQL Query...

You have to use users_accesstoken this way:

  1. Method 1: Use your account or users to login to your Facebook page with offline_access permissions and grab access_token while login success using $facebook->getAccessToken(), and save it in database so you can use it anytime. You can check the expiration time of the token here, token with offline_access permissions never expire except when the user changes his password or maybe anything else.

  2. Method 2: You can update your access_token dynamically using the code below (say goodbye to expire token). Facebook shows this solution here, it's a sample code for executing an FQL Query:


$app_id = 'YOUR_APP_ID';
$app_secret = 'YOUR_APP_SECRET';
$my_url = 'POST_AUTH_URL';
$code = $_REQUEST["code"];

//auth user
if(empty($code)) {
    $dialog_url = '' 
        . $app_id . '&redirect_uri=' . urlencode($my_url) ;
    echo("<script>top.location.href='" . $dialog_url . "'</script>");    

//get user access_token
$token_url = ''
. $app_id . '&redirect_uri=' . urlencode($my_url) 
. '&client_secret=' . $app_secret 
. '&code=' . $code;
$access_token = file_get_contents($token_url);   

Try this simple function to post onto a wall:

function doWallPost($postName = '', $postMessage = '', $postLink = '', $postCaption = '', $postDescription = '') {
  $FB_APP_ID     = 'xxxxxxxxxxxxxxxxxxxxxxxx';
  $FB_APP_SECRET = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx';

  $APP_RETURN_URL = ((substr($_SERVER['SERVER_PROTOCOL'], 0, 4) == "HTTP") ? "http://" : "https://") . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'];

  $code = $_REQUEST["code"];

  if (empty($code)) {
    $dialog_url = "" . $FB_APP_ID . "&redirect_uri=" . $APP_RETURN_URL . "&scope=publish_stream";

  $token_url    = "" . $FB_APP_ID . "&redirect_uri=" . urlencode($APP_RETURN_URL) . "&client_secret=" . $FB_APP_SECRET . "&code=" . $code;
  $access_token = file_get_contents($token_url);

  $param1          = explode("&", $access_token);
  $param2          = explode("=", $param1[0]);
  $FB_ACCESS_TOKEN = $param2[1];

  $url        = "";
  $attachment = array(
    'access_token' => $FB_ACCESS_TOKEN,
    'name' => $postName,
    'link' => $postLink,
    'description' => $postDescription,
    'message' => $postMessage,
    'caption' => $postCaption

  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_POST, true);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
  $result = curl_exec($ch);

  return $result;

Need Your Help

no find() method on xml object

javascript xml

After extracting the tags I require from an xml document I have two set of tags that I want to iterate over. For one of the sets I get the message:

Using variables with .insertAfter is not working

jquery css html5 variables insertafter

I'm a beginner coder and my English is very bad, so i apologize in front. I have the following CSS: