Back

Step by Step Guide to Building a Facebook Custom Audiences API Integration in PHP

Aug 2, 20246 minute read

Hey there, fellow developer! Ready to supercharge your Facebook marketing efforts? Let's dive into building a Custom Audiences API integration using PHP. We'll be using the facebook/php-business-sdk package, so buckle up and let's get coding!

Introduction

Custom Audiences are a game-changer for targeted advertising on Facebook. By integrating the API, you'll have programmatic control over audience creation and management. Trust me, your marketing team will love you for this!

Prerequisites

Before we jump in, make sure you've got:

  • A PHP environment (you're a PHP dev, right?)
  • Composer installed (because who doesn't love dependency management?)
  • A Facebook Business Manager account (if you don't have one, go create it now!)
  • An app with the necessary permissions (you'll need ads_management at minimum)

Got all that? Great! Let's move on.

Installation

First things first, let's get that SDK installed:

composer require facebook/php-business-sdk

Easy peasy, right?

Authentication

Now, let's get you authenticated:

use FacebookAds\Api; $app_id = 'YOUR_APP_ID'; $app_secret = 'YOUR_APP_SECRET'; $access_token = 'YOUR_ACCESS_TOKEN'; Api::init($app_id, $app_secret, $access_token);

Pro tip: Never hardcode these values in production. Use environment variables instead!

Creating a Custom Audience

Let's create our first audience:

use FacebookAds\Object\CustomAudience; use FacebookAds\Object\Fields\CustomAudienceFields; $audience = new CustomAudience(null, 'YOUR_AD_ACCOUNT_ID'); $audience->setData([ CustomAudienceFields::NAME => 'My Awesome Audience', CustomAudienceFields::DESCRIPTION => 'Created via API', CustomAudienceFields::CUSTOMER_FILE_SOURCE => 'USER_PROVIDED_ONLY', ]); $audience->create(); echo "Audience created with ID: " . $audience->id . "\n";

Boom! You've just created your first Custom Audience via API. Feel the power!

Adding Users to Custom Audience

Now, let's add some users:

$users = [ '[email protected]', '[email protected]', // Add more emails here ]; $hashed_users = array_map('hash_email', $users); $audience->addUsers($hashed_users, CustomAudience::SCHEMA_EMAIL); function hash_email($email) { return hash('sha256', strtolower(trim($email))); }

Remember, always hash those emails before sending them to Facebook!

Removing Users from Custom Audience

Need to remove some users? No problem:

$audience->removeUsers($hashed_users, CustomAudience::SCHEMA_EMAIL);

Updating Custom Audience

Want to change something about your audience? Here's how:

$audience->setData([ CustomAudienceFields::NAME => 'My Even More Awesome Audience', ]); $audience->update();

Retrieving Custom Audience Information

Curious about your audience details? Let's fetch them:

$audience = new CustomAudience('AUDIENCE_ID'); $audience->read([ CustomAudienceFields::NAME, CustomAudienceFields::APPROXIMATE_COUNT, ]); echo "Audience Name: " . $audience->{CustomAudienceFields::NAME} . "\n"; echo "Approximate Count: " . $audience->{CustomAudienceFields::APPROXIMATE_COUNT} . "\n";

Error Handling and Best Practices

Always wrap your API calls in try-catch blocks:

try { // Your API call here } catch(\FacebookAds\Exception\Exception $e) { echo 'Facebook API Error: ' . $e->getMessage(); }

And don't forget about rate limits! Be nice to the API, and it'll be nice to you.

Testing and Validation

Always verify your changes:

$audience = new CustomAudience('AUDIENCE_ID'); $audience->read([CustomAudienceFields::APPROXIMATE_COUNT]); echo "Audience size: " . $audience->{CustomAudienceFields::APPROXIMATE_COUNT} . "\n";

Conclusion

And there you have it! You're now a Facebook Custom Audiences API ninja. Remember, with great power comes great responsibility. Use these audiences wisely, and may your CTRs be ever in your favor!

Want to dive deeper? Check out the official Facebook Marketing API documentation. Happy coding!