AddTargetings.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 AddTargetings
  10. {
  11. public static $tads;
  12. public static $ACCESS_TOKEN = 'YOUR ACCESS TOKEN';
  13. public static $ACCOUNT_ID = 'YOUR ACCOUNT ID';
  14. // 本示例会提供创建两个定向人群包,其中一个是包含的人群包,另一个是排除的人群包
  15. public static $AUDIENCE_FILE_POS_IMEI = 'YOUR AUDIENCE FILE PATH';
  16. public static $AUDIENCE_FILE_NEG_IMEI = 'YOUR AUDIENCE FILE PATH';
  17. public function init()
  18. {
  19. $tads = TencentAds::init([
  20. 'access_token' => static::$ACCESS_TOKEN,
  21. 'is_debug' => true,
  22. ]);
  23. $tads->useSandbox(); // 默认访问沙箱环境,如访问正式环境,请切换为$tads->useProduction()
  24. static::$tads = $tads;
  25. return $tads;
  26. }
  27. public function main()
  28. {
  29. try {
  30. /* @var TencentAds $tads */
  31. $tads = static::$tads;
  32. // 第一步,获取地域ID,用于地域定向,微信朋友圈广告必须
  33. $regionNames = ['北京市', '上海市', '广东省'];
  34. $regions = $this->getTargetingRegion($tads, $regionNames);
  35. // echo 'Region IDs: ' . implode(', ', $regions) . PHP_EOL;
  36. // 第二步,创建人群包,可选,可创建定向用户群和排除用户群
  37. $positiveAudienceId = $this->addCustomAudience($tads, static::$AUDIENCE_FILE_POS_IMEI);
  38. $negativeAudienceId = $this->addCustomAudience($tads, static::$AUDIENCE_FILE_NEG_IMEI);
  39. // echo 'Audience IDs: ' . $positiveAudienceId . ', -' . $negativeAudienceId . PHP_EOL;
  40. // 第三步,创建定向包
  41. $targetingId = $this->addTargeting($tads, $regions, [$positiveAudienceId], [$negativeAudienceId]);
  42. // echo 'Targeting ID: ' . $targetingId . PHP_EOL;
  43. return $targetingId;
  44. } catch (TencentAdsResponseException $e) {
  45. // When Api returns an error
  46. echo 'Tencent ads returned an error: ' . $e->getMessage() . PHP_EOL;
  47. throw $e;
  48. } catch (TencentAdsSDKException $e) {
  49. // When validation fails or other local issues
  50. echo 'Tencent ads SDK returned an error: ' . $e->getMessage() . PHP_EOL;
  51. throw $e;
  52. } catch (Exception $e) {
  53. echo 'Other exception: ' . $e->getMessage() . PHP_EOL;
  54. throw $e;
  55. }
  56. }
  57. // 从区域名称得到区域定向ID
  58. protected function getTargetingRegion(TencentAds $tads, $regionNameList)
  59. {
  60. $targetingTags = $tads->targetingTags()
  61. ->get([
  62. 'type' => 'REGION',
  63. ]);
  64. $regions = [];
  65. foreach ($targetingTags->getList() as $region) {
  66. if (in_array($region->getName(), $regionNameList)) {
  67. // 名称匹配一致
  68. $regions [] = $region->getId();
  69. }
  70. }
  71. return $regions;
  72. }
  73. // 从IMEI文件上传一个新的人群包
  74. protected function addCustomAudience(TencentAds $tads, $audienceFilePath)
  75. {
  76. $audienceName = 'SDK sample aud ' . uniqid();
  77. $audienceDescription = 'created by SDK samples';
  78. $audience = $tads->customAudiences()
  79. ->add([
  80. 'account_id' => static::$ACCOUNT_ID,
  81. "name" => $audienceName,
  82. "type" => "CUSTOMER_FILE",
  83. "description" => $audienceDescription,
  84. ]);
  85. $audienceId = $audience->getAudienceId();
  86. $audienceFile = $tads->customAudienceFiles()
  87. ->add([
  88. 'account_id' => static::$ACCOUNT_ID,
  89. 'audience_id' => $audienceId,
  90. 'user_id_type' => "IMEI",
  91. 'file' => $audienceFilePath,
  92. ]); // 往人群包里上传
  93. $audienceFileId = $audienceFile->getCustomAudienceFileId();
  94. return $audienceId;
  95. }
  96. // 创建一个定向包
  97. protected function addTargeting(TencentAds $tads, $regions, $positionAudiences = [], $negativeAudiences = [])
  98. {
  99. $targetingName = 'SDK sample targeting ' . uniqid();
  100. $targetingDescription = 'created by SDK samples';
  101. $targetingDetail = [
  102. 'age' => [
  103. [ // 年龄定向,23~45岁
  104. 'min' => 23,
  105. 'max' => 45,
  106. ],
  107. ],
  108. 'gender' => ['MALE'], // 性别定向,男性
  109. 'geo_location' => [ // 地域定向
  110. 'location_types' => ['LIVE_IN'],
  111. 'regions' => $regions,
  112. ],
  113. 'user_os' => ['IOS'], // 操作系统定向
  114. 'custom_audience' => $positionAudiences, // 定向人群
  115. 'excluded_custom_audience' => $negativeAudiences, // 排除的定向人群
  116. ];
  117. $targeting = $tads->targetings()
  118. ->add([
  119. 'account_id' => static::$ACCOUNT_ID,
  120. 'targeting_name' => $targetingName,
  121. 'targeting' => $targetingDetail,
  122. 'description' => $targetingDescription,
  123. ]);
  124. $targetingId = $targeting->getTargetingId();
  125. return $targetingId;
  126. }
  127. }
  128. if (basename($_SERVER['PHP_SELF']) == basename(__FILE__)) {
  129. try {
  130. $example = new AddTargetings();
  131. $example->init();
  132. $example->main();
  133. } catch (\Exception $e) {
  134. exit(-1);
  135. }
  136. }