Publish on Facebook Fan Page as fan user Using PHP

First Follow all the steps to give permissions to publish on Facebook. http://blog.theunical.com/facebook-integration/5-steps-to-publish-on-a-facebook-wall-using-php/

Add FaceBook application to your fan page

Just Go to http://www.facebook.com/apps/application.php?id=ID_OF_YOUR_APP.

Find link ‘Add to my Page’ on the left, click on it. Find your page on the list and click on ‘Add to page’ next to this page.

Set Your Application type as WEB

Go to http://www.facebook.com/developers/editapp.php?app_id=ID_OF_YOUR_APP.

Choose ‘Web’ next to ‘Application type’ and click on ‘Save Changes’ button.

Add permissions to your app

Prepare special link:

http://www.facebook.com/connect/prompt_permissions.php?api_key=YOUR_API_KEY&v=1.0&next=http://www.facebook.com/connect/login_success.html?xxRESULTTOKENxx&display=page&ext_perm=publish_stream,offline_access&enable_profile_selector=1&profile_selector_ids=ID_OF_YOUR_PAGE

replace YOUR_API_KEY for an API KEY of your application and ID_OF_YOUR_PAGE for an id of your page

Run this URL in your web browser. Click on ‘Allow’. This will set the permissions to your application to publish on page.

You should see a message: ‘Success’ message.

Check Permissions

Go to http://developers.facebook.com/tools.php?app_id=ID_OF_YOUR_PAGE and check your permissions.

Choose your application, change ‘Response Format’ to JSON, choose method ‘users.hasAppPermission’, fill publish_stream in ext_perm and id of your page in uid.

You should see result: 1.

That means that everything is ok. If the result is 0. then something went wrong.  Then check all the above steps again.

Code To Publish on Fan Page as Fan User Using PHP

Then to publish on fan page as a Fan page user and not the facebook user just follow below code.

For this you need to get Fan page ID:

Example : http://www.facebook.com/pages/Fan-Page/xxxxxxx

where xxxxxxx is the fan page id.

In Steam publish method just replace the $page_id with your above fan page id.

stream_publish($message, $attachment,$action_links, null,$page_id).

<?php

// FB_APIKEY is your facebook application api key
// FB_SECRET is your application secrete key

define('FB_APIKEY', 'YOUR_APIKEY');
define('FB_SECRET', 'YOUR_SECRET');
define('FB_SESSION', 'YOUR_SESSION_key');

require_once('facebook-platform/php/facebook.php');

try {
 $facebook = new Facebook(FB_APIKEY, FB_SECRET);
$facebook->api_client->session_key = FB_SESSION;
 $fetch = array('friends' =>
 array('pattern' => '.*',
 'query' => "select uid2 from friend where uid1={$user}"));

echo $facebook->api_client->admin_setAppProperties(array('preload_fql' => json_encode($fetch))); 

$message = 'From My App: publish steven on facebook Fan Page as Fan User';
$page_id ="xxxxxxxxxxx";
if( $facebook->api_client->stream_publish($message, $attachment,$action_links, null,$page_id))
 echo "Added on FB Wall on Fan Page";
} catch(Exception $e) {
 echo $e . "<br />";

}
?>

Below is the screen where you can see the message published are fan user.
Publish on facebook fan page as fan user

— Steven
Follow Us

Steven Fan Page on Facebook

56 thoughts on “Publish on Facebook Fan Page as fan user Using PHP”

  1. Yeah, unfortunately I am still having the same error message. As with the other example. I’m ready to pull my hair out and wish I could punch the Facebook API in the face LOL. Integrating with Twitter was 1000x easier.

    Message still says:

    exception ‘FacebookRestClientException’ with message ‘This method call must be signed with the application secret (You are probably calling a secure method using a session secret)’

    I have my app defined as a “Desktop” app. Should it be defined otherwise? Any insight would be greatly appreciated…

  2. Sorry Steven don’t mean to be spamming your board – But alas I answered my own question. I changed my app from being defined as “Desktop” to “Web” and sure enough it worked!

    Thanks again for your helpful articles and I hope this info helps anybody else encountering this challenge…

  3. Hi Steven, I followed your previous post and I did post to my profile’s wall successful. But when I try to post to my page’s wall, I got an error :

    exception ‘FacebookRestClientException’ with message ‘User not visible’

    if I run: $facebook->api_client->stream_publish($message, NULL, NULL,$page_id)
    (call stream_publish with 4 params like FB’s documentation)

    and this error:
    exception ‘FacebookRestClientException’ with message ‘The user hasn’t authorized the application to perform this action’

    if I run: $facebook->api_client->stream_publish($message, NULL, NULL, NULL, $page_id)
    (call stream_publish with 5 params like your instructions)

    Notice that I already checked to ‘Facebook Pages’ on Authentication settings of Application and added my app to my page.

    Can you help me to solve this problem. Thanks in advance.

  4. Wang, Pls use the below URL to set the permissions to your fan page.
    http://www.facebook.com/connect/prompt_permissions.php?api_key=YOUR_API_KEY&v=1.0&next=http://www.facebook.com/connect/login_success.html?xxRESULTTOKENxx&display=page&ext_perm=publish_stream,offline_access&enable_profile_selector=1&profile_selector_ids=ID_OF_YOUR_PAGE

    replace YOUR_API_KEY for an API KEY of your application and ID_OF_YOUR_PAGE for an id of your page

    Run this URL in your web browser. Click on ‘Allow’.

    You should see a message: ‘Success’.

  5. Hi Steven, thanks for your very quick answer. But I did as you said, message Success did show up, but I still got the message:

    ‘The user hasn’t authorized the application to perform this action’.

    I tested my code again to publish message on profile’s wall, it still works good. I really dont know why it cant work on my page. Is there any other ways that I can try ?
    Thanks for your help again.

  6. About my problem, I look around on internet and I found this page:
    http://thinkdiff.net/facebook/update-facebook-page-status-automatically/

    The author said that we need ‘extended permission’ by running the permission.php (code posted on this site), but I dont know how to run that file on Facebook (http://apps.facebook.com/yourapps/permission.php !? )

    In case you have other solutions for me, if not, can you look at that page to check if it is a solution for my problem ? Thanks you.

  7. Hi Luca,
    There is some permission problems in your application. Your application returns 0 and not 1.
    Go to http://developers.facebook.com/tools.php?app_id=335851346097 and check your permissions.

    Choose your application, change ‘Response Format’ to JSON, choose method ‘users.hasAppPermission’, fill publish_stream in ext_perm and id of your page in uid.

    You should see result: 1.

    That means that everything is ok.

  8. Hi Steven,
    application is set to web..

    When I look at permissions I receive:

    {“error_code”:200,”error_msg”:”User must have accepted TOS”,….

    What I should do to sort it out?

    Thanks!!!

  9. Hi Steven, I tried many times but still got message ‘The user hasn’t authorized the application to perform this action’. When the first time I follow you 5 steps to publish on wall I got the same message too (because I didnt run the permission link yet). But this time I run your special link to set permission and I got the ‘Success’ message too, but still fail.
    Do you know how to debug this situations ? Pls help. Thanks alot !

  10. I tried even with my application’s wall and still got the same message. I use my app to post message on it’s wall but stil fail, I really can’t got it !

  11. Sorry, I dont want to spam your post, but I just want you to know that I did check permission using Facebook tools and I’m sure that my app already has permission to post on my fan page’s wall.

  12. Finally, with Steven’s help, we find out that my problem was because of the quotes. I pass the page_id to function stream_publish without quotes. Thanks Steven for your enthusiastic help.

  13. I fixed the 100 error by removing the “,offline_access” element from the prompt permissions URL and it worked perfectly.

  14. Steven – Great detailed post as usual! I have implemented your code and it’s all working fine except for one small important detail.

    I am using this to post as an fan page admin to fan page and it works. However, the posting comes across with my personal account profile information, picture instead of that of the Fan page.

    Is there a way to make the posting seem like its published by the Fan page itself?

    Thanks and regards,
    Doug

  15. Yes – thank you Steven. But I am passing the page id of the fan page. It just shows that I’ve (my account) has added the post to the fan page instead of looking like it was the page itself that made the post.

    I’m hoping that it’s just some type of permissions issue but I feel like I’ve tried all the urls here.

    Thanks again!

  16. I am getting this error and it is driving me nuts, I have been trying all the tutorials for 2 days straight and can’t get this to work. Please point me in the right direction 🙂

    Fatal error: Call to undefined method stdClass::admin_setAppProperties()

  17. Ok Steven I followed both of your tutorials and now I get the following message in Facebok anytime the code is called:

    Jeff Vaccaro->GODSurfer:AlphaProject: From GODSurfer:AlphaProject:A story on GODSurfer.com was voted up!

    4 minutes ago via GODSurfer:AlphaProject – App · Comment · LikeUnlike · Report

    That is sort of what I want, however, I don’t always want it to be me. Does that make sense? Is there a way that instead of the message coming from me can I just do it from the application?

    i.e.
    GODSurfer App->GODSurfer:AlphaProject: message goes here…

  18. Hi,
    I have followed each step as mentioned above,
    I have struck at step Check Permissions.

    When I check the URL mentioned in “Add permissions to your app” step using page id, I get the success message (FB is not showing me allow /Deny box and directly showing me the success message)

    I have created one time SessionId as given in last article (5 Steps to publish on a facebook wall using php)

    I am able to post my message on my profile wall.

    But when I am trying to do it on Page wall It is giving me the following error
    “The user hasn’t authorized the application to perform this action”
    You can check the error here;
    http://folkehesten.devant.no/postonfbpage.php

    Please let me know where I am going wrong.

    Thanks in advance

    Regards
    -Vrushali

  19. Hi Steven,

    Thank you for quick reply.

    I was doing same things yesterday but not got any luck.
    When today I follow the same it start working.

    I think Facebook is taking some time to update the settings.

    Both your posts are amazing, that really helps me to solve the challenges.

    One link I want to share for checking the permission, where I got confuse and finally found the following link..
    http://developers.facebook.com/docs/reference/rest/users.hasAppPermission

    Thanks again..

    Regards
    -Vrushali

  20. I am php developer working on an application to publish posts on to a page [available to public]

    I followed the steps to post a message as a page . I have set the target id to the page id

    When i log into the face book account in which i own this page I can see the posts from my web application only when I click on ‘+ other’ tab under wall. Just and just others so not show.

    Also when i friend [ not in my friend list /fan] searches for the page name in his account , the postings are not to be seen under any of the above mentioned 3 tabs . Could you please tell me how to make these posts from my application available to all those who visit the page.

    Thanks

  21. is there any way to automate the step :
    Add FaceBook application to your fan page

    in other words, can we show a popup of fan pages listing and redirect to our own site on success.

    actually I dont want the user to click on the link on Facebook which says “add to my page”

  22. I need to upload a photo/video on fan page wall. Functions are available in php rest lib for this: photos_upload() and video_upload() . I tried for photo but It was uploading the photo on my user page, NOT on fan page wall. Please advice.

    Thanks
    Baljeet

  23. Thanks Steve !
    Thanks for quick response, Its working now. Have you written any article on how to do similar things on Twitter and LinkedIn ?

  24. I’ll will wait for it, and will try at my end too.
    I was trying to publish video on fan page wall using the upload videos function. But looks like it does not take page_id or target_id as parameter, then how to post a video on fan page.

    Also for creating events, there is a function but it also does not seem taking page_id/target id as parameter. One of parameter here is an arrray, may be that contains all values.

    Please suggest how to move ahead.

    Thanks again
    Baljeet

  25. When I originally commented I clicked the “Notify me when new comments are added” checkbox and now each time a comment is added I get four emails with the same comment.

  26. Hi Steve, right now, while adding the app to the fan page, I’m redirecting to the facebook site. On facebook site, user clicks on “add to may page”, which opens a popups and so on..
    But user point of view it is not good. Is there any way to show this popup of my site itself or some iframe or something similar which is user friendly.

    Thanks
    Baljeet

  27. Hi Steve,

    Sorry for posting similar question again, but i have got some progress in adding fan page to my facebook application. Now I’m able to display the fan pages I own on my site itself. I just need some script which will add selected pages to the application.

    Thanks
    Baljeet

  28. people who able to post on their (user profile) wall, but can’t post on a fan page is because the sample above is not given a right target_id

    original: if( $facebook->api_client->stream_publish($message, $attachment,$action_links, null,$page_id))

    corrected: if( $facebook->api_client->stream_publish($message, $attachment,$action_links, $target_id[fan page id],$user_id))

    [fan page id] by clicking the fan page, and you can see a number on the url.
    [user_id] by clicking your name next to the profile photo then you can get it from url also. eg. http://www.facebook.com/#!/profile.php?id=xxxxxxxxx

    i have sample on C# but not in PHP.

  29. Hi Jay,
    Thanks for sharing your views. I was trying the original code from Steve and it was working fine for me. I was posting to page wall as page admin. The NULL in original code indicates the default poster (page admin this case).

    I tried ur code and it is working fine as well/
    are you pointing towards change in the sequence of parameters (last two here) ?

  30. Hi Steven ,

    I followed your article but some how it is failing at “$facebook->api_client->admin_setAppProperties” . Error message is something like [message:protected] => The session has been invalidated because the user has changed the password.
    Session is active . Could you please give some thoughts here

Leave a Reply

Your email address will not be published. Required fields are marked *

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Anti-spam image