AddLeadsAds.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. * 本文件提供了一个创建一个朋友圈线索收集广告的示例
  8. */
  9. class AddLeadsAds
  10. {
  11. public static $tads;
  12. public static $ACCESS_TOKEN = 'YOUR ACCESS TOKEN';
  13. public static $ACCOUNT_ID = 'YOUR ACCOUNT ID';
  14. public static $ADCREATIVE_TEMPLATE_ID = 450; // 朋友圈单图文
  15. public static $IMAGE_PATH = 'YOUR AD IMAGE PATH'; // 图片文件路径,450规格要求:800x450, <300K, png/jpg
  16. public static $IMAGE_PROFILE_PATH = 'YOUR AD IMAGE PATH'; // Profile图片文件路径,200x200, <300K, png/jpg
  17. public static $AD_TITLE = 'YOUR AD TEXT'; // 广告标题,450规格要求:字数:1~10
  18. public static $AD_DESCRIPTION = 'YOUR AD DESCRIPTION'; // 广告文案,450规格要求:字数:1~30
  19. public static $AD_URL = 'YOUR AD PAGE URL'; // H5外链地址
  20. public function init()
  21. {
  22. $tads = TencentAds::init([
  23. 'access_token' => static::$ACCESS_TOKEN,
  24. 'is_debug' => true,
  25. ]);
  26. $tads->useSandbox(); // 默认访问沙箱环境,如访问正式环境,请切换为$tads->useProduction()
  27. static::$tads = $tads;
  28. return $tads;
  29. }
  30. public function main()
  31. {
  32. try {
  33. /* @var TencentAds $tads */
  34. $tads = static::$tads;
  35. // 第一步,创建推广计划
  36. $campaignType = 'CAMPAIGN_TYPE_NORMAL'; // 普通展示广告
  37. $promotedObjectType = 'PROMOTED_OBJECT_TYPE_LEAD_AD'; // 线索收集
  38. $campaignId = $this->AddCampaign($tads, $campaignType, $promotedObjectType);
  39. // echo 'Campaign ID: ' . $campaignId . PHP_EOL;
  40. // 第二步,创建广告组,不允许使用定向包,因此在广告组里创建定向
  41. $siteSet = ['SITE_SET_MOMENTS']; // 投放微信流量
  42. $adgroupId = $this->AddAdgroup($tads, $campaignId, $promotedObjectType, $siteSet);
  43. // echo 'Adgroup ID: ' . $adgroupId . PHP_EOL;
  44. // 第三步,上传素材
  45. $imageId = $this->AddImage($tads, static::$IMAGE_PATH);
  46. // echo 'Image ID: ' . implode(', ', $imageIdList) . PHP_EOL;
  47. $imageIdLeads = $this->AddImage($tads, static::$IMAGE_PROFILE_PATH);
  48. // echo 'Image ID: ' . $imageIdLeads . PHP_EOL;
  49. $profileId = $this->AddProfile($tads, $promotedObjectType, $imageIdLeads);
  50. // echo 'Profile ID: ' . $profileId . PHP_EOL;
  51. // 第四步,创建创意
  52. $adcreativeId = $this->AddAdcreative($tads, static::$ADCREATIVE_TEMPLATE_ID, $campaignId,
  53. $promotedObjectType, $siteSet, $imageId, static::$AD_TITLE, static::$AD_DESCRIPTION,
  54. static::$AD_URL, $profileId);
  55. // echo 'Adcreative ID: ' . $adcreativeId . PHP_EOL;
  56. // 第五步,创建广告
  57. $adId = $this->AddAd($tads, $adgroupId, $adcreativeId);
  58. // echo 'Ad ID: ' . $adId . PHP_EOL;
  59. return $adId;
  60. } catch (TencentAdsResponseException $e) {
  61. // When Api returns an error
  62. echo 'Tencent ads returned an error: ' . $e->getMessage() . PHP_EOL;
  63. throw $e;
  64. } catch (TencentAdsSDKException $e) {
  65. // When validation fails or other local issues
  66. echo 'Tencent ads SDK returned an error: ' . $e->getMessage() . PHP_EOL;
  67. throw $e;
  68. } catch (Exception $e) {
  69. echo 'Other exception: ' . $e->getMessage() . PHP_EOL;
  70. throw $e;
  71. }
  72. }
  73. // 创建朋友圈推广计划
  74. protected function AddCampaign(TencentAds $tads, $campaignType, $promotedObjectType)
  75. {
  76. $campaignName = 'SDK sample campaign ' . uniqid();
  77. $campaignDailyBudget = 0; // 不限日预算
  78. $campaign = $tads->campaigns()
  79. ->add([
  80. 'account_id' => static::$ACCOUNT_ID,
  81. 'campaign_name' => $campaignName,
  82. 'campaign_type' => $campaignType,
  83. 'promoted_object_type' => $promotedObjectType,
  84. 'daily_budget' => $campaignDailyBudget,
  85. ]);
  86. $campaignId = $campaign->getCampaignId();
  87. return $campaignId;
  88. }
  89. // 创建广告组
  90. protected function AddAdgroup(
  91. TencentAds $tads,
  92. $campaignId,
  93. $promotedObjectType,
  94. $siteSet
  95. ) {
  96. $adgroupName = 'SDK sample adgroup ' . uniqid();
  97. $beginDate = date('Y-m-d', strtotime('+1 day')); // 开始投放日期
  98. $endDate = date('Y-m-d', strtotime('+10 day')); // 结束投放日期
  99. $billingEvent = 'BILLINGEVENT_IMPRESSION'; // CPM
  100. $bidAmount = 5000; // 50元(单位为分)
  101. $optimizationGoal = 'OPTIMIZATIONGOAL_IMPRESSION'; // 优化目标展示
  102. $timeSeries = str_repeat('1', 48 * 7); // 全天投放
  103. $adgroupDailyBudget = 1000000; // 日预算10000元(单位为分)
  104. $targeting = [
  105. 'age' => [
  106. [ // 年龄定向,23~45岁
  107. 'min' => 23,
  108. 'max' => 45,
  109. ],
  110. ],
  111. 'gender' => ['MALE'], // 性别定向,男性
  112. 'geo_location' => [ // 地域定向
  113. 'location_types' => ['LIVE_IN'],
  114. 'regions' => ['110000'], // 北京
  115. ],
  116. ];
  117. $adgroup = $tads->adgroups()
  118. ->add([
  119. 'account_id' => static::$ACCOUNT_ID,
  120. 'campaign_id' => $campaignId,
  121. 'adgroup_name' => $adgroupName,
  122. 'promoted_object_type' => $promotedObjectType,
  123. 'begin_date' => $beginDate,
  124. 'end_date' => $endDate,
  125. 'billing_event' => $billingEvent,
  126. 'bid_amount' => $bidAmount,
  127. 'optimization_goal' => $optimizationGoal,
  128. 'time_series' => $timeSeries,
  129. 'site_set' => $siteSet,
  130. 'daily_budget' => $adgroupDailyBudget,
  131. 'targeting' => $targeting,
  132. ]);
  133. $adgroupId = $adgroup->getAdgroupId();
  134. return $adgroupId;
  135. }
  136. // 上传素材
  137. protected function AddImage(TencentAds $tads, $imageFile)
  138. {
  139. $imageSignature = md5_file($imageFile);
  140. $uploadType = 'UPLOAD_TYPE_FILE';
  141. $image = $tads->images()
  142. ->add([
  143. 'account_id' => static::$ACCOUNT_ID,
  144. 'upload_type' => $uploadType,
  145. 'file' => $imageFile,
  146. 'signature' => $imageSignature,
  147. ]);
  148. $imageId = $image->getImageId();
  149. return $imageId;
  150. }
  151. // 创建微信原生页
  152. protected function AddWechatPage(TencentAds $tads, $pageTemplateId, $imageIdList, $text)
  153. {
  154. $pageName = 'SDK sample wechat page ' . uniqid();
  155. $pageElementsSpecList = [
  156. [
  157. 'element_type' => 'CAROUSEL',
  158. 'image_spec' => [
  159. 'image_id_list' => $imageIdList,
  160. ],
  161. ],
  162. [
  163. 'element_type' => 'BUTTON',
  164. 'button_spec' => [
  165. 'url' => 'http://e.qq.com',
  166. 'title' => $text,
  167. ],
  168. ],
  169. ];
  170. $shareContentSpec = [
  171. 'share_title' => '分享标题',
  172. 'share_description' => '分享内容',
  173. ];
  174. $page = $tads->wechatPages()
  175. ->add([
  176. 'account_id' => static::$ACCOUNT_ID,
  177. 'page_name' => $pageName,
  178. 'page_template_id' => $pageTemplateId,
  179. 'page_elements_spec_list' => $pageElementsSpecList,
  180. 'share_content_spec' => $shareContentSpec,
  181. ]);
  182. return $page->getPageId();
  183. }
  184. // 创建profile
  185. protected function AddProfile(TencentAds $tads, $promotedObjectType, $imageId)
  186. {
  187. $profile = $tads->profiles()
  188. ->add([
  189. 'account_id' => static::$ACCOUNT_ID,
  190. 'profile_name' => 'SDK用例' . uniqid(),
  191. 'profile_type' => 'PROFILE_TYPE_DEFINITION',
  192. 'promoted_object_type' => $promotedObjectType,
  193. 'head_image_id' => $imageId,
  194. 'description' => 'SDK sample',
  195. ]);
  196. return $profile->getProfileId();
  197. }
  198. // 创建创意
  199. protected function AddAdcreative(
  200. TencentAds $tads,
  201. $adcreativeTemplateId,
  202. $campaignId,
  203. $promotedObjectType,
  204. $siteSet,
  205. $imageId,
  206. $adTitle,
  207. $adDescription,
  208. $adUrl,
  209. $profileId
  210. ) {
  211. $adcreativeName = 'SDK sample adcreative ' . uniqid();
  212. $adcreativeElements = [
  213. 'image_list' => [$imageId],
  214. 'title' => $adTitle,
  215. 'description' => $adDescription,
  216. ];
  217. $pageType = 'PAGE_TYPE_DEFAULT';
  218. $pageSpec = [
  219. 'page_url' => $adUrl,
  220. ];
  221. $adcreative = $tads->adcreatives()
  222. ->add([
  223. 'account_id' => static::$ACCOUNT_ID,
  224. 'campaign_id' => $campaignId,
  225. 'adcreative_name' => $adcreativeName,
  226. 'adcreative_template_id' => $adcreativeTemplateId,
  227. 'adcreative_elements' => $adcreativeElements,
  228. 'promoted_object_type' => $promotedObjectType,
  229. 'page_type' => $pageType,
  230. 'page_spec' => $pageSpec,
  231. 'site_set' => $siteSet,
  232. 'profile_id' => $profileId,
  233. ]);
  234. $adcreativeId = $adcreative->getAdcreativeId();
  235. return $adcreativeId;
  236. }
  237. // 创建广告
  238. protected function AddAd(TencentAds $tads, $adgroupId, $adcreativeId)
  239. {
  240. $adName = 'SDK sample ad ' . uniqid();
  241. $ad = $tads->ads()
  242. ->add([
  243. 'account_id' => static::$ACCOUNT_ID,
  244. 'adgroup_id' => $adgroupId,
  245. 'adcreative_id' => $adcreativeId,
  246. 'ad_name' => $adName,
  247. ]);
  248. $adId = $ad->getAdId();
  249. return $adId;
  250. }
  251. }
  252. if (basename($_SERVER['PHP_SELF']) == basename(__FILE__)) {
  253. try {
  254. $example = new AddLeadsAds();
  255. $example->init();
  256. $example->main();
  257. } catch (\Exception $e) {
  258. exit(-1);
  259. }
  260. }