The Facebook API lets you post to Pages you administrate via the API. With the release of Graph API v2.x and the new Facebook PHP SDK v4.0.x, I’m updating my old tutorial to show how to achieve the same functionality using the newer Graph API and PHP SDK.
First off, you need to add the manage_pages and publish_actions permissions to your app. This will let the application access the pages you own, and give the application permission to post to these pages. Note that this permission will give an application access to all the pages you own – not just the one you want to use.
Next, you need the access_token for the page you want to post to. This can be done by making a simple API call, as follows:
The API call with return the page ID
and access_token
, which will use later on in the tutorial to post to the page.
Once we have the page token, we can then construct a new API call to publish the post:
Along with the API call, you must post at lease one of the following parameters:
- message – the text for the status update, e.g. “Thanks for helping us reach 1.000 fans”.
- link – A URL to a website you want to link to
- picture – URL of an image you want to include in the update
- name – The name of the link attachment
- caption – The caption of the link (appears beneath the link name)
- description – The description of the link (appears beneath the link caption)
Once the API call has been made, Facebook will return the post_id for the update, so you can later get information back on it. If no post_id is included, an error message will be returned letting you know what went wrong. The return data will look like:
The status update on the page itself will look something like.
A full list of properties to use with the /feed API call can be found on Facebook’s Documentation for Feeds.
If you see a error message like the following when attempting to post to the page, you are missing the publish_actions
permission. You won’t be able to publish to the page without this permission. Note that accepting this permission will also give the application access to publish posts to your own timeline.
[…] UPDATE: See this tutorial for Facebook PHP SDK 4.0.x / Graph API v2.x version. […]
Awesome Post. Thanks a lot.
The link gets posted but I get following exception.
Fatal error: Uncaught exception ‘Facebook\FacebookAuthorizationException’ with message ‘Unsupported post request.’
Could you help me out with that?
Yes, it means your App is currently in sandbox mode. Update your app to be Public and it should work.
How do i make my app public as I have the same problem? it says I need to have a valid contact email to make this app available to all users
So all you need to do here is enter a valid contact email address in your App Settings and you’ll be able to make the App live.
Kudos Niraj. Yes, as you said the app was in sandbox mode. So from Roles, I added test users, using which I was able to make it work.
After the recent updates by facebook, when I try to add manage_pages permission and submit the app for review, the app gets rejected due to general reasons. Could you please give some tips to get my app approved by Facebook?
Make sure you give precise instruction on how the application works and where the permission is used. You need to provide step-by-step instructions on getting to that part of the application where the permissions are used. Include screenshots where necessary and provide a test user account for them to use to test the functionality.
Niraj, I am new to this API so please forgive my ignorance. How do I test my code? Quite simply, I am trying to post to a group page that I administer on certain anniversary dates. This will be a “cron” job running on my ISP. Any help would be GREATLY appreciated!!! I have some demos but when I try to have them post get a message indicating that my URL is not permitted. What should I put in “Settings” if my code is on “sal45.com” in /Facebook/index.php?
The best way to test your cron script would be to run it on command like: e.g. run
php your_script.php
in the terminal. Once you are happy with the output, you can create the corresponding cron on your system.Hi Niraj,
thanks a lot for this post but there are a couple of steps that are still unknown for me:
1) how can I add manage_pages and publish_actions permissions to my app? Do I have to update the app items and submit it for review?
2) how can I get the $session object you use in the first line of code?
Thanks in advance!
Please read this article to find the answer to both (1) and (2). Yes, your permissions will need to reviewed and approved before all users can use your app.
Thanks Niraj,
but my problem is that I don’t have any redirect url. My script is a cron that post on a fb page. How can I get a valid facebook session without using any of the facebook helper classes?
Thanks in advance!
You will need to get a valid access_token using Facebook Login, and store it in a database for the cron script to use. You won’t have to use a session in this case, as long as you have a valid access_token.
Hi, it dont work for me
Fatal error: Call to undefined function custom_facebook_getToken() in /home/clicmutu/www/laremise/_/www/facebook2.php on line 41
Where did you get that function from? The error means it doesn’t exist.
I have implemented Sharing photo on Facebook in my website, whenever I switch my Profile to my own “Page” and attempted to share something from my site then this popup appears BLANK.
I want to detect whether current profile is my “personal profile” or my “Page” using javascript sdk so that I can do something else.
or Please Please help me posting to Facebook page using FB javascript sdk
To Reproduce issue:
1 ==> Switch your FB profile to “Page”.
2 ==> Goto YouTube and try to share Video to FB.
This is an issue specific to your account as you are using an account linked with a page. The only way you can detect the type of profile being used is to make a call to
/me
and see ifcategory
exists. If it does, it’s likely to be a page.can you give me the full code please, much as I try not to get permits achievement to post a Facebook page
See the other tutorial on my site for instructions on how to setup login and the PHP SDK. This tutorial should can help you get started. If you need further help, you can pay for consulting services.
Hi,
Have Facebook removed the field “username” now? Something broke in our sscript last week. Seems to be this.
But can you post the all code? So…what I have to include in my script and in wich page? Thanks 😉
Please use the tutorial on how to use the Facebook PHP SDK to see the full code. You just need to drop in this API call into that code.
So I have only to add the code in the tutorial you suggest me and after the code in this tutorial?
Thanks 😉
Yep, just replace the API call in the first tutorial (specifically lines 92-95) with the code in this one.
I tried it, but when I open the page with the code in this tutorial….I have a “login” link, and nothing will be published…
I’m using PHP v 5.2.17…have you got the same tutorial for Facebook SDK 3.x?
You can use this old tutorial to achieve the same API call.
I solved with an upgrade to 5.6.6 php version
Hi !
I want to execute my post via a cronjob. How get you your $session variable ?
Thanks
Sessions don’t work with cronjobs. So you will need to use a Database to store the appropriate access tokens for each user, and then use them in a cron.
Is there more detail somewhere? Things like $page_id are undefined, even replacing lines 92-95.
A full example of this code in one spot would be most helpful.
$page_id
is a placeholder for your page ID. You need to define it yourself, or replace it with an actual page_id. If you start with this tutorial, the code in this article will make more sense.Hi Niraj,,
I created an app to post into users wall. Facebook team reviewed and approved “manage_pages, publish_actions’. still i am not getting page access_token.
Thanks
Are you making an API call to
/me/accounts
to get the Pageaccess_token
?I have a Problem with my code .. i pretty much copied what you wrote. When executed a Post is send from My User on the page wall but thats not what i want and i dont know what i am doing wrong. Obviously i want to Post as my Page onto my Page.
Thanks in advance
You need to request the
manage_pages
permission and get the Pageaccess_token
. Use this token in the API call to post as the page.Fatal error: Class ‘FacebookRequest’ not found in
Posting an error isn’t going to get you any closer to an answer. Please elaborate with a question and your source code. Also include the full error message.
Great tutorial. Thank you very much!
I have one question. I am successfully posting to the page and we are doing scheduled posts (in the future). After posting, I can see the posts show in the scheduled section on FB, but other people who are admins to the page cannot see the scheduled posts.
If I make them an admin to the App, they then can see the posts. Unfortunately, we have 100s of people that are page admins so not realistic to give them all access to the app.
Any thoughts on why this would happen. I would assume any page admin should be able to see the scheduled posts?
Thanks in advance,
Bo
Unfortunately, I don’t another way around this unless you build a Page Tab where the page admins can see the scheduled posts in a customer interface. It means you would have to build the Page Tab app (and it would be viewable to other uses), but you can lock down the content to only Admins, where you can list the posts that have been scheduled by the app.
I seem to have a chicken and egg issue. I used your login code for the new 4.1/5 php sdk, and successfully logged in, and I can post to my page with the page id, but it shows up as I posted it. In order to post as page, I used this as a guideline(with the new sdk) to get the page’s access token, and substituted that on the $fp-post(), however I’m getting the above error. Permissions are given to a user, as they login. how do you give permissions to a page’s access token in your above example…can you elaborate?
currently when I redirect to a login, it logs in my user, and the page’s access token is never given the permissions.(login as user, get page access token, login as page, get permission denied, log back in as user, etc…I’m in a loop)
When logging the user in, are you asking for the
manage_pages
permission? I’m assuming thepublish_actions
permission is also granted if you are able to post to the page. There aren’t any additional steps required after what you’ve done. The page access_token will grant you access to change anything on the page without requesting separate / additional permissions. Might be worth checking that the pageaccess_token
is correctly passed to yourpost()
API call.great post, thanks a lot
Everything is working, I can post to a page that I own, but that post is only visible to me, even though it shows that the post is public.
here’s the post image thats visible to me
https://postimg.org/image/52auc437n/
What I have to do so that post is visible to everyone
The
publish_actions
permission needs to be reviewed and approved by Facebook before it’s publicly viewable. Until then, only you and other developers / admins of your application will be able to view the posts.