$account_id = null;
// Configurations - End
if (is_null($access_token) || is_null($app_id) || is_null($app_secret)) {
    throw new \Exception('You must set your access token, app id and app secret before executing');
}
if (is_null($account_id)) {
    throw new \Exception('You must set your account id before executing');
}
define('SDK_DIR', __DIR__ . '/..');
// Path to the SDK directory
$loader = (include SDK_DIR . '/vendor/autoload.php');
use FacebookAds\Api;
Api::init($app_id, $app_secret, $access_token);
// use the namespace for Custom Audiences and Fields
use FacebookAds\Object\CustomAudience;
use FacebookAds\Object\Fields\CustomAudienceFields;
use FacebookAds\Object\Values\CustomAudienceTypes;
use FacebookAds\Object\Values\CustomAudienceSubtypes;
// Create a custom audience object, setting the parent to be the account id
$audience = new CustomAudience(null, $account_id);
$audience->setData(array(CustomAudienceFields::NAME => 'My Custom Audiece', CustomAudienceFields::DESCRIPTION => 'Lots of people', CustomAudienceFields::SUBTYPE => CustomAudienceSubtypes::CUSTOM));
// Create the audience
$audience->create();
echo "Audience ID: " . $audience->id . "\n";
// Assuming you have an array of emails:
// NOTE: The SDK will hash (SHA-2) your data before submitting
// it to Facebook servers
$emails = array('*****@*****.**', '*****@*****.**', '*****@*****.**', '*****@*****.**');
$audience->addUsers($emails, CustomAudienceTypes::EMAIL);
$audience->read(array(CustomAudienceFields::APPROXIMATE_COUNT));
echo "Estimated Size:" . $audience->{CustomAudienceFields::APPROXIMATE_COUNT} . "\n";
// should begin with "act_" (eg: $account_id = 'act_1234567890';)
$account_id = null;
// Configurations - End
if (is_null($access_token) || is_null($app_id) || is_null($app_secret)) {
    throw new \Exception('You must set your access token, app id and app secret before executing');
}
if (is_null($account_id)) {
    throw new \Exception('You must set your account id before executing');
}
define('SDK_DIR', __DIR__ . '/..');
// Path to the SDK directory
$loader = (include SDK_DIR . '/vendor/autoload.php');
use FacebookAds\Api;
Api::init($app_id, $app_secret, $access_token);
// use the namespace for Custom Audiences and Fields
use FacebookAds\Object\CustomAudience;
use FacebookAds\Object\Fields\CustomAudienceFields;
use FacebookAds\Object\Values\CustomAudienceTypes;
// Create a custom audience object, setting the parent to be the account id
$audience = new CustomAudience(null, $account_id);
$audience->setData(array(CustomAudienceFields::NAME => 'My Custom Audiece', CustomAudienceFields::DESCRIPTION => 'Lots of people'));
// Create the audience
$audience->create();
echo "Audience ID: " . $audience->id . "\n";
// Assuming you have an array of emails:
// NOTE: The SDK will hash (SHA-2) your data before submitting
// it to Facebook servers
$emails = array('*****@*****.**', '*****@*****.**', '*****@*****.**', '*****@*****.**');
$audience->addUsers($emails, CustomAudienceTypes::EMAIL);
$audience->read(array(CustomAudienceFields::APPROXIMATE_COUNT));
echo "Estimated Size:" . $audience->{CustomAudienceFields::APPROXIMATE_COUNT} . "\n";
$response = $api->call('/me/accounts');
$data = $response->getContent();
$page_token = '';
foreach ($data['data'] as $page) {
    if ($page['id'] == $page_id) {
        $page_token = $page['access_token'];
        break;
    }
}
if ($page_token === '') {
    throw new \InvalidArgumentException('Page access token for the page id ' . $page_id . ' cannot be found.');
}
$page_session = new Session($config->appId, $config->appSecret, $page_token);
$page_api = new Api($api->getHttpClient(), $page_session);
$page_api->setLogger($api->getLogger());
$request = $page_api->prepareRequest('/' . $page_id . '/videos', RequestInterface::METHOD_POST);
$request->setLastLevelDomain('graph-video');
$request->getFileParams()->offsetSet('source', $video_path);
$response = $page_api->executeRequest($request);
$data = $response->getContent();
$video_id = is_string($data) ? $data : (string) $data['id'];
// _DOC open [CUSTOM_AUDIENCE_CREATE_VIDEO_VIEWS_RETARGET]
// _DOC vars [ad_account_id:s, video_id]
// use FacebookAds\Object\CustomAudience;
// use FacebookAds\Object\Fields\CustomAudienceFields;
// use FacebookAds\Object\Values\CustomAudienceSubtypes;
$lookalike = new CustomAudience(null, $ad_account_id);
$lookalike->setData(array(CustomAudienceFields::SUBTYPE => CustomAudienceSubtypes::LOOKALIKE, CustomAudienceFields::LOOKALIKE_SPEC => array('ratio' => 0.01, 'country' => 'US', 'engagement_specs' => array(array('action.type' => 'video_view', 'post' => $video_id)), 'conversion_type' => 'dynamic_rule')));
$lookalike->create();
// _DOC close [CUSTOM_AUDIENCE_CREATE_VIDEO_VIEWS_RETARGET]
$lookalike->delete();