AddWechatMomentAds.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. <?php
  2. require_once __DIR__ . '/../../vendor/autoload.php'; // change path as needed
  3. use TencentAds\TencentAds;
  4. use TencentAds\Exception\TencentAdsResponseException;
  5. use TencentAds\Exception\TencentAdsSDKException;
  6. /*****
  7. * 本文件提供了一个创建一个微信推广IOS App的朋友圈单图广告的示例
  8. */
  9. class AddWechatMomentAds
  10. {
  11. public static $tads;
  12. public static $ACCESS_TOKEN = 'YOUR ACCESS TOKEN';
  13. public static $ACCOUNT_ID = 'YOUR ACCOUNT ID';
  14. public static $IOS_APP_ID = 'YOUR APP ID'; // 这里放被推广的iOS App的ID,即App Store的数字ID
  15. public static $ADCREATIVE_TEMPLATE_ID = 310; // 单图规格
  16. public static $PROMOTED_OBJECT_TYPE = 'PROMOTED_OBJECT_TYPE_APP_IOS'; // 推广IOS App
  17. public static $IMAGE_PATH = 'YOUR AD IMAGE PATH'; // 广告主图文件路径,310规格要求:640x800, <300K, png/jpg
  18. public static $IMAGE_PROFILE_PATH = 'YOUR AD IMAGE PATH'; // 如果未创建Profile的,提供Profile图片文件路径,200x200, <300K, png/jpg
  19. public static $PROFILE_ID = ''; // 如果已经创建Profile的,可提供Profile ID
  20. public static $AD_TITLE = 'YOUR AD TEXT'; // 广告文案,310规格要求:字数:1~40
  21. public static $PAGE_TYPE = 'PAGE_TYPE_MOMENTS_SIMPLE_NATIVE_WECHAT'; // 外层落地页:简版原生页
  22. public static $WEBVIEW_URL = 'YOUR WEBVIEW URL'; // 简版原生页内嵌页链接
  23. public static $LINK_PAGE_TYPE = 'LINK_PAGE_TYPE_DEFAULT'; // 行动按钮跳转落地页:ios应用下载页
  24. public static $LINK_NAME_TYPE = 'DOWNLOAD_APP'; // 行动按钮:下载应用
  25. public function init()
  26. {
  27. $tads = TencentAds::init([
  28. 'access_token' => static::$ACCESS_TOKEN,
  29. 'is_debug' => true,
  30. ]);
  31. $tads->useSandbox(); // 默认访问沙箱环境,如访问正式环境,请切换为$tads->useProduction()
  32. static::$tads = $tads;
  33. return $tads;
  34. }
  35. public function main()
  36. {
  37. try {
  38. /* @var TencentAds $tads */
  39. $tads = static::$tads;
  40. // 第一步,创建推广计划
  41. $campaignType = 'CAMPAIGN_TYPE_NORMAL';
  42. $promotedObjectType = static::$PROMOTED_OBJECT_TYPE;
  43. $campaignId = $this->AddCampaign($tads, $campaignType, $promotedObjectType);
  44. // echo 'Campaign ID: ' . $campaignId . PHP_EOL;
  45. // 第二步,创建推广目标
  46. $promotedObjectId = $this->AddPromotedObject($tads, $promotedObjectType, static::$IOS_APP_ID);
  47. // echo 'Promoted object ID: ' . $promotedObjectId . PHP_EOL;
  48. // 第三步,创建广告组,不允许使用定向包,因此在广告组里创建定向
  49. $siteSet = ['SITE_SET_MOMENTS']; // 投放朋友圈流量
  50. $adgroupId = $this->AddAdgroup($tads, $campaignId, $promotedObjectType, $promotedObjectId, $siteSet);
  51. // echo 'Adgroup ID: ' . $adgroupId . PHP_EOL;
  52. // 第四步,上传素材
  53. $imageId = $this->AddImage($tads, static::$IMAGE_PATH);
  54. // echo 'Image ID: ' . $imageId . PHP_EOL;
  55. if (empty(static::$PROFILE_ID)) {
  56. $imageIdLeads = $this->AddImage($tads, static::$IMAGE_PROFILE_PATH);
  57. // echo 'Image ID: ' . $imageIdLeads . PHP_EOL;
  58. $profileId = $this->AddProfile($tads, $promotedObjectType, $imageIdLeads);
  59. // echo 'Profile ID: ' . $profileId . PHP_EOL;
  60. } else {
  61. $profileId = static::$PROFILE_ID;
  62. }
  63. // 第五步,创建创意
  64. $adcreativeId = $this->AddAdcreative($tads, static::$ADCREATIVE_TEMPLATE_ID, $campaignId,
  65. $promotedObjectType, $promotedObjectId, $siteSet, $imageId, static::$AD_TITLE, $profileId);
  66. // echo 'Adcreative ID: ' . $adcreativeId . PHP_EOL;
  67. // 第六步,创建广告
  68. $adId = $this->AddAd($tads, $adgroupId, $adcreativeId);
  69. // echo 'Ad ID: ' . $adId . PHP_EOL;
  70. return $adId;
  71. } catch (TencentAdsResponseException $e) {
  72. // When Api returns an error
  73. echo 'Tencent ads returned an error: ' . $e->getMessage() . PHP_EOL;
  74. throw $e;
  75. } catch (TencentAdsSDKException $e) {
  76. // When validation fails or other local issues
  77. echo 'Tencent ads SDK returned an error: ' . $e->getMessage() . PHP_EOL;
  78. throw $e;
  79. } catch (Exception $e) {
  80. echo 'Other exception: ' . $e->getMessage() . PHP_EOL;
  81. throw $e;
  82. }
  83. }
  84. // 创建朋友圈推广计划
  85. private function AddCampaign(TencentAds $tads, $campaignType, $promotedObjectType)
  86. {
  87. $campaignName = 'SDK计划' . uniqid();
  88. $campaignDailyBudget = 0; // 不限日预算
  89. $campaign = $tads->campaigns()
  90. ->add([
  91. 'account_id' => static::$ACCOUNT_ID,
  92. 'campaign_name' => $campaignName,
  93. 'campaign_type' => $campaignType,
  94. 'promoted_object_type' => $promotedObjectType,
  95. 'daily_budget' => $campaignDailyBudget,
  96. ]);
  97. $campaignId = $campaign->getCampaignId();
  98. return $campaignId;
  99. }
  100. // 创建iOS App推广目标
  101. private function AddPromotedObject(TencentAds $tads, $promotedObjectType, $promotedObjectId)
  102. {
  103. $promotedObjects = $tads->promotedObjects()
  104. ->get([
  105. 'account_id' => static::$ACCOUNT_ID,
  106. 'filtering' => [
  107. [
  108. 'field' => 'promoted_object_id',
  109. 'operator' => 'EQUALS',
  110. 'values' => [$promotedObjectId],
  111. ],
  112. ],
  113. ]);
  114. if (count($promotedObjects->getList()) > 0) {
  115. // 从App Store更新推广目标的信息
  116. $promotedObjects = $tads->promotedObjects()
  117. ->update([
  118. 'account_id' => static::$ACCOUNT_ID,
  119. 'promoted_object_type' => $promotedObjectType,
  120. 'promoted_object_id' => $promotedObjectId,
  121. ]);
  122. } else {
  123. // 创建推广目标
  124. $promotedObjects = $tads->promotedObjects()
  125. ->add([
  126. 'account_id' => static::$ACCOUNT_ID,
  127. 'promoted_object_type' => $promotedObjectType,
  128. 'promoted_object_id' => $promotedObjectId,
  129. ]);
  130. }
  131. return $promotedObjectId;
  132. }
  133. // 创建广告组
  134. private function AddAdgroup(
  135. TencentAds $tads,
  136. $campaignId,
  137. $promotedObjectType,
  138. $promotedObjectId,
  139. $siteSet
  140. ) {
  141. $adgroupName = 'SDK sample adgroup ' . uniqid();
  142. $beginDate = date('Y-m-d', strtotime('+1 day')); // 开始投放日期
  143. $endDate = date('Y-m-d', strtotime('+10 day')); // 结束投放日期
  144. $bidMode = 'BID_MODE_OCPM'; // CPM
  145. $bidStrategy = 'BID_STRATEGY_AVERAGE_COST'; // 稳定拿量
  146. $bidAmount = 20000; // 200元(单位为分)
  147. $optimizationGoal = 'OPTIMIZATIONGOAL_APP_ACTIVATE'; // 优化目标APP激活
  148. $timeSeries = str_repeat('1', 48 * 7); // 全天投放
  149. $adgroupDailyBudget = 1000000; // 日预算10000元(单位为分)
  150. $expandEnabled = true; // 开启自动扩量
  151. $expandTargeting = ['age', 'geo_location', 'gender']; // 自动扩量定向范围
  152. $regionIds = ['310000', '110000']; // 上海,北京,朋友圈广告地域只能位于同一级别
  153. $targeting = [
  154. 'age' => [
  155. [ // 年龄定向,23~45岁
  156. 'min' => 23,
  157. 'max' => 45,
  158. ],
  159. ],
  160. 'gender' => ['MALE'], // 性别定向,男性
  161. 'geo_location' => [ // 地域定向
  162. 'location_types' => ['LIVE_IN'],
  163. 'regions' => $regionIds,
  164. ],
  165. 'user_os' => ['IOS'], // 操作系统定向
  166. ];
  167. $adgroup = $tads->adgroups()
  168. ->add([
  169. 'account_id' => static::$ACCOUNT_ID,
  170. 'campaign_id' => $campaignId,
  171. 'adgroup_name' => $adgroupName,
  172. 'promoted_object_type' => $promotedObjectType,
  173. 'begin_date' => $beginDate,
  174. 'end_date' => $endDate,
  175. 'bid_mode' => $bidMode,
  176. 'bid_strategy' => $bidStrategy,
  177. 'bid_amount' => $bidAmount,
  178. 'optimization_goal' => $optimizationGoal,
  179. 'time_series' => $timeSeries,
  180. 'site_set' => $siteSet,
  181. 'daily_budget' => $adgroupDailyBudget,
  182. 'promoted_object_id' => $promotedObjectId,
  183. 'targeting' => $targeting,
  184. 'expand_enabled' => $expandEnabled,
  185. 'expand_targeting' => $expandTargeting,
  186. ]);
  187. $adgroupId = $adgroup->getAdgroupId();
  188. return $adgroupId;
  189. }
  190. // 上传素材
  191. private function AddImage(TencentAds $tads, $imageFile)
  192. {
  193. $imageSignature = md5_file($imageFile);
  194. $uploadType = 'UPLOAD_TYPE_FILE';
  195. $image = $tads->images()
  196. ->add([
  197. 'account_id' => static::$ACCOUNT_ID,
  198. 'upload_type' => $uploadType,
  199. 'file' => $imageFile,
  200. 'signature' => $imageSignature,
  201. ]);
  202. $imageId = $image->getImageId();
  203. return $imageId;
  204. }
  205. // 创建profile
  206. protected function AddProfile(TencentAds $tads, $promotedObjectType, $imageId)
  207. {
  208. $profile = $tads->profiles()
  209. ->add([
  210. 'account_id' => static::$ACCOUNT_ID,
  211. 'profile_name' => 'SDK用例' . uniqid(),
  212. 'profile_type' => 'PROFILE_TYPE_DEFINITION',
  213. 'promoted_object_type' => $promotedObjectType,
  214. 'head_image_id' => $imageId,
  215. 'description' => 'SDK sample',
  216. ]);
  217. return $profile->getProfileId();
  218. }
  219. // 创建创意
  220. private function AddAdcreative(
  221. TencentAds $tads,
  222. $adcreativeTemplateId,
  223. $campaignId,
  224. $promotedObjectType,
  225. $promotedObjectId,
  226. $siteSet,
  227. $imageId,
  228. $adTitle,
  229. $profileId
  230. ) {
  231. $adcreativeName = 'SDK sample adcreative ' . uniqid();
  232. $adcreativeElements = [
  233. 'image_list' => [$imageId],
  234. 'title' => $adTitle,
  235. ];
  236. $shareContentSpec = [
  237. 'share_title' => '分享',
  238. 'share_description' => '分享详情',
  239. ];
  240. $adcreative = $tads->adcreatives()
  241. ->add([
  242. 'account_id' => static::$ACCOUNT_ID,
  243. 'campaign_id' => $campaignId,
  244. 'adcreative_name' => $adcreativeName,
  245. 'adcreative_template_id' => $adcreativeTemplateId,
  246. 'adcreative_elements' => $adcreativeElements,
  247. 'promoted_object_type' => $promotedObjectType,
  248. 'page_type' => static::$PAGE_TYPE,
  249. //'page_spec' => [],
  250. 'site_set' => $siteSet,
  251. 'promoted_object_id' => $promotedObjectId,
  252. 'link_page_type' => static::$LINK_PAGE_TYPE,
  253. 'link_name_type' => static::$LINK_NAME_TYPE,
  254. //'link_page_spec' => [],
  255. 'profile_id' => $profileId,
  256. 'webview_url' => static::$WEBVIEW_URL,
  257. 'share_content_spec' => $shareContentSpec,
  258. ]);
  259. $adcreativeId = $adcreative->getAdcreativeId();
  260. return $adcreativeId;
  261. }
  262. // 创建广告
  263. private function AddAd(TencentAds $tads, $adgroupId, $adcreativeId)
  264. {
  265. $adName = 'SDK sample ad ' . uniqid();
  266. $ad = $tads->ads()
  267. ->add([
  268. 'account_id' => static::$ACCOUNT_ID,
  269. 'adgroup_id' => $adgroupId,
  270. 'adcreative_id' => $adcreativeId,
  271. 'ad_name' => $adName,
  272. ]);
  273. $adId = $ad->getAdId();
  274. return $adId;
  275. }
  276. }
  277. if (basename($_SERVER['PHP_SELF']) == basename(__FILE__)) {
  278. try {
  279. $example = new AddWechatMomentAds();
  280. $example->init();
  281. $example->main();
  282. } catch (\Exception $e) {
  283. exit(-1);
  284. }
  285. }