Hello,
In this quick blog we will quickly go through how to publish a post on Facebook Page with graph API from your Laravel Application.
Step 1 : Create Facebook App
Login to https://developers.facebook.com/ with your Facebook account and create an app.
Now go to Settings -> Basic of your Facebook app and copy app id and app secret.
In this quick blog we will quickly go through how to publish a post on Facebook Page with graph API from your Laravel Application.
Step 1 : Create Facebook App
Login to https://developers.facebook.com/ with your Facebook account and create an app.
Now go to Settings -> Basic of your Facebook app and copy app id and app secret.
Step 2 : Install Facebook PHP SDK
Run following command to install PHP SDK in your laravel application.
composer require facebook/graph-sdk
Step 3: Get Exchange Token from Facebook App
Go to Facebook Graph API Explorer. Here is the link https://developers.facebook.com/tools/explorer/
From here first select your application and then select the page for Page Access Token
When you are generating this token please select graph API version 2.2 and select the following permissions.
publish_actions,
manage_pages,
pages_show_list,
publish_pages,
public_profile
Step 4 : Generate Access Token
From the exchange token generated in Step 3, Get the access token.
$url = 'https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token&client_id= YOUR_APP_ID&client_secret= YOUR_APP_SECRET&fb_exchange_token=YOUR_TOKEN';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
$output = json_decode($result);
$access_token = $output->access_token;
Step 5 : Call Facebook Graph API to Send Post to Page
Create SDK object
$fb = new \Facebook\Facebook([
'app_id' => 'YOUR_APP_ID',
'app_secret' => 'YOUR_APP_SECRET',
'default_graph_version' => 'v2.2',
]);
Generate Payload
$linkData = [
'link' => YOUR_LINK,
'message' => YOUR_TITLE
];
Call Graph API.
$pageAccessToken =$access_token;
try {
$response = $fb->post('/me/feed', $linkData, $pageAccessToken);
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: '.$e->getMessage();
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: '.$e->getMessage();
}
$graphNode = $response->getGraphNode();
Now check your Facebook page there will be post on your Facebook page. Hope this helps you.
No comments:
Post a Comment