public function setup() { parent::setup(); $targeting = new TargetingSpecs(); $targeting->{TargetingSpecsFields::GEO_LOCATIONS} = array('countries' => array('US')); $this->campaign = new Campaign(null, $this->getConfig()->accountId); $this->campaign->{CampaignFields::NAME} = $this->getConfig()->testRunId; $this->campaign->create(); $this->adSet = new AdSet(null, $this->getConfig()->accountId); $this->adSet->{AdSetFields::CAMPAIGN_ID} = (int) $this->campaign->{AdSetFields::ID}; $this->adSet->{AdSetFields::NAME} = $this->getConfig()->testRunId; $this->adSet->{AdSetFields::DAILY_BUDGET} = '150'; $this->adSet->{AdSetFields::START_TIME} = (new \DateTime("+1 week"))->format(\DateTime::ISO8601); $this->adSet->{AdSetFields::END_TIME} = (new \DateTime("+2 week"))->format(\DateTime::ISO8601); $this->adSet->{AdFields::TARGETING} = $targeting; $this->adSet->{AdSetFields::OPTIMIZATION_GOAL} = OptimizationGoals::REACH; $this->adSet->{AdSetFields::BILLING_EVENT} = BillingEvents::IMPRESSIONS; $this->adSet->{AdSetFields::BID_AMOUNT} = 2; $this->adSet->create(array(AdSet::STATUS_PARAM_NAME => AdSet::STATUS_PAUSED)); $this->adImage = new AdImage(null, $this->getConfig()->accountId); $this->adImage->{AdImageFields::FILENAME} = $this->getConfig()->testImagePath; $this->adImage->save(); $this->adCreative = new AdCreative(null, $this->getConfig()->accountId); $this->adCreative->{AdCreativeFields::TITLE} = 'My Test Ad'; $this->adCreative->{AdCreativeFields::BODY} = 'My Test Ad Body'; $this->adCreative->{AdCreativeFields::OBJECT_ID} = $this->getConfig()->pageId; $this->adCreative->{AdCreativeFields::IMAGE_HASH} = $this->adImage->{AdImageFields::HASH}; $this->adCreative->create(); $this->ad = new Ad(null, $this->getConfig()->accountId); $this->ad->{AdFields::NAME} = $this->getConfig()->testRunId; $this->ad->{AdFields::ADSET_ID} = (int) $this->adSet->{AdSetFields::ID}; $this->ad->{AdFields::CREATIVE} = array('creative_id' => $this->adCreative->{AdCreativeFields::ID}); $this->ad->create(array(Ad::STATUS_PARAM_NAME => Ad::STATUS_PAUSED)); }
$adset = new AdSet(null, $account->id); $adset->setData(array(AdSetFields::NAME => 'My First AdSet', AdSetFields::CAMPAIGN_ID => $campaign->id, AdSetFields::STATUS => AdSet::STATUS_ACTIVE, AdSetFields::DAILY_BUDGET => '150', AdSetFields::TARGETING => $targeting, AdSetFields::OPTIMIZATION_GOAL => OptimizationGoals::REACH, AdSetFields::BILLING_EVENT => BillingEvents::IMPRESSIONS, AdSetFields::BID_AMOUNT => 2, AdSetFields::START_TIME => (new \DateTime("+1 week"))->format(\DateTime::ISO8601), AdSetFields::END_TIME => (new \DateTime("+2 week"))->format(\DateTime::ISO8601))); $adset->validate()->create(); echo 'AdSet ID: ' . $adset->id . "\n"; /** * Step 5 Create an AdImage */ use FacebookAds\Object\AdImage; use FacebookAds\Object\Fields\AdImageFields; $image = new AdImage(null, $account->id); $image->{AdImageFields::FILENAME} = SDK_DIR . '/test/misc/image.png'; $image->create(); echo 'Image Hash: ' . $image->hash . "\n"; /** * 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"; /** * 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";
private function createAd($adSetId, $creativeId) { $data = array(AdFields::NAME => 'My Ad ' . date("Y-m-d H:i:s"), AdFields::ADSET_ID => $adSetId, AdFields::CREATIVE => array('creative_id' => $creativeId)); $ad = new Ad(null, 'act_' . $this->adAccountId); $ad->setData($data); $ad->create(array(Ad::STATUS_PARAM_NAME => Ad::STATUS_PAUSED)); return $ad->getData()[Ad::FIELD_ID]; }