123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- <?php
- require_once __DIR__ . '/../../vendor/autoload.php';
- use TencentAds\TencentAds;
- use TencentAds\Exception\TencentAdsResponseException;
- use TencentAds\Exception\TencentAdsSDKException;
- class AddMiniProgramBannerAds
- {
- public static $tads;
- public static $ACCESS_TOKEN = 'YOUR ACCESS TOKEN';
- public static $ACCOUNT_ID = 'YOUR ACCOUNT ID';
- public static $ADCREATIVE_TEMPLATE_ID = 608;
- public static $IMAGE_PATH = 'YOUR AD IMAGE PATH';
- public static $MINI_GAME_ID = 'YOUR MINI GAME ID';
- public static $MINI_GAME_PATH = 'YOUR MINI GAME PATH';
- public function init()
- {
- $tads = TencentAds::init([
- 'access_token' => static::$ACCESS_TOKEN,
- 'is_debug' => true,
- ]);
- $tads->useSandbox();
- static::$tads = $tads;
- return $tads;
- }
- public function main()
- {
- try {
-
- $tads = static::$tads;
-
- $campaignType = 'CAMPAIGN_TYPE_NORMAL';
- $promotedObjectType = 'PROMOTED_OBJECT_TYPE_MINI_GAME_WECHAT';
- $campaignId = $this->AddCampaign($tads, $campaignType, $promotedObjectType);
-
-
- $siteSet = ['SITE_SET_WECHAT'];
- $adgroupId = $this->AddAdgroup($tads, $campaignId, $promotedObjectType, static::$MINI_GAME_ID, $siteSet);
-
-
- $imageId = $this->AddImage($tads, static::$IMAGE_PATH);
-
-
- $adcreativeId = $this->AddAdcreative($tads, static::$ADCREATIVE_TEMPLATE_ID, $campaignId,
- $promotedObjectType, static::$MINI_GAME_ID, $siteSet, $imageId, static::$MINI_GAME_PATH);
-
-
- $adId = $this->AddAd($tads, $adgroupId, $adcreativeId);
-
- return $adId;
- } catch (TencentAdsResponseException $e) {
-
- echo 'Tencent ads returned an error: ' . $e->getMessage() . PHP_EOL;
- throw $e;
- } catch (TencentAdsSDKException $e) {
-
- echo 'Tencent ads SDK returned an error: ' . $e->getMessage() . PHP_EOL;
- throw $e;
- } catch (Exception $e) {
- echo 'Other exception: ' . $e->getMessage() . PHP_EOL;
- throw $e;
- }
- }
-
- private function AddCampaign(TencentAds $tads, $campaignType, $promotedObjectType)
- {
- $campaignName = 'SDK计划' . uniqid();
- $campaignDailyBudget = 0;
- $campaign = $tads->campaigns()
- ->add([
- 'account_id' => static::$ACCOUNT_ID,
- 'campaign_name' => $campaignName,
- 'campaign_type' => $campaignType,
- 'promoted_object_type' => $promotedObjectType,
- 'daily_budget' => $campaignDailyBudget,
- ]);
- $campaignId = $campaign->getCampaignId();
- return $campaignId;
- }
-
- private function AddAdgroup(
- TencentAds $tads,
- $campaignId,
- $promotedObjectType,
- $promotedObjectId,
- $siteSet
- ) {
- $adgroupName = 'SDK sample adgroup ' . uniqid();
- $beginDate = date('Y-m-d', strtotime('+1 day'));
- $endDate = date('Y-m-d', strtotime('+10 day'));
- $billingEvent = 'BILLINGEVENT_IMPRESSION';
- $bidAmount = 20000;
- $optimizationGoal = 'OPTIMIZATIONGOAL_APP_REGISTER';
- $timeSeries = str_repeat('1', 48 * 7);
- $adgroupDailyBudget = 1000000;
- $expandEnabled = true;
- $expandTargeting = ['age', 'geo_location', 'gender'];
- $regionIds = ['310000', '110000'];
- $targeting = [
- 'age' => [
- [
- 'min' => 23,
- 'max' => 45,
- ],
- ],
- 'gender' => ['MALE'],
- 'geo_location' => [
- 'location_types' => ['LIVE_IN'],
- 'regions' => $regionIds,
- ],
- 'user_os' => ['IOS'],
- ];
- $adgroup = $tads->adgroups()
- ->add([
- 'account_id' => static::$ACCOUNT_ID,
- 'campaign_id' => $campaignId,
- 'adgroup_name' => $adgroupName,
- 'promoted_object_type' => $promotedObjectType,
- 'begin_date' => $beginDate,
- 'end_date' => $endDate,
- 'billing_event' => $billingEvent,
- 'bid_amount' => $bidAmount,
- 'optimization_goal' => $optimizationGoal,
- 'time_series' => $timeSeries,
- 'site_set' => $siteSet,
- 'daily_budget' => $adgroupDailyBudget,
- 'promoted_object_id' => $promotedObjectId,
- 'targeting' => $targeting,
- 'expand_enabled' => $expandEnabled,
- 'expand_targeting' => $expandTargeting,
- ]);
- $adgroupId = $adgroup->getAdgroupId();
- return $adgroupId;
- }
-
- private function AddImage(TencentAds $tads, $imageFile)
- {
- $imageSignature = md5_file($imageFile);
- $uploadType = 'UPLOAD_TYPE_FILE';
- $image = $tads->images()
- ->add([
- 'account_id' => static::$ACCOUNT_ID,
- 'upload_type' => $uploadType,
- 'file' => $imageFile,
- 'signature' => $imageSignature,
- ]);
- $imageId = $image->getImageId();
- return $imageId;
- }
-
- private function AddAdcreative(
- TencentAds $tads,
- $adcreativeTemplateId,
- $campaignId,
- $promotedObjectType,
- $promotedObjectId,
- $siteSet,
- $imageId,
- $miniProgramPath
- ) {
- $adcreativeName = 'SDK sample adcreative ' . uniqid();
- $adcreativeElements = [
- 'image_list' => [intval($imageId)],
- ];
- $pageType = 'PAGE_TYPE_MINI_GAME_WECHAT';
- $pageSpec = [
- 'mini_program_spec' => [
- 'mini_program_id' => $promotedObjectId,
- 'mini_program_path' => $miniProgramPath,
- ],
- ];
- $adcreative = $tads->adcreatives()
- ->add([
- 'account_id' => static::$ACCOUNT_ID,
- 'campaign_id' => $campaignId,
- 'adcreative_name' => $adcreativeName,
- 'adcreative_template_id' => $adcreativeTemplateId,
- 'adcreative_elements' => $adcreativeElements,
- 'promoted_object_type' => $promotedObjectType,
- 'page_type' => $pageType,
- 'page_spec' => $pageSpec,
- 'site_set' => $siteSet,
- 'promoted_object_id' => $promotedObjectId,
- ]);
- $adcreativeId = $adcreative->getAdcreativeId();
- return $adcreativeId;
- }
-
- private function AddAd(TencentAds $tads, $adgroupId, $adcreativeId)
- {
- $adName = 'SDK sample ad ' . uniqid();
- $ad = $tads->ads()
- ->add([
- 'account_id' => static::$ACCOUNT_ID,
- 'adgroup_id' => $adgroupId,
- 'adcreative_id' => $adcreativeId,
- 'ad_name' => $adName,
- ]);
- $adId = $ad->getAdId();
- return $adId;
- }
- }
- if (basename($_SERVER['PHP_SELF']) == basename(__FILE__)) {
- try {
- $example = new AddMiniProgramBannerAds();
- $example->init();
- $example->main();
- } catch (\Exception $e) {
- exit(-1);
- }
- }
|