Post on Facebook page with Graph API and Laravel
In this article, we will describe in detail the process of posting on Facebook pages by using Laravel with Graph API. Posts can include text and/or images.
The Facebook publishing flow
Before we can publish on the Facebook page we have to go through the steps illustrated in the following drawing.
First, we should log into Facebook Graph API. To do that, we need to create a Facebook developer application and find the APP ID and APP SECRET that will be used for the authentication.
By using those credentials, we will get a user access token.
After that, we should send a GET request to a Facebook Graph API to get all user pages. Inside request headers we should include a user access token.
If we did everything right, we should get a list of the pages that includes the data that is required for publishing posts — pages access tokens and the IDs.
After we get a page access token and id we can do POST request to Facebook Graph API and finally publish desired content.
Create Facebook application
Let’s create an application on the Facebook Developers panel.
https://developers.facebook.com/apps
For the App type select Business. Copy your APP ID and APP SECRET (Settings -> Basics in the sidebar).
Add Facebook Login Product
Go to Add Product. Choose Add Facebook Login.
Choose WEB for the platform.
Then go to Permissions and Features and ask for permissions:
Add Valid OAuth Redirect URIs: https://yourapp.test/auth/facebook/callback
Note: To get https url you can use ngrok for testing.
The last step in the Facebook developer application is to allow cross-domain share redirects.
Login to the Facebook Graph API from the Laravel application
Install Facebook SDK for PHP package
This package is linked in the official Facebook Graph documentation: https://developers.facebook.com/docs/graph-api/
You just need to run the following command:
composer require facebook/graph-sdk
Store Keys in the config file
Create config/providers.php file.
Add your application ID and application secret to the .env file.
FACEBOOK_APP_ID=xxxxxxxxx
FACEBOOK_APP_SECRET=xxxxxxxxxxxxxxxxxxx
Create a Facebook repository and load our app configuration
Next, let’s create a FacebookRepository. We will separate the logic we use to communicate with Facebook into a separate file, so that we will have cleaner code and better organization.
The code could be better organized, but we don’t want to lose focus on the subject in this article, so there is no emphasis on that.
Let’s create a repository file:
app/Logic/Providers/FacebookRepository.php;
Before sending a requests to the Graph API, we need to load our app configuration into the Facebook\Facebook
service.
Add __construct method and $facebook property in the FacebookRepository.
Get User Access Token
We should register two routes. One for redirecting to the Facebook auth screen and one for handling providers callback.
For this purpose, we need to create a Social Controller.
Now, let’s add main logic into FacebookRepository.
Redirect to provider
We are using getRedirectLoginHelper helper and getLoginUrl method from the package.
It’s important to send pages_manage_posts and pages_read_engagement permissions when generating login URL.
When we redirect a user to this URL, he will get a Facebook authorization page where he can select pages and grant permissions to our application.
Handling Facebook Callback and get Facebook Pages
In the handle callback method, we will get a long-lived access token. We use this token to authenticate our users and retrieve their previously selected pages.
Get Facebook pages
Now, when we have a user access token we can get selected pages.
Facebook Graph endpoint for getting pages is /me/accounts.
The response within the method is an array of data that can be useful for representing pages to your users.
As we said earlier, access token and id are required for Facebook page publishing.
Publishing on the Facebook Page
Posting just text is pretty simple.
Posting images with text as a description is a bit complicated.
First, we need to upload images to the Facebook, then we should send IDs of uploaded images as attachments.
This code should be slightly refactored. Merge posting logic into one method and here we have logic for posting with or without images depending on $images variable.
That’s it!
Facebook Repository overview:
Thanks for reading this article. Happy coding! :)