How to Auto Publish with PHP to Facebook Wall

Posted on 2011-10-13
Last Modified: 2012-05-12
Hello. I am still relatively new to php, and I am trying to utilize this script to publish content to a facebook wall.

However, when I run the script, it does not publish to the facebook wall and all that prints is: Array ([ID]=>[(facebook page id)_(random number that changes upon page refresh)])

My intention is to eventually use a version of this script to enable multiple individuals to auto-publish content to facebook.

If anyone has any ideas on what I am doing wrong and how I can make this script publish to a facebook wall, I would appreciate it.

For reference, I am using the recommended GitHub facebook phpSDK, which can be found here:
    // Include facebook class (make sure your path is correct)  
    $userID ='xxxx'; //the user id of the account to publish to
	$accessToken ='xxxx'; //access token granting permission
	$message ='This is a Test'; 
    // Create our Application instance (replace this with your appId and secret).  
    $facebook = new Facebook(array(  
      'appId'  => 'xxxx',  
      'secret' => 'xxxx',  
      'cookie' => true,  
    $post = array('access_token' => $accessToken, 'message' => $message . date('Y-m-d'));  
    $res = $facebook->api('/'.$userID.'/feed','POST',$post);  
    } catch (Exception $e){  
        echo $e->getMessage();  
    }  ;

Open in new window

Question by:Cybervanes
    1 Comment
    LVL 18

    Accepted Solution


    the first thing I see is that your call to the fb api is incorrect. Here's a sample of what it should look like using the latest version of the PHP SDK:

    $post_id = $facebook->api('/me/feed', 'post', array('name'=>$title, 'link'=>$link, 'picture'=>$picture,'caption'=>$caption,'description'=>$description, 'actions'=>array('name'=>$actionname, 'link'=>$link)));

    Open in new window

    You will need to define the values of the variables so that they are referenced correctly at the time you do a wallpost.

    The other thing I noticed is that you aren't checking for the publish_stream permission. Your app must ask for implicit permission to use this as it doesn't come enabled out of the box. To get the permissions you can use something like:

    $perms = $facebook->api("/me/permissions/");

    Open in new window

    Then check for the perms:

    if(array_key_exists('publish_stream', $perms['data'][0])){
    // publish perms are found
    } else{ 
    $url = $facebook->getLoginUrl(array('scope' => 'publish_stream'));
    header("Location: $url");

    Open in new window

    The documentation on has been greatly improved so you should find everything you need.

    Hope this helps.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    Workplace bullying has increased with the use of email and social media. Retain evidence of this with email archiving to protect your employees.
    This story has been written with permission from the scammed victim, a valued client of mine – identity protected by request.
    Video by: Tony
    This video teaches viewers how to export a project from Adobe Premiere Pro and the various file types involved.
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now