2017 © Pedro Peláez
 

library php-ads-sdk

PHP SDK for Facebook Business

image

facebook/php-ads-sdk

PHP SDK for Facebook Business

  • Saturday, July 28, 2018
  • by pruno
  • Repository
  • 100 Watchers
  • 374 Stars
  • 832,799 Installations
  • PHP
  • 9 Dependents
  • 0 Suggesters
  • 298 Forks
  • 123 Open issues
  • 44 Versions
  • 17 % Grown

The README.md

Facebook Business SDK for PHP

Packagist License Build Status, (*1)

Introduction

The Facebook Business SDK is a one-stop shop to help our partners better serve their businesses. Partners are using multiple Facebook API's to serve the needs of their clients. Adopting all these API's and keeping them up to date across the various platforms can be time consuming and ultimately prohibitive. For this reason Facebook has developed the Business SDK bundling many of its APIs into one SDK to ease implementation and upkeep. The Business SDK is an upgraded version of the Marketing API SDK that includes the Marketing API as well as many Facebook APIs from different platforms such as Pages, Business Manager, Instagram, etc., (*2)

Quick Start

Business SDK Getting Started Guide, (*3)

Pre-requisites

Register An App

To get started with the SDK, you must have an app registered on developers.facebook.com., (*4)

To manage the Marketing API, please visit your App Dashboard and add the Marketing API product to your app., (*5)

IMPORTANT: For security, it is recommended that you turn on 'Require App Secret' in your app's Settings->Advanced page., (*6)

Obtain An Access Token

When someone connects with an app using Facebook Login and approves the request for permissions, the app obtains an access token that provides temporary, secure access to Facebook APIs., (*7)

An access token is an opaque string that identifies a User, app, or Page., (*8)

For example, to access the Marketing API, you need to generate a User access token for your app and ask for the ads_management permission; to access Pages API, you need to generate a Page access token for your app and ask for the manage_page permission., (*9)

Refer to our Access Token Guide to learn more., (*10)

For now, we can use the Graph Explorer to get an access token., (*11)

Installation

The Facebook Business SDK requires PHP 8.0 or greater., (*12)

Composer

The Facebook Business SDK uses composer to manage dependencies. Visit the composer documentation to learn how to install composer., (*13)

Execute the command below in your project root:, (*14)

composer require facebook/php-business-sdk

This SDK and its dependencies will be installed under ./vendor., (*15)

Alternatives

This repository is written following the psr-4 autoloading standard. Any psr-4 compatible autoloader can be used., (*16)

Usage

Api main class

The FacebookAds\Api object is the foundation of the Business SDK which encapsulates a FacebookAds\Session and is used to execute requests against the Graph API., (*17)

To instantiate an Api object you will need a valid access token:, (*18)

use FacebookAds\Api;

// Initialize a new Session and instantiate an Api object
Api::init($app_id, $app_secret, $access_token);

// The Api object is now available through singleton
$api = Api::instance();

Once instantiated, the Api object will allow you to start making requests to the Graph API., (*19)

Fields names

Due to the high number of field names in the Graph API existing objects, in order to facilitate your code maintainability, enum-like classes are provided. These files are stored under the FacebookAds/Object/Fields directory. You can access object properties in the same manner you would usually do in php:, (*20)

use FacebookAds\Object\AdAccount;

$account = new AdAccount();
$account->name = 'My account name';
echo $account->name;

or using the enums:, (*21)

use FacebookAds\Object\AdAccount;
use FacebookAds\Object\Fields\AdAccountFields;

$account = new AdAccount();
$account->{AdAccountFields::NAME} = 'My account name';
echo $account->{AdAccountFields::NAME};

Object classes

Facebook Ads entities are defined as classes under the FacebookAds/Object directory., (*22)

Read Objects

use FacebookAds\Object\AdAccount;

$account = (new AdAccount($account_id))->getSelf();

For some objects, the Ads API doesn't return all available fields by default. The first argument of the object's read method is an array of field names to be requested., (*23)

use FacebookAds\Object\AdAccount;
use FacebookAds\Object\Fields\AdAccountFields;

$fields = array(
  AdAccountFields::ID,
  AdAccountFields::NAME,
);

$account = (new AdAccount($account_id))->getSelf($fields);

Requesting a high number of fields may cause the response time to visibly increase, you should always request only the fields you really need., (*24)

Create Objects

use FacebookAds\Object\AdSet;
use FacebookAds\Object\AdAccount;
use FacebookAds\Object\Fields\AdSetFields;

$account_id = 'act_123123';
$campaign_id = '123456';

$account = new AdAccount($account_id);
$adset = $account->createAdSet(
    array(),
    array(
      AdSetFields::NAME => 'My Test AdSet',
      AdSetFields::CAMPAIGN_ID => $campaign_id,
      AdSetFields::DAILY_BUDGET => 150,
      AdSetFields::START_TIME => (new \DateTime("+1 week"))->format(\DateTime::ISO8601),
      AdSetFields::END_TIME => (new \DateTime("+2 week"))->format(\DateTime::ISO8601),
      AdSetFields::BILLING_EVENT => 'IMPRESSIONS',
      AdSetFields::TARGETING => array('geo_locations' => array('countries' => array('US'))),
      AdSetFields::BID_AMOUNT => '1000',
    )
);

echo $adset->id;

Update Objects

use FacebookAds\Object\AdSet;
use FacebookAds\Object\Fields\AdSetFields;

$ad_set_id = '123456';

$set = new AdSet($ad_set_id);
$fields = array(
);
$params = array(
  AdSetFields::NAME => 'My new AdSet name',
);
$set->updateSelf($fields, $params);

Delete Objects

use FacebookAds\Object\AdSet;

$ad_set_id = '123456';

$set = new AdSet($ad_set_id);
$set->deleteSelf();

Cursors

Since the release of the Facebook Graph API 2.0, pagination is handled through cursors. Here cursors are defined as in \FacebookAds\Cursor. Cursors are generally returned from connection methods:, (*25)

use FacebookAds\Object\AdAccount;
use FacebookAds\Object\Fields\CampaignFields;

$account = new AdAccount('<ACT_ID>');
$cursor = $account->getCampaigns(['id','name']);

// Loop over objects
foreach ($cursor as $campaign) {
  echo $campaign->{CampaignFields::NAME}.PHP_EOL;
}

// Access objects by index
if ($cursor->count() > 0) {
  echo "The first campaign in the cursor is: ".$cursor[0]->{CampaignFields::NAME}.PHP_EOL;
}

// Fetch the next page
$cursor->fetchAfter();
// New Objects will be appended to the cursor

Implicit Fetching

Whenever all object connected to a parent are required (carelessly from the number of HTTP requests) implicit fetching can help reducing the amount of code required. If cursor has Implicit Fetching enabled, while iterating (foreach, Cursor::next(), Cursor::prev()) the page end is reached, the SDK will automatically fetch and append a new page, until cursor end. Implicit Fetching will make you lose control of the number of HTTP request that will be sent and, for this reason, is disabled by default. Implicit Fetching can be enabled for a specific cursor:, (*26)

$cursor->setUseImplicitFetch(true);

Or globally:, (*27)

use FacebookAds\Cursor;

Cursor::setDefaultUseImplicitFetch(true);

Reverse Iterations

Cursors are bidirectional, and can be iterated in reverse order:, (*28)

use FacebookAds\Object\AbstractCrudObject;

/** @var \FacebookAds\Cursor $cursor */
$cursor->setUseImplicitFetch(true);

$cursor->end();
while ($cursor->valid()) {
  echo $cursor->current()->{AbstractCrudObject::FIELD_ID}.PHP_EOL;
  $cursor->prev();
}

Tests

The 'test' folder contains the test cases. These are logically divided in unit and integration tests. Integration tests require an active Facebook Ad Account, a Facebook Application and a valid Access Token., (*29)

Note: we are currently unable to securely and reliably run integration tests on a public CI system. Our integrations with Travis and Scrutinizer (including badges at the top of this file) include only unit tests., (*30)

Install dependencies

From the root folder run:, (*31)

php composer.phar install --dev

Execute unit tests only

./vendor/bin/phpunit -c test/phpunit-travis.xml

To run tests individually (be sure not to be pointing to an integration test file):, (*32)

./vendor/bin/phpunit -c test/phpunit-travis.xml path/to/class/file

Execute all tests (unit + integration)

Setup your integration config:, (*33)

1 - Copy the config file template., (*34)

cp test/config.php.dist test/config.php

2 - Edit test/config.php with your information., (*35)

Execute:, (*36)

./vendor/bin/phpunit -c test/

To run tests individually:, (*37)

./vendor/bin/phpunit -c test/ path/to/class/file

Debug

If this SDK is not working as expected, it may be either an SDK issue or API issue., (*38)

This can be identified by constructing a raw cURL request and seeing if the response is as expected, (*39)

for example:, (*40)

require __DIR__ . '/vendor/autoload.php';
use FacebookAds\Api;
use FacebookAds\Object\AdAccount;

Api::init($app_id, $app_secret, $access_token);
$api = Api::instance();

use FacebookAds\Logger\CurlLogger;
$api->setLogger(new CurlLogger());
$account = new AdAccount($account_id);
$account->read(array('id'));

When running this code, this cURL request will be printed to the console as:, (*41)

curl -G \
  -d 'fields=id' \
  -d 'access_token=<access_token>' \
  https://graph.facebook.com/v3.1/<act_accountid>

SDK Codegen

Our SDK is autogenerated from SDK Codegen. If you want to learn more about how our SDK code is generated, please check this repository., (*42)

Issue

Since we want to handle bugs more efficiently, we've decided to close issue reporting in GitHub and move to our dedicated bug reporting channel. If you encounter a bug with Business SDK (PHP), please report the issue at our developer bug reporting channel., (*43)

The Versions

28/07 2018

dev-master

9999999-dev https://developers.facebook.com/

PHP SDK for Facebook Business

  Sources   Download

Facebook Platform

The Development Requires

page sdk facebook instagram business ads

28/07 2018

3.1.0

3.1.0.0 https://developers.facebook.com/

PHP SDK for Facebook Business

  Sources   Download

The Development Requires

page sdk facebook instagram business ads

19/07 2018

3.0.5

3.0.5.0 https://developers.facebook.com/

PHP SDK for Facebook Business

  Sources   Download

The Development Requires

page sdk facebook instagram business ads

21/06 2018

3.0.4

3.0.4.0 https://developers.facebook.com/

PHP SDK for Facebook Business

  Sources   Download

The Development Requires

page sdk facebook instagram business ads

19/06 2018

3.0.3

3.0.3.0 https://developers.facebook.com/

PHP SDK for Facebook Business

  Sources   Download

The Development Requires

page sdk facebook instagram business ads

11/05 2018

3.0.1

3.0.1.0 https://developers.facebook.com/

PHP SDK for Facebook Business

  Sources   Download

The Development Requires

page sdk facebook instagram business ads

11/05 2018

3.0.2

3.0.2.0 https://developers.facebook.com/

PHP SDK for Facebook Business

  Sources   Download

The Development Requires

page sdk facebook instagram business ads

01/05 2018

3.0.0

3.0.0.0 https://developers.facebook.com/

PHP SDK for Facebook Business

  Sources   Download

The Development Requires

page sdk facebook instagram business ads

22/01 2018

2.11.3

2.11.3.0 https://developers.facebook.com/

PHP SDK for Facebook ads

  Sources   Download

Facebook Platform

The Development Requires

sdk facebook ads

05/12 2017

2.11.2

2.11.2.0 https://developers.facebook.com/

PHP SDK for Facebook ads

  Sources   Download

Facebook Platform

The Development Requires

sdk facebook ads

08/11 2017

2.11.1

2.11.1.0 https://developers.facebook.com/

PHP SDK for Facebook ads

  Sources   Download

Facebook Platform

The Development Requires

sdk facebook ads

18/07 2017

2.10.1

2.10.1.0 https://developers.facebook.com/

PHP SDK for Facebook ads

  Sources   Download

Facebook Platform

The Development Requires

sdk facebook ads

23/06 2017

2.9.2

2.9.2.0 https://developers.facebook.com/

PHP SDK for Facebook ads

  Sources   Download

Facebook Platform

The Development Requires

sdk facebook ads

24/04 2017

2.9.1

2.9.1.0 https://developers.facebook.com/

PHP SDK for Facebook ads

  Sources   Download

Facebook Platform

The Development Requires

sdk facebook ads

13/04 2017

2.8.2

2.8.2.0 https://developers.facebook.com/

PHP SDK for Facebook ads

  Sources   Download

Facebook Platform

The Development Requires

sdk facebook ads

06/10 2016

2.8.1

2.8.1.0 https://developers.facebook.com/

PHP SDK for Facebook ads

  Sources   Download

Facebook Platform

The Development Requires

sdk facebook ads

14/07 2016

2.7.1

2.7.1.0 https://developers.facebook.com/

PHP SDK for Facebook ads

  Sources   Download

Facebook Platform

The Development Requires

sdk facebook ads

12/04 2016

dev-legacy

dev-legacy https://developers.facebook.com/

PHP SDK for Facebook ads

  Sources   Download

Facebook Platform

The Development Requires

sdk facebook ads

12/04 2016

2.6.0

2.6.0.0 https://developers.facebook.com/

PHP SDK for Facebook ads

  Sources   Download

Facebook Platform

The Development Requires

sdk facebook ads

07/04 2016

2.5.2

2.5.2.0 https://developers.facebook.com/

PHP SDK for Facebook ads

  Sources   Download

Facebook Platform

The Development Requires

sdk facebook ads

06/11 2015

2.5.1

2.5.1.0 https://developers.facebook.com/

PHP SDK for Facebook ads

  Sources   Download

Facebook Platform

The Development Requires

sdk facebook ads

08/10 2015

2.5.0

2.5.0.0 https://developers.facebook.com/

PHP SDK for Facebook ads

  Sources   Download

Facebook Platform

The Development Requires

sdk facebook ads

04/09 2015

2.4.2

2.4.2.0 https://developers.facebook.com/

PHP SDK for Facebook ads

  Sources   Download

Facebook Platform

The Development Requires

sdk facebook ads

12/08 2015

2.4.1

2.4.1.0 https://developers.facebook.com/

PHP SDK for Facebook ads

  Sources   Download

Facebook Platform

The Development Requires

sdk facebook ads

10/07 2015

2.4.0

2.4.0.0 https://developers.facebook.com/

PHP SDK for Facebook ads

  Sources   Download

Facebook Platform

The Development Requires

sdk facebook ads

04/05 2015

2.3.1

2.3.1.0 https://developers.facebook.com/

PHP SDK for Facebook ads

  Sources   Download

Facebook Platform

The Development Requires

sdk facebook ads

25/03 2015

2.3.0

2.3.0.0 https://developers.facebook.com/

PHP SDK for Facebook ads

  Sources   Download

Facebook Platform

The Development Requires

sdk facebook ads

06/03 2015

2.2.4

2.2.4.0 https://developers.facebook.com/

PHP SDK for Facebook ads

  Sources   Download

Facebook Platform

The Development Requires

sdk facebook ads

19/12 2014

2.2.3

2.2.3.0 https://developers.facebook.com/

PHP SDK for Facebook ads

  Sources   Download

Facebook Platform

The Development Requires

sdk facebook ads

19/11 2014

2.2.2

2.2.2.0 https://developers.facebook.com/

PHP SDK for Facebook ads

  Sources   Download

Facebook Platform

The Development Requires

sdk facebook ads

04/11 2014

2.2.1

2.2.1.0 https://developers.facebook.com/

PHP SDK for Facebook ads

  Sources   Download

Facebook Platform

The Development Requires

sdk facebook ads

31/10 2014

2.2.0

2.2.0.0 https://developers.facebook.com/

PHP SDK for Facebook ads

  Sources   Download

Facebook Platform

The Development Requires

sdk facebook ads

08/10 2014

0.9.11

0.9.11.0 https://developers.facebook.com/

PHP SDK for Facebook ads

  Sources   Download

Facebook Platform

The Requires

 

The Development Requires

sdk facebook ads

08/10 2014

0.9.10

0.9.10.0 https://developers.facebook.com/

PHP SDK for Facebook ads

  Sources   Download

Facebook Platform

The Requires

 

The Development Requires

sdk facebook ads

15/09 2014

0.9.9

0.9.9.0 https://developers.facebook.com/

PHP SDK for Facebook ads

  Sources   Download

Facebook Platform

The Requires

 

The Development Requires

sdk facebook ads

12/09 2014

0.9.8

0.9.8.0 https://developers.facebook.com/

PHP SDK for Facebook ads

  Sources   Download

Facebook Platform

The Requires

 

The Development Requires

sdk facebook ads

11/09 2014

0.9.7

0.9.7.0 https://developers.facebook.com/

PHP SDK for Facebook ads

  Sources   Download

Facebook Platform

The Requires

 

The Development Requires

sdk facebook ads

02/09 2014

0.9.6

0.9.6.0 https://developers.facebook.com/

PHP SDK for Facebook ads

  Sources   Download

Facebook Platform

The Requires

 

The Development Requires

sdk facebook ads

10/07 2014

0.9.5

0.9.5.0 https://developers.facebook.com/

PHP SDK for Facebook ads

  Sources   Download

Facebook Platform

The Requires

 

The Development Requires

sdk facebook ads

26/06 2014

0.9.4

0.9.4.0 https://developers.facebook.com/

PHP SDK for Facebook ads

  Sources   Download

Facebook Platform

The Requires

 

The Development Requires

sdk facebook ads

24/06 2014

0.9.3

0.9.3.0 https://developers.facebook.com/

PHP SDK for Facebook ads

  Sources   Download

Facebook Platform

The Requires

 

The Development Requires

sdk facebook ads

18/06 2014

0.9.2

0.9.2.0 https://developers.facebook.com/

PHP SDK for Facebook ads

  Sources   Download

Facebook Platform

The Requires

 

The Development Requires

sdk facebook ads

17/06 2014

0.9.1

0.9.1.0 https://developers.facebook.com/

PHP SDK for Facebook ads

  Sources   Download

Facebook Platform

The Requires

 

The Development Requires

sdk facebook ads

16/06 2014

0.9.0

0.9.0.0

  Sources   Download

The Requires

 

The Development Requires