public function testMultiProductObjectSpec() { // Create a new AdCreative $creative = new AdCreative(null, $this->getActId()); $creative->{AdCreativeFields::NAME} = 'Multi Product Ad Creative'; // Create a new ObjectStorySpec to create an unpublished post $story = new ObjectStorySpec(); $story->{ObjectStorySpecFields::PAGE_ID} = $this->getPageId(); // Create LinkData object representing data for a link page post $link = new LinkData(); $link->{LinkDataFields::LINK} = $this->getAppUrl(); $link->{LinkDataFields::CAPTION} = 'My Caption'; // Upload a test image to use in Attachments $adImage = new AdImage(null, $this->getActId()); $adImage->{AdImageFields::FILENAME} = $this->getTestImagePath(); $adImage->save(); // Create 3 products as this will be a multi-product ad $product1 = (new AttachmentData())->setData(array(AttachmentDataFields::LINK => $this->getAppUrl() . 'p1', AttachmentDataFields::IMAGE_HASH => $adImage->hash, AttachmentDataFields::NAME => 'Product 1', AttachmentDataFields::DESCRIPTION => '$100')); $product2 = (new AttachmentData())->setData(array(AttachmentDataFields::LINK => $this->getAppUrl() . 'p2', AttachmentDataFields::IMAGE_HASH => $adImage->hash, AttachmentDataFields::NAME => 'Product 2', AttachmentDataFields::DESCRIPTION => '$200')); $product3 = (new AttachmentData())->setData(array(AttachmentDataFields::LINK => $this->getAppUrl() . 'p3', AttachmentDataFields::IMAGE_HASH => $adImage->hash, AttachmentDataFields::NAME => 'Product 3', AttachmentDataFields::DESCRIPTION => '$300')); // Add the products into the child attachments $link->{LinkDataFields::CHILD_ATTACHMENTS} = array($product1, $product2, $product3); $story->{ObjectStorySpecFields::LINK_DATA} = $link; $creative->{AdCreativeFields::OBJECT_STORY_SPEC} = $story; $this->assertCanCreate($creative); $this->assertCanDelete($creative); }
public function testBulkZipUpload() { $images = AdImage::createFromZip($this->getTestZippedImagesPath(), $this->getActId()); $this->assertTrue(is_array($images)); foreach ($images as $image) { $this->assertCanDelete($image); } }
public function testBulkZipUpload() { $images = AdImage::createFromZip($this->getConfig()->testZippedImagesPath, $this->getConfig()->accountId); $this->assertTrue(is_array($images)); foreach ($images as $image) { $this->assertCanDelete($image); } }
public function tearDown() { if ($this->adSet) { $this->adSet->delete(); $this->adSet = null; } if ($this->campaign) { $this->campaign->delete(); $this->campaign = null; } if ($this->adCreative) { $this->adCreative->delete(); $this->adCreative = null; } if ($this->adImage) { $this->adImage->delete(); $this->adImage = null; } parent::tearDown(); }
/** * Uploads images from a zip file and returns a cursor of results * * @param string $file_path * @param string $account_id * @param array $params * @param Api $api * @return array */ public static function createFromZip($file_path, $account_id, array $params = array(), Api $api = null) { $image = new AdImage(null, $account_id, $api); $image->{AdImageFields::FILENAME} = $file_path; return $image->arrayFromZip($params); }
* 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_GROUP_ID => $campaign->id, AdSetFields::CAMPAIGN_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 AdGroup */
/** * @param array $fields * @param array $params * @return Cursor */ public function getAdImages(array $fields = array(), array $params = array()) { return $this->getManyByConnection(AdImage::className(), $fields, $params); }
public function createAdImage(array $fields = array(), array $params = array(), $pending = false) { $this->assureId(); $param_types = array('bytes' => 'string', 'copy_from' => 'Object', 'zipbytes' => 'string'); $enums = array(); $request = new ApiRequest($this->api, $this->data['id'], RequestInterface::METHOD_POST, '/adimages', new AdImage(), 'EDGE', AdImage::getFieldsEnum()->getValues(), new TypeChecker($param_types, $enums), true); $request->addParams($params); $request->addFields($fields); return $pending ? $request : $request->execute(); }
// _DOC open [ADIMAGE_CREATE] // _DOC vars [ad_account_id:s, image_path:s] // use FacebookAds\Object\AdImage; // use FacebookAds\Object\Fields\AdImageFields; $image = new AdImage(null, $ad_account_id); $image->{AdImageFields::FILENAME} = $image_path; $image->create(); echo 'Image Hash: ' . $image->{AdImageFields::HASH} . PHP_EOL; // _DOC close [ADIMAGE_CREATE] $image_id = $image->{AdImageFields::ID}; $image_hash = $image->{AdImageFields::HASH}; // _DOC open [ADIMAGE_DELETE] // _DOC vars [image_id, ad_account_id:s, image_hash:s] // use FacebookAds\Object\AdImage; // use FacebookAds\Object\Fields\AdImageFields; $image = new AdImage($image_id, $ad_account_id); $image->{AdImageFields::HASH} = $image_hash; $image->delete(); // _DOC close [ADIMAGE_DELETE] // _DOC open [ADIMAGE_CREATE_ZIP] // _DOC vars [zip_path:s, ad_account_id:s] // use FacebookAds\Object\AdImage; $images = AdImage::createFromZip($zip_path, $ad_account_id); // Output image hashes foreach ($images as $image) { echo $image->{AdImageFields::HASH} . PHP_EOL; } // _DOC close [ADIMAGE_CREATE_ZIP] $image_1_hash = $images[0]->{AdImageFields::HASH}; $image_2_hash = $images[1]->{AdImageFields::HASH}; // _DOC open [ADIMAGE_READ_MULTI_WITH_HASH]
// _DOC vars [adgroup_id] // use FacebookAds\Object\AdGroup; // use FacebookAds\Object\Fields\AdGroupFields; $adgroup = new AdGroup($ad_group_id); $adgroup->read(array(AdGroupFields::FAILED_DELIVERY_CHECKS)); echo $adgroup->{AdGroupFields::FAILED_DELIVERY_CHECKS} . PHP_EOL; // _DOC close [ADGROUP_READ_FAILED_DELIVERY_CHECKS] // _DOC open [ADGROUP_DELETE] // _DOC vars [ad_group_id] // use FacebookAds\Object\AdGroup; $adgroup = new AdGroup($ad_group_id); $adgroup->delete(); // _DOC close [ADGROUP_DELETE] $creative->delete(); $image->delete(); $image = new AdImage(null, $ad_account_id); $image->{AdImageFields::FILENAME} = $image_path; $image->create(); $image_hash = $image->{AdImageFields::HASH}; // _DOC open [ADGROUP_CREATE_INLINE_CREATIVE] // _DOC vars [ad_account_id:s, image_hash:s, ad_set_id:s] // use FacebookAds\Object\AdCreative; // use FacebookAds\Object\AdGroup; // use FacebookAds\Object\TargetingSpecs; // use FacebookAds\Object\Fields\AdCreativeFields; // use FacebookAds\Object\Fields\AdImageFields; // use FacebookAds\Object\Fields\AdGroupFields; // First, upload the ad image that you will use in your ad creative // Please refer to Ad Image Create for details. // Then, use the image hash returned from above $creative = new AdCreative(null, $ad_account_id);
} } 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()); $data = $page_api->call('/' . $page_id . '/promotable_posts', RequestInterface::METHOD_GET)->getContent(); if (is_null($data) || !is_array($data['data']) || count($data['data']) === 0) { throw new \RuntimeException("no promotable posts available for page " . $page_id); } $post_id = $data['data'][0]['id']; // create Ad Image $zip_path = $config->testZippedImagesPath; $images = AdImage::createFromZip($zip_path, $ad_account_id); $image_1_hash = $images[0]->{AdImageFields::HASH}; $image_2_hash = $images[1]->{AdImageFields::HASH}; $image_3_hash = $images[1]->{AdImageFields::HASH}; // create Ad Video $retry_delay = 3; $max_retry = 4; $video_name = $config->testRunId; $video_path = $config->testVideoPath; $video = new AdVideo(null, $ad_account_id); $video->setData(array(AdVideoFields::NAME => $video_name, AdVideoFields::SOURCE => $video_path)); $video->create(); $video_id = $video->{AdVideoFields::ID}; $video_thumbnails = null; while (--$max_retry >= 0) { sleep($retry_delay);