Auto Post on Facebook page with php using Graph Api
Today, We want to share with you Auto Post on Facebook page with php using Graph Api.In this post we will show you post on facebook page using php, hear for Automatically post to a Facebook page using the Facebook PHP API we will give you demo and example for implement.
In this post, we will learn about Automatically Post on Facebook page using PHP API with an example.
IMP NOTE: as of 3rd August 2018, This is no longer time possible to config set up devloper account an app for the purpose of PHP auto post on facebook page…as per the some security based Disallowed Usage under section under the main checkbox tick ‘publish_pages’ marked get permission some R & D on the Facebook Permissions Reference Your Facebook page.
The Facebook graph API lets start We post to pages We administrate. In the post simple way to run cron We are going to steps how How to Auto Post on Facebook Page with PHP, using latest API version 5 of the useful SDK for PHP libs.
The More description was to step by step a source code which automatically facebook Page posted any newly puted CMS WordPress custom type posts to my Tutorials Blogs Facebook page. There for 100% working Source code how it went:
1. Create a Facebook ‘App’:
Firstly We should to Make a simple create a Facebook App, which is some required in order to use the web API. Login in to owned Create Facebook Developers account as well as click the ‘Add a New App’ link as display below:
Enter simple owned App name Like(testDemo) and then click ‘create’. Under some basic ‘settings’, I will should to put a simple choose‘website platform’ and enter the your domain name Like (pakainfo.com) where select this main script will sit. For some security purpose, This is also worth putting the white IP address of the hosting server where owned web sites in the select ‘Server IP Whitelist’ field. We do so under select ‘advanced settings’.
Step 2. Install the Facebook PHP SDK:
In this source code We will assume owned source code will sit in a ‘facebook’ Directory in the root directory of owned site. Firstly, create a blank ‘index.php’ file within owned ‘facebook’ Directory. After that, We should to include or main part top section the Facebook SDK for PHP.
Facebook supported We include the simple all the PHP Libs SDK in owned new project To using simple install and update your libs using Composer, And any other ways We can also download manually PHP Libs do so. In this source code all the You Will manually include PHP API the SDK, There for We firstly should to download the php-graph-sdk.
and then Unzip the all files and put the simple here display below ‘src’ Directory in owned ‘facebook’ Directory. Then include the PHP libs SDK in owned index.php file:
//put your facebook libs path and set define('FACEBOOK_SDK_V4_SRC_DIR', __DIR__.'/src/Facebook/'); require_once(__DIR__.'/src/Facebook/autoload.php');
Step 3. Enter Your App’s settings:
And then, We should to enter owned project ‘App ID’ and ‘App secret’ generated keys into the source code below. We can fetch owned App’s Like ‘App ID’ and ‘App secret’go to the under the display ‘dashboard’ tab:
$fb = new Facebook\Facebook([ 'app_id' => 'xxxxxxxxxx', // your app Id put here 'app_secret' => 'xxxxxxxxxx', // your secret key put here 'default_graph_version' => 'v2.2', ]);
Step 4. Get a non-expiring Access Token:
After that, if We want to set here automatically post or data content and Images to owned Facebook page like I shoulded to… I am gonna to should to fetch a non-expiring Access Token(60 days valid) for owned page. so you lets get, visit the main Facebook Graph API Explorer page. We will display an select ‘Application’ button in the choose top right as display below screen shot:
As We can display, actively this is set to here ‘Graph API Explorer’. Select owned Facebook App instead. After that simple trigger or click the ‘get token’ button which We can also display in the screen shote above. choose a new ‘Get User Access Token’ and make sure the ‘manage_pages’ ’publish_pages’, and ‘publish_actions’ checkbox click permissions, then click the get ‘Get Access Token’ button. and Lastly click the fetch ‘get token’ button try again and choose owned Facebook page. We now have owned facebook Access Token which we should to simple convert into the validate non-expiring get Access Token. To do next things, pass owned facebook App ID, with facebook App Secret and simple click Access Token into the following below URL:
Facebook Developers Account permissions are ticked For Auto Post.
- Graph API Explorer
- ’publish_pages’
- ‘manage_pages’
- ‘publish_actions’
- ‘Get Access Token
appid&client_secret=appsecret&fb_exchange_token=accesstoken
And then Your copy the new generated token provided.
Step 5. The Source code that does the ‘posting’:
After That, the source code that actually facebook posts the Data content to the Facebook page. In my Task We wanted to simple random through select any WordPress posts Title and post a link and post Images for any newly added posts onto simple my clients Facebook page. Within my CMS WordPress article loop was the all autoload important source code below:
//Post some property to Facebook in PHP $linkData = [ 'link' => 'www.pakainfo.com', // Example 'www.ownedurl.com' 'message' => 'Hello world in Facebook Page using PHP' //Your message here ]; $pageAccessToken ='Enter_owned_nonexpiring_access_token'; try { $results = $fb->post('/me/feed', $linkData, $pageAccessToken); } catch(Facebook\Exceptions\FacebookResponseException $e) { echo 'Generated Graph returned an error: '.$e->getMessage(); exit; } catch(Facebook\Exceptions\FacebookSDKException $e) { echo 'Some Display Facebook SDK returned an error: '.$e->getMessage(); exit; } $graphNode = $results->getGraphNode();
There We go, if We now run this file it will post owned content to owned Facebook page.
Imp Note: We will display the post showing on the Facebook page Like (pakainfo My page), So no-one else will as We have facebook Developers not set owned App ‘live’! To do so, go back to owned Facebook Developers account and select owned main developers App. Under relavnts the ‘Status & Review’ choose tab tick the privacy checkbox at the top of the main page and then to the text ‘Do We want to make this web app as well as all its live Advantages available to the simple Make general public?’.
Step 6. The automation:
Lastley all We shoulded was the PHP auto post on facebook page. To Post this all We shoulded to do was set up a Cpanel cron job set To simple some Days that ran automatically the index.php file run every 2 amount of days/hours or months… and We digital Marketing and Save Time there was the Auto Post On Facebook Page Using PHP.
Read :
Summary
You can also read about AngularJS, ASP.NET, VueJs, PHP.
I hope you get an idea about How to Send Post Automatically on Facebook Page with PHP.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.