First, edit the
composer.json
file in the project’s root folder to include the Facebook SDK:
{
"require": {
"facebook/php-business-sdk": "3.1.*"
}
}
Next, run composer update
in the terminal to pull the SDK into the vendor folder:
php composer.phar install --no-dev
If you do not have composer.phar
in your Laravel project, download it from Facebook Business SDK for PHP.
Now, we will use the Facebook SDK within our app as a command. First, set up the app_id
, app_secret
, and default_graph_version
, which are required for making requests to the Facebook API. You can obtain the app_id
and app_secret
from your Facebook App Settings.
Once you have these credentials, edit the .env
file in the project’s root folder and add the following at the end:
FB_ADS_APP_ID="XXXXXXXXX"
FB_ADS_APP_SECRET="XXXXXXXXXXXXX"
FB_DEFAULT_GRAPH_VERSION=v3.2
FB_ACCESS_TOKEN="XXXXXXXXXXX"
Replace the XXXXXXXXX
placeholders with the values provided to you. Note that the variable names are custom; you can name them as you prefer. Next, create a configuration file to use Laravel’s config()
helper function to retrieve these values anywhere in the app. Create facebook-ads.php
in the config
folder and add the following:
return [
'app_id' => env('FB_ADS_APP_ID', null),
'app_secret' => env('FB_ADS_APP_SECRET', null),
'default_graph_version' => env('FB_DEFAULT_GRAPH_VERSION', 'v2.8'),
];
Now, create a class FacebookAdsService.php
using the following command:
php artisan make:console FacebookAdsService
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class FacebookAdsService extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'facebook_sdk:service';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Facebook ads api for campaign service, adset etc';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
// Your code goes here
}
}
Write your code to retrieve Facebook campaign and reporting data in the handle() function.
To run this command in the terminal, register it in Kernel.php
located at app/Console/Kernel.php
. Add Commands\FacebookAdsService::class
to the $commands
array as follows:
protected $commands = [
Commands\FacebookAdsService::class
];
Now, everything is set up to retrieve campaign or adset reporting from the Facebook Marketing API.
Add the following classes at the top of the FacebookAdsService class to use for retrieving campaign data:
use FacebookAds\Object\AdAccount;
use FacebookAds\Api;
use FacebookAds\Object\AdSet;
use FacebookAds\Object\Fields\AdSetFields;
Write the code to retrieve Facebook campaign data in the handle() function as follows:
First, instantiate an API object, which requires a valid access token:
$app_id = env('FB_ADS_APP_ID');
$app_secret = env('FB_ADS_APP_SECRET');
$access_token = env('FB_ACCESS_TOKEN');
Api::init($app_id, $app_secret, $access_token);
$api = Api::instance();
Get Ad Account Details
$account_id = 'act_XXXXXXXX';
$account = new AdAccount($account_id);
$account->read();
Get All Campaigns
$fields = array(
'name',
'objective',
'start_time',
);
$params = array(
'effective_status' => array('ACTIVE', 'PAUSED'),
);
$r = json_encode($account->getCampaigns($fields, $params)->getResponse()->getContent(), JSON_PRETTY_PRINT);
Get All AdSets
$fields = array(
AdSetFields::ID,
AdSetFields::NAME,
AdSetFields::STATUS,
AdSetFields::START_TIME,
AdSetFields::END_TIME,
);
$r = json_encode($account->getAdSets($fields)->getResponse()->getContent(), JSON_PRETTY_PRINT);
Get Ad Insights
You can customize fields and parameters as needed. Refer to Facebook Ads Insights Parameters and Fields for more details.
$fields = array(
'adset_id',
'adset_name',
'impressions',
'clicks',
'ctr',
'spend',
);
$params = array(
'time_increment' => '1',
'date_preset' => 'lifetime',
'breakdowns' => array(
'hourly_stats_aggregated_by_advertiser_time_zone',
),
'sort' => array(
'date_start_ascending',
),
);
$cmp_id = 'XXXXXXXXX';
$data = json_encode((new AdSet($cmp_id))->getInsights($fields, $params)->getResponse()->getContent(), JSON_PRETTY_PRINT);
Run the command in the terminal:
php artisan facebook_sdk:service
The output will be displayed in the terminal.
If you encounter any issues setting up the Facebook SDK with Laravel, please leave a comment.
First, Thank alot for this blog. As of now this is working when the app and ad_id are with same fb profile. How to get ad account details for any other fb profile users using my app? Thanks.
Its not possible to get all ad account of other users till the time he or she doesn't give you permission to access their ad account. Thanks.