Auto Post On Facebook Using PHP
Last Updated : Jul 1, 2023
IN - PHP Facebook API | Written & Updated By - Ashish
In this tutorial we will show you how to post automatically on Facebook using PHP and Facebook Graph API.
You just have to create a Facebook App and get your Access Token and need a Facebook PHP SDK to help to post on Facebook.You may also like login with facebook using PHP.
To Post On Facebook Automatically It Takes Only Three Steps:-
- Create A Facebook App
- Get Facebook Access Token
- Make a PHP file and define methods to Post On Facebook
Step 1. Create A Facebook App
Before creating Facebook App you need to download Facebook PHP SDK and it requires PHP 5.2.0 or higher for PHP curl extension.
Now we have to create Facebook App go to https://developers.facebook.com/apps and follow the instructions.
Step 2. Get Facebook Access Token
To get Access Token you have to set callback URL for the Facebook app to get the result. Then authorize the app for permissions on https://developers.facebook.com/docs/facebook-login/permissions/v2.2 and then get the access Token.
Make sure renew your Facebook Token After 60 days because Facebook Access token will expires in 60 days.
Step 3. Make a PHP file and define methods to Post On Facebook
We make a PHP file and save it with a name autopost.php
<?php require_once("FacebookSDK/facebook.php"); class FacebookApi { var $consumer; var $token; var $method; var $http_status; var $last_api_call; var $callback; var $connection; var $access_token; function __construct($data) { $config = array(); $config['appId'] = $data['consumer_key']; $config['secret'] = $data['consumer_secret']; $this->connection = new Facebook($config); } function share($title, $targetUrl, $imgUrl, $description, $access_token) { $this->connection->setAccessToken($access_token); $params["access_token"] = $access_token; if(!empty($title)) { $params["message"] = $title; $params["name"] = $title; } if(!empty($targetUrl)) { $params["link"] = $targetUrl; } if(!empty($imgUrl)) { $params["picture"] = $imgUrl; } if(!empty($description)) { $params["description"] = $description; } // post to Facebook try { $ret = $this->connection->api('/me/feed', 'POST', $params); } catch(Exception $e) { $e->getMessage(); } return true; } function getLoginUrl($params) { return $this->connection->getLoginUrl($params); } function getContent($url) { $ci = curl_init(); /* Curl settings */ curl_setopt($ci, CURLOPT_URL, $url); curl_setopt($ci, CURLOPT_RETURNTRANSFER, true); curl_setopt($ci, CURLOPT_HEADER, false); curl_setopt( $ci, CURLOPT_CONNECTTIMEOUT, 10 ); curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, false); $response = curl_exec($ci); curl_close ($ci); return $response; } } ?>
<?php $access_token = Your_Access_Token; $facebookData = array(); $facebookData['consumer_key'] = Your_key; $facebookData['consumer_secret'] = Your_Secret; $title = Post_Title; $targetUrl = Post_URL; $imgUrl = Image_URL; $description = Post_Description; $facebook = new FacebookApi($facebookData); $facebook->share($title, $targetUrl, $imgUrl, $description, $access_token); ?>
After using above code you post will be posted automatically on your Facebook profile automatically. You may also like get facebook like, share and comment using PHP.
Thats all, this is how to Post On Facebook Automatically using PHP. You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.
I hope this tutorial on auto post on facebook helps you and the steps and method mentioned above are easy to follow and implement.