Skip to content

Commit

Permalink
Updated according to SDK 2.8* and added try catch error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
avastamin committed Nov 15, 2016
1 parent ad5da80 commit 53d481b
Showing 1 changed file with 107 additions and 89 deletions.
196 changes: 107 additions & 89 deletions examples/adgroup_creation.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,120 +76,138 @@
*/
use FacebookAds\Object\Campaign;
use FacebookAds\Object\Fields\CampaignFields;
use FacebookAds\Object\Values\AdObjectives;

$campaign = new Campaign(null, $account->id);
$campaign->setData(array(
CampaignFields::NAME => 'My First Campaign',
CampaignFields::OBJECTIVE => AdObjectives::LINK_CLICKS,
));

$campaign->validate()->create(array(
Campaign::STATUS_PARAM_NAME => Campaign::STATUS_PAUSED,
));
echo "Campaign ID:" . $campaign->id . "\n";

try{
$campaign = new Campaign(null, $account->id);
$campaign->setData(array(
CampaignFields::NAME => 'My First Campaign',
CampaignFields::OBJECTIVE => 'LINK_CLICKS',
));

$campaign->validate()->create(array(
Campaign::STATUS_PARAM_NAME => Campaign::STATUS_PAUSED,
));
echo "Campaign ID:" . $campaign->id . "\n";
}
catch (Exception $e) {
echo 'Error message: ' .$e->getMessage() ."\n" . "<br/>";
echo 'Error Code: ' .$e->getCode() ."<br/>";
}
/**
* Step 3 Search Targeting
*/
use FacebookAds\Object\TargetingSearch;
use FacebookAds\Object\Search\TargetingSearchTypes;
use FacebookAds\Object\TargetingSpecs;
use FacebookAds\Object\Fields\TargetingSpecsFields;

$results = TargetingSearch::search(
$type = TargetingSearchTypes::INTEREST,
$class = null,
$query = 'facebook');

// we'll take the top result for now
$target = (count($results)) ? $results->current() : null;

echo "Using target: ".$target->name."\n";

$targeting = new TargetingSpecs();
$targeting->{TargetingSpecsFields::GEO_LOCATIONS}
= array('countries' => array('GB'));
$targeting->{TargetingSpecsFields::INTERESTS} = array(
array(
'id' => $target->id,
'name' => $target->name,
use FacebookAds\Object\Targeting;
use FacebookAds\Object\Fields\TargetingFields;


$targeting = new Targeting();
$targeting->setData(array(
TargetingFields::GEO_LOCATIONS => array(
'countries' => array('JP'),
'regions' => array(array('key' => '3886')),
'cities' => array(
array(
'key' => '2420605',
'radius' => 10,
'distance_unit' => 'mile',
),
),
),
);
));


/**
* Step 4 Create the AdSet
*/
use FacebookAds\Object\AdSet;
use FacebookAds\Object\Fields\AdSetFields;
use FacebookAds\Object\Values\OptimizationGoals;
use FacebookAds\Object\Values\BillingEvents;

$adset = new AdSet(null, $account->id);
$adset->setData(array(
AdSetFields::NAME => 'My First AdSet',
AdSetFields::CAMPAIGN_ID => $campaign->id,
AdSetFields::DAILY_BUDGET => '150',
AdSetFields::TARGETING => $targeting,
AdSetFields::OPTIMIZATION_GOAL => OptimizationGoals::REACH,
AdSetFields::BILLING_EVENT => BillingEvents::IMPRESSIONS,
AdSetFields::BID_AMOUNT => 100,
AdSetFields::START_TIME =>
(new \DateTime("+1 week"))->format(\DateTime::ISO8601),
AdSetFields::END_TIME =>
(new \DateTime("+2 week"))->format(\DateTime::ISO8601),
));

$adset->validate()->create(array(
AdSet::STATUS_PARAM_NAME => AdSet::STATUS_ACTIVE,
));

echo 'AdSet ID: '. $adset->id . "\n";

use FacebookAds\Object\Values\AdSetBillingEventValues;
use FacebookAds\Object\Values\AdSetOptimizationGoalValues;

try{
$adset = new AdSet(null, $account->id);
$adset->setData(array(
AdSetFields::NAME => 'My First AdSet',
AdSetFields::CAMPAIGN_ID => $campaign->id,
AdSetFields::DAILY_BUDGET => '150',
AdSetFields::TARGETING => $targeting,
AdSetFields::OPTIMIZATION_GOAL => AdSetOptimizationGoalValues::REACH,
AdSetFields::BILLING_EVENT => AdSetBillingEventValues::IMPRESSIONS,
AdSetFields::BID_AMOUNT => 100,
AdSetFields::START_TIME =>
(new \DateTime("+1 week"))->format(\DateTime::ISO8601),
AdSetFields::END_TIME =>
(new \DateTime("+2 week"))->format(\DateTime::ISO8601),
));

$adset->validate()->create(array(
AdSet::STATUS_PARAM_NAME => AdSet::STATUS_ACTIVE,
));

echo 'AdSet ID: '. $adset->id . "\n";
}
catch (Exception $e) {
echo 'Error message: ' .$e->getMessage() ."\n" . "<br/>";
echo 'Error Code: ' .$e->getCode() ."<br/>";
}
/**
* Step 5 Create an AdImage
*/
use FacebookAds\Object\AdImage;
use FacebookAds\Object\Fields\AdImageFields;
try {
$image = new AdImage(null, $account->id);
$image->{AdImageFields::FILENAME}
= SDK_DIR.'/test/misc/image.png';

$image = new AdImage(null, $account->id);
$image->{AdImageFields::FILENAME}
= SDK_DIR.'/test/misc/image.png';

$image->create();
echo 'Image Hash: '.$image->hash . "\n";

$image->create();
echo 'Image Hash: '.$image->hash . "\n";
}
catch (Exception $e) {
echo 'Error message: ' .$e->getMessage() ."\n" . "<br/>";
echo 'Error Code: ' .$e->getCode() ."<br/>";
}
/**
* Step 6 Create an AdCreative
*/
use FacebookAds\Object\AdCreative;
use FacebookAds\Object\Fields\AdCreativeFields;

$creative = new AdCreative(null, $account->id);
$creative->setData(array(
AdCreativeFields::NAME => 'Sample Creative',
AdCreativeFields::TITLE => 'Welcome to the Jungle',
AdCreativeFields::BODY => 'We\'ve got fun \'n\' games',
AdCreativeFields::IMAGE_HASH => $image->hash,
AdCreativeFields::OBJECT_URL => 'http://www.example.com/',
));

$creative->create();
echo 'Creative ID: '.$creative->id . "\n";

try{
$creative = new AdCreative(null, $account->id);
$creative->setData(array(
AdCreativeFields::NAME => 'Sample Creative',
AdCreativeFields::TITLE => 'Welcome to the Jungle',
AdCreativeFields::BODY => 'We\'ve got fun \'n\' games',
AdCreativeFields::IMAGE_HASH => $image->hash,
AdCreativeFields::OBJECT_URL => 'http://www.example.com/',
));

$creative->create();
echo 'Creative ID: '.$creative->id . "\n";
}
catch (Exception $e) {
echo 'Error message: ' .$e->getMessage() ."\n" . "<br/>";
echo 'Error Code: ' .$e->getCode() ."<br/>";
}
/**
* Step 7 Create an Ad
*/
use FacebookAds\Object\Ad;
use FacebookAds\Object\Fields\AdFields;

$ad = new Ad(null, $account->id);
$ad->setData(array(
AdFields::CREATIVE =>
array('creative_id' => $creative->id),
AdFields::NAME => 'My First Ad',
AdFields::ADSET_ID => $adset->id,
));

$ad->create();
echo 'Ad ID:' . $ad->id . "\n";
try {
$ad = new Ad(null, $account->id);
$ad->setData(array(
AdFields::CREATIVE =>
array('creative_id' => $creative->id),
AdFields::NAME => 'My First Ad',
AdFields::ADSET_ID => $adset->id,
));

$ad->create();
echo 'Ad ID:' . $ad->id . "\n";
}
catch (Exception $e) {
echo 'Error message: ' .$e->getMessage() ."\n" . "<br/>";
echo 'Error Code: ' .$e->getCode() ."<br/>";
}

0 comments on commit 53d481b

Please sign in to comment.