How to post status and photos to facebook fan page using Graph API in PHP

How to post status and photos to facebook fan page using Graph API in PHP
Reading Time: 4 minutes

hello guys, today we will learn how to upload photos and status to Facebook fan page. You need access token and Page ID to post anything on your page. We will discuss everything in detail. After doing a long research, I got the solution for this. I would like to share it with you all so that you can save your time.

First create Facebook App, you can follow this tutorial to learn creating App in Facebook. 

Now, you have to give two permission to app. manage_pages and publish_pages. You can see this tutorial creating App in Facebook to learn how to give permission to App.

adding_permissionGet Page ID and ACCESS_TOKEN.

You can get Page ID by opening your page about section. You can see below image.

facebook_page_id

Now we have to get access token.

Go to this link -> https://developers.facebook.com/tools/explorer/145634995501895/
On the top right side, click on Graph API Explorer dropdown, Please select your page in which you want to post status or photos.

select_page

after selecting the page, now click on get user access toekn button from dropdown. Please check below image.
get_user_token

now pop-up will open, you have to select publish_action option and then click on get access token button. Give permission to App to access your account, It will get user access toekn.

select_publish_action

Now select your page in dropdown, to get page_access_token. Please check below image.select_for_page_access_token

when you select your page, then you get the short lived page access token. This token will expire in about an hour. I will tell you now how to get permanent access token.

short_lived_access_token

To get permanent access token, go to this link -> https://developers.facebook.com/tools/debug/accesstoken/
Paste your short lived access toekn here and it will show you expiry time. Now click on bottom button Extended Access Token, to get permanent access token. Copy this permanent access token, you will need in Graph API call.

extended_access_token

Now we will use Graph API Calls to post status and photos on facebook.

Steps to Upload Photos to your Fan Page.

Use below code to post photos to fan page, fill details like, access_token, Page Id which we got from above steps.
To post image, you need to fill URL parameter, you can give any caption also if you want. URL should be link of the image.
To see full list of parameter. Please follow offical doc of fb -> https://developers.facebook.com/docs/graph-api/reference/page/photos/

we are using CURL POST method to post data on Graph API. So, to make this working, you need to install CURL on your server.

Steps to Upload Status to your Fan Page.

Use below code to post status to fan page, fill details like, access_token, Page Id which we got from above steps.
To post status, you need to fill MESSAGE parameter, you can give any link also if you want.
To see full list of parameter. Please follow offical doc of fb -> https://developers.facebook.com/docs/graph-api/reference/page/photos/

we are using CURL POST method to post data on Graph API. So, to make this working, you need to install CURL on your server.

Leave a Reply

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