Jorge Albaladejo

Hard & Soft design…

Publish to Facebook page or application’s wall with PHP

with 22 comments

Just after implementing the Twitter Oauth API to publish tweets from a PHP application, I thought that doing the same with Facebook would be a piece of cake. Well, not quite, although I’ve finally managed to have the messages published to a page wall, or even to an application’s page wall from an automated script. In this article I explain some of the problems I encountered and how to solved them. I hope this example may be of help if you are having trouble with this.

Publishing to a page’s wall

According to the Facebook documentation, we need to register an application in order to use it to publish messages to a page’s wall. The process is explained on that page, so this should be a good moment to read it.

The process could be summarized as follows:

  1. Create an application.
  2. Get the token for that application to manage the user’s pages (where the user is administrator of those pages).
  3. Get the specific token for that application through the user to a precise page.

While it should have been worked as it is explained on the documentation, I was having the following error:

(#200) The user hasn’t authorized the application to perform this action

So… why? According to the documentation, manage_pages is the only permission required for an application to access a page, and everything seemed in order. Actually, it wasn’t. After some tests going back and forth and some googleing, I realized I didn’t have enough scope, and adding publish_stream and read_stream seemed to be a requirement for the application to actually write to page’s wall. In addition, offline_access would produce a token that does not expire.

Once this issue fixed, the following steps should lead to a successful publication in a page’s wall from an external script:

  1. Register your application here.
  2. Obtain access to manage pages from the authorized page administrator
    https://www.facebook.com/dialog/oauth?client_id=$id&client_secret=$secret&redirect_uri=$uri&scope=publish_stream,offline_access,read_stream,manage_pages&response_type=token
  3. Get an access token to the precise page of that administrator
    https://graph.facebook.com/me/accounts?access_token=$all_apps_token
  4. Now you should be able to publish to that page with this token through a POST call to
    https://graph.facebook.com/$page_id/feed

Here you are an example in PHP that uses this token to publish messages:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
class Facebook
{	    
    /**
     * @var The page id to edit
     */
    private $page_id = 'XXX';	
 
    /**
     * @var the page access token given to the application above
     */
    private $page_access_token = 'YYY';	
 
    /**
     * @var The back-end service for page's wall
     */
    private $post_url = '';
 
    /**
     * Constructor, sets the url's
     */
    public function Facebook()
    {
        $this->post_url = 'https://graph.facebook.com/'.$this->page_id.'/feed';
    }
 
    /**
     * Manages the POST message to post an update on a page wall
     * 
     * @param array $data
     * @return string the back-end response
     * @private
     */
    public function message($data)
    {   
        // need token
        $data['access_token'] = $this->page_access_token;
 
        // init
        $ch = curl_init();
 
        curl_setopt($ch, CURLOPT_URL, $this->post_url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 
        // execute and close
        $return = curl_exec($ch);
        curl_close($ch);
 
        // end
        return $return;        
    }
}
 
$facebook = new Facebook();
 
$facebook->message(array( 'message'     => 'The status header', 
                          'link'        => 'http://theurltopoint.to', 
                          'picture'     => 'http://thepicturetoinclude.jpg',
                          'name'        => 'Name of the picture, shown just above it', 
                          'description' => 'Full description explaining whether the header or the picture' ) );

Publishing to an app’s wall

In some cases, we might want to publish to the application’s wall itself, so that there is no need to have a second application to publish to the first one’s wall.

According to some of the discussions on the matter that can be found on the internet, this should work by simply using $page_id as the target and the same $page_id to request the manage_pages permission.

In this case, we would follow the same process explained above, but slightly simplified:

  1. Register your application here.
  2. Obtain an access code for your application
    https://graph.facebook.com/oauth/access_token?type=client_cred&client_id=$id&client_secret=$secret
  3. Now you should be able to publish to the application’s wall with the token obtained through a POST call to
    https://graph.facebook.com/$app_id/feed

However, Following the steps above, I obtained an error again:

{“error”:{“type”:”OAuthException”,”message”:”(#200) Posts where the actor is a page cannot also include a target_id”}}

Which was way confusing since there shouldn’t be any limitation according to Facebook’s documentation. After some searching and reading literature about (here and here), I discovered that publishing from an application to its own wall is doable, and in fact it works very well with the JS API provided by Facebook.

Then why the JS API would achieve the goal while its web service sister just throws a mystic error? It was quite obvious that they had to be using somewhat different service url’s… after inspecting the HTTP calls made by the JS API and was surprised to found that it is actually a GET (instead of POST call):

https://graph.facebook.com/$app_id/feed?access_token=$token&callback=FB.ApiServer._callbacks.$some_callback_id&message=Hello%2C%20World!&method=post&pretty=0&sdk=joey

I just had to test this new url with the parameters of my application, and this time it just worked as expected:

https://graph.facebook.com/$app_id/feed?access_token=$api_access_token&message=Hello%2C%20World!&method=post

This is the only solution I have found so far to be able to publish messages from PHP on behalf of an application itself. It is a bit awful, but I haven’t found any other way to make this work, and the documentation provided by Facebook seems like knocking on a wall.

Has anybody found any better solution for this problem? Comments are welcome!

Written by Jorge Albaladejo

June 13th, 2011 at 3:57 pm

22 Responses to 'Publish to Facebook page or application’s wall with PHP'

Subscribe to comments with RSS or TrackBack to 'Publish to Facebook page or application’s wall with PHP'.

  1. Hi Jorge,

    Thank you for your helpful post. I’ve got a question regarding your script though:

    Where in the code are you authenticating with the $page_access_token?? So far I haven’t been able to post to my page’s wall, even though I’ve gone through the whole process you described above.

    Another small note: why did you make the function message private? That way you wouldn’t be able to call the function through the object $facebook right??

    Robin

    30 Jun 11 at 13:20

  2. Hello Robin, thanks for your comment. You are right in both subjects: the function ‘message’ must be public to be called from an instance of the Facebook class, and $page_access_token must be added to the POST message as ‘access_token’. I’ve corrected the code in this post’s body.

    Please let me know if you are still having trouble with this script to work.

    Jorge Albaladejo

    30 Jun 11 at 17:04

  3. Thanks so much for this information, I’ve been trying to do similar myself and the information has been extremely lacking from Facebook documentations.

    Does this require my website to have an SSL cert? When trying to register my application its requiring that I fill in a secure link section.

    John

    5 Jul 11 at 16:32

  4. Hi Jorge,

    Thanks for your sharing! It’s work for me.

    Could you advise how to change the poster name with application’s name not the application owner’s name?

    David

    17 Aug 11 at 01:57

  5. Thanks for the code.

    Needed to change the following line:
    curl_setopt($ch, CURLOPT_URL, $this->post_page_url);

    to:
    curl_setopt($ch, CURLOPT_URL, $this->post_url);

    And, if the following wasn’t clear to everyone, what should go here:
    private $post_url = ”;

    use the following as the value:
    https://graph.facebook.com/$page_id/feed

    Thanks again!

    James

    26 Aug 11 at 06:45

  6. Thanks for your comment, James. I’ve corrected the concerned line of code.

    Jorge Albaladejo

    26 Aug 11 at 07:36

  7. It looks like you have EXACTLY what I am looking for… now I have to go thru this step by step and get the silly thing to work.

    (basically the ability to post comments to our FB Page “https://www.facebook.com/pages/Inferno-Online-Stockholm/149232401793444″ from the website “www.infernoonline.com/” (you would think this was a little more straight forward)

    Fox Purtill

    14 Oct 11 at 10:00

  8. Thaaanks, worked fine here!

    André Steinn

    21 Nov 11 at 12:51

  9. Thank you Jorge . It worked fine for me. At first I thought I should download and include php-sdk. But it was not needed.
    Thank you james for giving hint of $post_url.

    sandip kc

    2 Dec 11 at 19:08

  10. Hello and thank you for your code, it works very well !
    I want to know if it is possible to post at the page’s wall as the “page” and not as the page owner. Currently when i make a post, i post by my facebook name instead of my page’s name.

    Thank you in advance.

    redion

    21 Dec 11 at 17:09

  11. I would appreciate it if you could tell me how to get the post’s id after publishing (for future deletion).

    Thank you again.

    redion

    22 Dec 11 at 00:05

  12. I followed all the steps but I dont understand why I get a blank page when I run the code! :S

    Salma

    5 Jan 12 at 15:59

  13. Yes I would be keen to know (like redion) how to ‘post as the app’ and not as the facebook user. If that is even possible.

    Jeff

    11 Jan 12 at 06:51

  14. @redion: Check the return value of $facebook->message(), I haven’t had the need to use it for my project but as far as I can remember, there is some information about the publication there.

    Jorge Albaladejo

    11 Jan 12 at 09:16

  15. @Jeff & @redion As far as I know, it is not possible, at the least I haven’t found a way. Due to the tokens system in Facebook, you need whether a user, whether an application to publish on a page, therefore their information will appear on the publication credits, instead of it being posted as the page itself. If anybody has found a solution to this problem, please let us know!

    Jorge Albaladejo

    11 Jan 12 at 09:20

  16. Thanks Jorge for this helpful post,

    I am able post on my page but the problem is whatever I post is posted as me and not the page itself, secondly I am unable to post images (though I’ve setup things in the curl part) and it actually posts images on my profile instead of the page.

    please help.

    Muhammad Ali

    21 Jan 12 at 14:55

  17. Trying to get the access token, but Facebook only returns “An error occurred. Please try again later.”

    I’ve added client_id = app id and client_secret = app secret.

    And the redirect_uri goes to my page wall.

    Did I miss something?

    Urme

    7 Feb 12 at 09:25

  18. Got it working by using this address instead to get the access token

    https://graph.facebook.com/oauth/authorize?
    type=user_agent&
    client_id=app_id&
    redirect_uri=my_redirect_uri&scope=publish_stream,offline_access,read_stream,manage_pages&response_type=token

    Urme

    7 Feb 12 at 10:30

  19. Very nice post mate thanks.

    One question (sorry i am a noob) how can I call external vars in the final array ?
    Like :
    $facebook->message(array(
    ‘message’ => $dbvar1,
    ‘link’ => $dbvar2 ) );

    so i can get that data for dbvar1 and dbvar2 from a database call.

    Thanks allot
    Larry

    Larry

    14 Feb 12 at 14:06

  20. Have the same problem, like you, Urme, but your solution also doesn’t work for me. Got the JSON-Error-Object

    {
    error: {
    message: “Invalid redirect_uri: Die Anwendungseinstellungen lassen die angegebene URL nicht zu.”,
    type: “OAuthException”,
    code: 191
    }
    }

    (Translated from german: “ARL not allowed by app settings”).
    Any idea?

    It’s driving me crazy: Just want to do a f***g post to a pages’ wall and blowed out about 5 hours without any working solution, because google find’s many solutions but finding and interpreting the RIGHT solution is a pain.

    Mirco

    29 Feb 12 at 13:26

  21. It seems like this is what I need, but no idea of how to use it, can you be less “dev” and give to some of us the steps as in where this code need to be placed ? thanks a lot

    Daniel

    23 Mar 12 at 17:19

  22. Nice tut. But i have one problem. The Share link doesn`t apear on my posts. Can i solve this? Thanls

    florin

    7 May 12 at 19:54

Leave a Reply

This site is protected with Urban Giraffe's plugin 'HTML Purified' and Edward Z. Yang's Powered by HTML Purifier. 7051 items have been purified.