Web Niraj
  • Facebook
  • Flickr
  • Github
  • Linkedin
  • Twitter
  • YouTube
Online portfolio, code examples and developer blog
  • About
  • Contact
  • Portfolio
  • WordPress
Search the site...
  • Home
  • Blog
  • Facebook API: Posting As A Page using Graph API v2.x and PHP SDK 4.0.x

Facebook API: Posting As A Page using Graph API v2.x and PHP SDK 4.0.x

44

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.

Post to Page using Facebook PHP SDK v4.0.x

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.

Facebook, Facebook Graph API, Facebook PHP SDK 4.0, PHP, Tutorial

44 comments on “Facebook API: Posting As A Page using Graph API v2.x and PHP SDK 4.0.x”

  1. Facebook API: Posting As A Page | Web Niraj says:
    August 23, 2014 at 2:36 PM

    […] UPDATE:  See this tutorial for Facebook PHP SDK 4.0.x / Graph API v2.x version. […]

    Reply
  2. Harit says:
    August 27, 2014 at 7:02 AM

    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?

    Reply
    • Niraj Shah says:
      August 27, 2014 at 11:11 AM

      Yes, it means your App is currently in sandbox mode. Update your app to be Public and it should work.

      Reply
      • Adam says:
        September 2, 2014 at 1:27 PM

        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

      • Niraj Shah says:
        September 2, 2014 at 1:45 PM

        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.

      • Harit says:
        September 16, 2014 at 11:29 AM

        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?

      • Niraj Shah says:
        September 25, 2014 at 9:59 AM

        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.

      • jerseysalsal says:
        January 5, 2016 at 12:24 PM

        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?

      • Niraj Shah says:
        January 18, 2016 at 3:44 PM

        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.

  3. Tom says:
    September 5, 2014 at 4:30 PM

    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!

    Reply
    • Niraj Shah says:
      September 5, 2014 at 4:54 PM

      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.

      Reply
      • Tom says:
        September 7, 2014 at 11:02 AM

        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!

      • Niraj Shah says:
        September 22, 2014 at 8:21 AM

        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.

  4. actupro says:
    September 6, 2014 at 3:02 PM

    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

    Reply
    • Niraj Shah says:
      September 14, 2014 at 11:59 PM

      Where did you get that function from? The error means it doesn’t exist.

      Reply
  5. Ranveer Malik says:
    September 9, 2014 at 4:31 PM

    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.

    Reply
    • Niraj Shah says:
      September 29, 2014 at 10:28 AM

      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 if category exists. If it does, it’s likely to be a page.

      Reply
  6. jhonny says:
    September 11, 2014 at 10:41 PM

    can you give me the full code please, much as I try not to get permits achievement to post a Facebook page

    Reply
    • Niraj Shah says:
      September 25, 2014 at 10:02 AM

      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.

      Reply
  7. John Smith says:
    November 30, 2014 at 6:47 PM

    Hi,

    Have Facebook removed the field “username” now? Something broke in our sscript last week. Seems to be this.

    Reply
  8. Luca Pandolfo says:
    February 27, 2015 at 9:30 AM

    But can you post the all code? So…what I have to include in my script and in wich page? Thanks 😉

    Reply
    • Niraj Shah says:
      February 27, 2015 at 9:50 AM

      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.

      Reply
      • Luca Pandolfo says:
        February 27, 2015 at 10:02 AM

        So I have only to add the code in the tutorial you suggest me and after the code in this tutorial?

        Thanks 😉

      • Niraj Shah says:
        February 27, 2015 at 8:38 PM

        Yep, just replace the API call in the first tutorial (specifically lines 92-95) with the code in this one.

      • Luca Pandolfo says:
        March 5, 2015 at 11:33 AM

        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…

  9. Luca Pandolfo says:
    February 28, 2015 at 3:50 PM

    I’m using PHP v 5.2.17…have you got the same tutorial for Facebook SDK 3.x?

    Reply
    • Niraj Shah says:
      February 28, 2015 at 10:26 PM

      You can use this old tutorial to achieve the same API call.

      Reply
      • Luca Pandolfo says:
        March 5, 2015 at 5:12 PM

        I solved with an upgrade to 5.6.6 php version

  10. djoo69o says:
    May 7, 2015 at 4:22 PM

    Hi !
    I want to execute my post via a cronjob. How get you your $session variable ?
    Thanks

    Reply
    • Niraj Shah says:
      May 7, 2015 at 4:31 PM

      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.

      Reply
  11. Admin-c says:
    May 11, 2015 at 2:42 PM

    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.

    Reply
    • Niraj Shah says:
      May 11, 2015 at 4:10 PM

      $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.

      Reply
  12. Jayaprakash says:
    June 5, 2015 at 7:55 PM

    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

    Reply
    • Niraj Shah says:
      June 22, 2015 at 9:40 AM

      Are you making an API call to /me/accounts to get the Page access_token?

      Reply
  13. Kevin May says:
    June 15, 2015 at 2:04 PM

    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

    Reply
    • Niraj Shah says:
      June 22, 2015 at 9:21 AM

      You need to request the manage_pages permission and get the Page access_token. Use this token in the API call to post as the page.

      Reply
  14. ᔕᕼᗩᕼIᗪ KᗩᖇIᗰI says:
    July 16, 2015 at 11:04 PM

    Fatal error: Class ‘FacebookRequest’ not found in

    Reply
    • Niraj Shah says:
      August 14, 2015 at 7:57 PM

      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.

      Reply
  15. Bo Schueramn says:
    August 11, 2015 at 9:44 PM

    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

    Reply
    • Niraj Shah says:
      August 14, 2015 at 8:04 PM

      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.

      Reply
  16. Barry Smoke says:
    September 20, 2015 at 6:15 AM

    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)

    Reply
    • Niraj Shah says:
      September 30, 2015 at 10:24 AM

      When logging the user in, are you asking for the manage_pages permission? I’m assuming the publish_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 page access_token is correctly passed to your post() API call.

      Reply
  17. Nishant says:
    December 12, 2016 at 7:49 PM

    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

    Reply
    • Niraj Shah says:
      December 14, 2016 at 11:29 AM

      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.

      Reply

Leave a Reply to Kevin MayCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

StackExchange / StackOverflow

profile for Niraj Shah on Stack Exchange, a network of free, community-driven Q&A sites

Support Me

Buy Me a Coffee

PSN Profile

Tags

ACL Amazon Amazon Web Services Android Android 4.4 KitKat Android 5.0 Lollipop Apache Backup Bug Command Line Cordova cPanel / WHM Facebook Facebook Graph API Facebook PHP SDK 4.0 Facebook Social Plugins Fan Page Firewall Flash Gadget Geolocation Google Nexus 5 Hacking HTML5 iOS JavaScript jQuery Laravel 5 Linux NodeJS Parse PDF PHP Plugin Portfolio PS4 Review Security Server SSH SSL Sysadmin Tutorial WordPress WordPress Plugins
© 2011-2025 Niraj Shah
  • Blog
  • Portfolio
  • WordPress
  • About Me
  • Contact Me
  • Privacy Policy
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Privacy Policy