|

|  How to Implement Facebook Marketing API to Create Custom Audiences in PHP

How to Implement Facebook Marketing API to Create Custom Audiences in PHP

October 31, 2024

Learn to use Facebook Marketing API with PHP to create custom audiences. Step-by-step guide for developers aiming to boost campaign results efficiently.

How to Implement Facebook Marketing API to Create Custom Audiences in PHP

 

Facebook Marketing API Overview

 

  • The Facebook Marketing API allows businesses to automate their advertising processes, including the creation of custom audiences, ad management, and insights extraction.
  •  

  • Custom Audiences enable you to target specific groups of users based on data you already have.

 

Setting Up the Environment

 

  • Make sure you have the Facebook SDK for PHP installed in your project. You can install it using Composer.
  •  

  • Ensure you have the necessary access token with permissions to manage and create Custom Audiences.

 

composer require facebook/graph-sdk

 

Initialize the Facebook SDK

 

  • Start by initializing the Facebook SDK with your app credentials. This involves setting your app id, app secret, and default access token.
  •  

  • These credentials are necessary for authenticating requests to the Facebook API.

 

require 'vendor/autoload.php';

use Facebook\Facebook;

$fb = new Facebook([
  'app_id' => 'YOUR_APP_ID',
  'app_secret' => 'YOUR_APP_SECRET',
  'default_graph_version' => 'v12.0',
  'default_access_token' => 'YOUR_ACCESS_TOKEN',
]);

 

Create a Custom Audience

 

  • Use the SDK to interact with the API and create a custom audience. You will need the ad account ID and any required data for audience creation.
  •  

  • Custom audiences require a "subtype", such as 'CUSTOM', and a "name" for identification.

 

try {
  $response = $fb->post(
    '/act_<AD_ACCOUNT_ID>/customaudiences',
    [
      'name' => 'My Custom Audience',
      'subtype' => 'CUSTOM',
      'description' => 'People who bought on my website'
    ]
  );

  $graphNode = $response->getGraphNode();
  echo 'Created Audience ID: '. $graphNode['id'];
} catch(Facebook\Exceptions\FacebookResponseException $e) {
  echo 'Graph returned an error: ' . $e->getMessage();
  exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
  echo 'Facebook SDK returned an error: ' . $e->getMessage();
  exit;
}

 

Upload User Data to Custom Audience

 

  • Once the audience is created, you can add users by their email addresses, phone numbers, or other identifiers.
  •  

  • Create a payload with the user data you want to add and execute the request.

 

try {
  $data = [
    'schema' => ['EMAIL'],
    'data' => [
      ['example@example.com'],
      ['another@example.com']
    ],
  ];

  $response = $fb->post(
    '/<CUSTOM_AUDIENCE_ID>/users',
    ['payload' => json_encode($data)]
  );

  $graphNode = $response->getGraphNode();
  echo 'Users added to Audience: ' . $graphNode['success'];
} catch(Facebook\Exceptions\FacebookResponseException $e) {
  echo 'Graph returned an error: ' . $e->getMessage();
  exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
  echo 'Facebook SDK returned an error: ' . $e->getMessage();
  exit;
}

 

Best Practices

 

  • Handle errors gracefully to understand what part of your request may have failed.
  •  

  • Regularly verify your rate limits and API call usage to avoid interruptions.
  •  

  • Make sure to comply with Facebook’s data and privacy policies when handling user data.