Getting started, why the facebook php sdk is out of date

Launching Visual Studio

If nothing happens, tải về the extension for Visual Studio và try again.

Bạn đang xem: Getting started, why the facebook php sdk is out of date

Go baông chồng


The Facebook Business SDK is a one-stop siêu thị to lớn help our partners better serve sầu their businesses. Partners are using multiple Facebook API"s khổng lồ VPS the needs of their clients. Adopting all these API"s và 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 lớn one SDK to lớn ease implementation & 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.

Quick Start

Business SDK Getting Started Guide


Register An App

To get started with the SDK, you must have sầu an appregistered on

To manage the Marketing API, please visit your/dashboard" rel="nofollow"> App Dashboard & add the Marketing API product to your phầm mềm.

IMPORTANT: For security, it is recommended that you turn on "App SecretProof for Server API calls" in your app"s Settings->Advanced page.

Obtain An Access Token

When someone connects with an phầm mềm using Facebook Login and approves the requestfor permissions, the tiện ích obtains an access token that provides temporary, secureaccess to lớn Facebook APIs.

An access token is an opaque string that identifies a User, phầm mềm, or Page.

For example, to access the Marketing API, you need khổng lồ generate a User access tokenfor your phầm mềm and ask for the ads_management permission; lớn access Pages API,you need to lớn generate a Page access token for your phầm mềm và ask for the manage_page permission.

Refer lớn ourAccess Token Guide khổng lồ learn more.

For now, we can use theGraph Explorerto lớn get an access token.


The Facebook Business SDK requires PHPhường. 5.6 or greater.


The Facebook Business SDK uses composer lớn manage dependencies. Visit the composer documentation lớn learn how khổng lồ install composer.

Xem thêm: phần mềm zkaccess 3.5

Add the following to lớn your composer.json file:

"require": "facebook/php-business-sdk": "5.0.5"
then install it through composer:

php composer.phar install --no-dev
This SDK và its dependencies will be installed under ./vendor.


This repository is written following the psr-4 autoloading standard. Any psr-4 compatible autoloader can be used.


Api main class

The FacebookAdsApi object is the foundation of the Business SDK which encapsulates a FacebookAdsSession and is used to execute requests against the Graph API.

To instantiate an Api object you will need a valid access token:

use FacebookAdsApi;// Initialize a new Session and instantiate an Api objectApi::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 lớn start making requests lớn the Graph API.

Fields names

Due lớn the high number of field names in the Graph API existing objects, in order lớn facilitate your code maintainability, enum-lượt thích 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 bởi vì in php:

use FacebookAdsObjectAdAccount;$account = new AdAccount();$account->name = "My tài khoản name";eđến $account->name;
or using the enums:

use FacebookAdsObjectAdAccount;use FacebookAdsObjectFieldsAdAccountFields;$tài khoản = new AdAccount();$account->AdAccountFields::NAME = "My tài khoản name";emang lại $account->AdAccountFields::NAME;

Object classes

Facebook Ads entities are defined as classes under the FacebookAds/Object directory.

Read Objects
use FacebookAdsObjectAdAccount;$trương mục = (new AdAccount($account_id))->getSelf();
For some objects, the Ads API doesn"t return all available fields by mặc định. The first argument of the object"s read method is an array of field names lớn be requested.

use FacebookAdsObjectAdAccount;use FacebookAdsObjectFieldsAdAccountFields;$fields = array( AdAccountFields::ID, AdAccountFields::NAME,);$tài khoản = (new AdAccount($account_id))->getSelf($fields);
Requesting an high number of fields may cause the response time lớn visibly increase, you should always request only the fields you really need.

Create Objects
use FacebookAdsObjectAdSet;use FacebookAdsObjectAdAccount;use FacebookAdsObjectFieldsAdSetFields;$account_id = "act_123123";$campaign_id = "123456";$trương mục = new AdAccount($account_id);$adphối = $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 FacebookAdsObjectAdSet;use FacebookAdsObjectFieldsAdSetFields;$ad_set_id = "123456";$mix = new AdSet($ad_set_id);$fields = array();$params = array( AdSetFields::NAME => "My new AdSet name",);$set->updateSelf($fields, $params);
Delete Objects
use FacebookAdsObjectAdSet;$ad_set_id = "123456";$mix = new AdSet($ad_set_id);$set->deleteSelf();


Since the release of the Facebook Graph API 2.0, pagination is handled through cursors.Here cursors are defined as in FacebookAdsCursor. Cursors are generally returned from connection methods:

use FacebookAdsObjectAdAccount;use FacebookAdsObjectFieldsCampaignFields;$account = new AdAccount("");$cursor = $account->getCampaigns(<"id","name">);// Loop over objectsforeach ($cursor as $campaign) emang lại $campaign->CampaignFields::NAME.PHP_EOL;// Access objects by indexif ($cursor->count() > 0) eđến "The first campaign in the cursor is: ".$cursor<0>->CampaignFields::NAME.PHP_EOL;// Fetch the next page$cursor->fetchAfter();// New Objects will be appended khổng lồ the cursor
Implicit FetchingWhenever all object connected to lớn 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 kết thúc is reached, the SDK will automatically fetch and appover a new page, until cursor over.Implicit Fetching will make you lose control of the number of HTTP.. request that will be sent &, for this reason, is disabled by mặc định.Implicit Fetching can be enabled for a specific cursor:

Or globally:

use FacebookAdsCursor;Cursor::setDefaultUseImplicitFetch(true);
Reverse IterationsCursors are bi-directional, and can be iterated in reverse order:

use FacebookAdsObjectAbstractCrudObject;/**
var FacebookAdsCursor $cursor */$cursor->setUseImplicitFetch(true);$cursor->end();while ($cursor->valid()) emang đến $cursor->current()->AbstractCrudObject::FIELD_ID.PHP_EOL; $cursor->prev();


The "test" folder contains the chạy thử cases. These are logically divided in unit & integration tests.Integration tests require an active Facebook Ad Account, a Facebook Application và a valid Access Token.

Note: we are currently unable to lớn securely và reliably run integration tests on a public CI system. Our integrations with Travis & Scrutinizer (including badges at the top of this file) include only unit tests.

Install dependencies

From the root folder run:

php composer.phar install --dev

Exexinh tươi unit tests only

./vendor/bin/phpunit -c test/phpunit-travis.xml
To run tests individually (be sure not to be pointing lớn an integration kiểm tra file):

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

Exedễ thương all tests (unit + integration)

Setup your integration config:

1 - Copy the config file template.

cp test/config.php.dist test/config.php
2 - Edit test/config.php with your informations.


./vendor/bin/phpunit -c test/
To run tests individually:

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


If this SDK is not working as expected, it may be either a SDK issue or API issue.

This can be identified by constructing a raw cURL request & seeing if the response is as expected

for example:

require __DIR__ . "/vendor/autoload.php";use FacebookAdsApi;use FacebookAdsObjectAdAccount;Api::init($app_id, $app_secret, $access_token);$api = Api::instance();use FacebookAdsLoggerCurlLogger;$api->setLogger(new CurlLogger());$tài khoản = new AdAccount($account_id);$account->read(array("id"));