January 27, 2014 3:13 pm

How to post into a Facebook Page with PHP using Graph API

We have published one article on How to publish status on Facebook with Graph API which post status on user’s wall when user write , now I am going to show how to post status on pages to post status on Facebook page we use Facebook Graph API in PHP to perform this task.

How to post into a Facebook Page with PHP using Graph API

To create Facebook application follow instruction on my previous post How to Login with Facebook Graph API in PHP

Script contains two folders called src and images with PHP files.

src

– base_facebook.php // Class Get user Information.

– facebook.php // Class Get user Information.

– config.php // Configuration file.

images

index.php // Main index file.

PHP Code

Edit config.php

On the main page show you one button for connect with PHP on redirect to Facebook it will ask you for permission here we add one more permission which is manage_pages with this permission you will allow that application to manage your pages.

index.php

Explanation of above code:

We will call this URL to get Facebook profile token.

On callback page it will give you a code parameter using that code you will call this URL in backhand and get access token.

After that we got the access token of that user with manage pages permission in $response variable.

Now use that token and get all pages under that user account and their access tocke.

facebook-page-tocken-phpgang

This page show you all pages token and page name and id’s in demo we show dropdown of pages you can select any page and add status in text box and publish it will publish it on your page.

Create a drop down from above json code is very simple and easy here is the snippet for that.

Status Publish Code

There is 2 code snippet one can share link on page with custom image and description like blow:

The above code can share a link like this screenshot:

link-share-on-facebook-page

2nd snippet of sharing which only share your text on the page and if you include any link it will extract that link itself and show title image and description.

Screenshot of status posted by above snippet:

simple-status-share-on-facebook-page

 This is the simple scenario of posting on Facebook pages I hope you like this tutorial so please give your feedback in comments.

Author Huzoor Bux

I am Huzoor Bux from Karachi (Pakistan). I have been working as a PHP Developer from last 5+ years, and its my passion to learn new things and implement them as a practice. Basically I am a PHP developer but now days exploring more in HTML5, CSS and jQuery libraries.


Tutorial Categories:
  • Lokesh Sharma

    hello sir after login my page is not redircting

    • huzoorbux

      redirect where did you set callback url in config.php file?

  • shojol80

    thank you sir,,its working fine,,but i want to delete some post after few moments,,is it possible to delete any post using there API??

    • huzoorbux

      Yes you can remove post by its id

      Use this command and delete.

    • shojol80

      hy ,thanks for ur reply,,do i need any token ?

    • Chakrimela com

      i am facing the problem that the posts are not showing publicly.. But i selected PUBLIC while apps was asking permission. can you please advise a solution

  • 555|STi

    Can’t download script. =/ I’m already subscribed with 2 email addresses.

    • Sebastien

      Same problem

      • huzoorbux

        On download page we mention that subscribers list takes 6 hours to update, now you can download if you have subscribed.

        • Sebastien

          I suscribed two weeks ago … ๐Ÿ™‚

        • 555|STi

          I subscribed two days ago… =/
          The arrows just keep spinning.

        • 555|STi

          Still can’t download, now it says i have to subscribe again.
          “Sorry no email found subscribe below.”

          • huzoorbux

            You have unsubscribed from our feed that’s why you are again not able to download any code again join our subscription to download source code.

          • 555|STi

            I have subscribed a third email account. This is all i get.

            Can you send the file to my email?

      • huzoorbux

        give me your email i will send you direct link to download script.

        • 555|STi

          555 at gmail dot com

          Thank you =)!

  • srinuchilukuri

    Hi huzoorbux sir,

    using Facebook graph API to post links into my Facebook page. From yesterday on wards I am getting ‘Use App’ button with each post feed.

    how to remove “Use App” button
    please find the screenshoot.

    • huzoorbux

      Basically it comes in your news feed and Facebook promote your app itself nothing wrong in this.

      • srinuchilukuri

        Thanks a lot for reply…..

    • Chakrimela com

      i am facing the problem that the posts are not showing publicly.. But i selected PUBLIC while apps was asking permission. can you please advise a solution please

  • mayank

    no link found for downloading

  • Marcos Trama

    Good article!
    I’m having a problem when i post the link. When i use the ‘link’ field, the post is made with my user (owner of the page). When i dont use the ‘link’ field, the post is made as the page, but i get no link to my website. Do you know what this happens? thanks a lot

  • Chakrimela com

    My posts are posting. But only i can see the post when i log in as admin but can not see the post while i ma not logged as admin, means outside users are unable to see.

    Please advise solution.

    • huzoorbux

      posts created by this app any one can see but not if you chose only me during authentication.

      • Chakrimela com

        I have selected Public here but same problem

        • huzoorbux

          give me link of your application

          • Chakrimela com

            Found that if i post to my page facebook.com/chakrimela using your demo at your site then it is showing to everyone.

            Can you please advise

          • huzoorbux

            Make sure that your app is on live mode with that green dot.

            if not follow this to do it: http://stackoverflow.com/questions/20696738/i-am-new-how-do-i-make-my-app-live-on-fb

          • Guest

            No it’s not live. How to make it live?

          • Chakrimela com

            You have made a GREAT code and you have assisted me greatly…. i am grateful to you because i was looking for this solution since last 1 year..

  • Kiran

    Hi .. Nice tutorials !
    I was looking for same stuffs to integrate in my website.. But little change is I want to post comments in to users profile instead page.. I have goggled but what i found is not working here. Pls Kindly suggest me asap..

  • Dave

    6 hours to download a crappy script that has laready shown a huge amount of error in your own Demo! lol fuck off and remove my email from your mailing list asap. I’ve already spam listed your email so don’t botehr to send me anything anyway. idiots.

  • niko

    Now this is old example. How can we do with facebook php sdk version 4.0.9?

  • arpita

    nice tutorial but not downlodable.. i subscribe your policy then n then not download..plzzz send me mail on this email arpita@omexinfotech.com

  • huzoorbux

    Might be file_get_contents not allowed on your hosting you try to curl file get content from here: http://www.phpgang.com/replace-file_get_contentsfopen-with-curl_30.html

    • Engfar Srisombat

      error : The request is invalid because the app is configured as a desktop app
      how to solve?

  • Gurubaksh

    Please anyone tell me how to get publish steam permission i am using this
    define(“PERMISSIONS”, “publish_stream,email,user_about_me,user_birthday,user_website “);

    $loginURL = $facebook->getLoginUrl(array( “scope” => PERMISSIONS, ‘redirect_uri’ => REDIRECT_URL));

    But all time facebook ignore my PERMISSIONS

    • huzoorbux

      You need to send request to facebook to review your application permission and allow your app for these permissions https://developers.facebook.com/docs/apps/review.

      • Gurubaksh

        Thanks huzoorbux,

        huzoorbux, I have send request two times to facebook but still i did not get any reply form them, Will they Email me. And One question how long should I wait for reply.

        • huzoorbux

          The review time estimate will range between 3 to 7 business days.

          • Gurubaksh

            Thanks I will wait till that time, Thanks for your help. I will keep in touch with your for more help

          • Gurubaksh

            Dear Sir,

            Already 6 days has gone but still I do not get permissions in my account. Please suggest me what to do. Sir Please give the exact link for fill up to contact facebook. I have missed the out the link what I used last time. Please give the exact link. I am not finding it.

  • Vikas

    Is this works for graph api v2.1 ?

  • vikas

    Notice: Undefined variable: message in index.php on line 90 . Have you changed the source code ?as I am getting above error after facebook login .

  • Nilesh Daldra

    where can i find facebook.php and base_facebook.php files?????

  • Tommy Rah

    Demo is not working .
    Fatal error: Uncaught OAuthException: (#200) Permissions error
    thrown in
    /hermes/waloraweb002/b2497/pow.huzoorbuxcom/htdocs/phpgang/demo/post-into-a-facebook-page-with-php-using-graph-api/src/base_facebook.php
    on line 1243

    • Nguyen Long

      me too. help me ๐Ÿ™

  • Ajith

    Hi,
    Your code is working . This code only working in Main app created user account. I mean the the main page admin can list all pages. But the sub admin users can’t access the pages. Is it any issue in my app..
    Please help me for this issue

  • vfgvfgvbgfv

    grrgvfc

  • mukund

    Latest Api is not Support this Code..So Plz put Latest Api code….

  • kailashkumar

    I was successfully paste the folder in my online server.
    But, i got following error:

    Given URL is not permitted by the Application configuration: One or more
    of the given URLs is not permitted by the App’s settings. It must match
    the Website URL or Canvas URL, or the domain must be a subdomain of one
    of the App’s domains.

  • q-think the welcome

    Nice,
    But, How I can Download your this code?
    I have subscribe your Feed but still can’t download?

    Please help,,,thanks

    • Naiyar Azam

      Please help,,,thanks

  • Mehul Kumbhani

    Hi,

    It is working fine but i have one issue..

    When i am posting it is showing like i have posted to my page.

    Like ::: — “My Name -> My Page Name”
    But i wanted it to show like my page has posted that post on the page timeline.

    I tried specifing “from” param also but still it is posting as same.
    Sir, plz guide me what i am missing.

  • Parvez

    When i use this code and run it after login it give error like this: Fatal error: Uncaught OAuthException: (#200) Permissions error
    thrown in /home/websocialite/public_html/post-to-fb-wall/src/base_facebook.php on line 1243

    • dfg dfgdf

      cvbcb cbvb

  • Naiyar Azam

    thanks

  • michael

    it’s not show me my page… the select tag is null

    • Vivek

      Have u got any solution for this ??

  • Vivek

    My select tag is null even if i have 5 pages in my account. Please give me solution.

  • Vivek

    Can i post multiple image in single post using this same demo….?? If yes, then how please ??

  • khubbab

    Hi script cannot be downloaded do you have newer version or can you send me the script at khubbaib@gmail.com

  • Van Muoi

    Hi script can’t working. Check it, please. Thanks.

  • Komal Garg

    hii can u please send me the code to auto share post on fb at komalgarg052@gmail.com
    Thanks in advance

  • Not found select fanpage

  • Shaan Ansari

    My client need his facebook post on website is their any api