GetCampaigns.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. * 本文件提供了一个获取推广计划(Campaign)列表的简单示例
  8. */
  9. class GetCampaigns
  10. {
  11. public static $tads;
  12. public static $ACCESS_TOKEN = 'YOUR ACCESS TOKEN';
  13. public static $ACCOUNT_ID = 'YOUR ACCOUNT ID';
  14. public function init()
  15. {
  16. $tads = TencentAds::init([
  17. 'access_token' => static::$ACCESS_TOKEN,
  18. 'is_debug' => true,
  19. ]);
  20. $tads->useSandbox(); // 默认访问沙箱环境,如访问正式环境,请切换为$tads->useProduction()
  21. static::$tads = $tads;
  22. return $tads;
  23. }
  24. public function main()
  25. {
  26. try {
  27. /* @var TencentAds $tads */
  28. $tads = static::$tads;
  29. $filtering = [
  30. [
  31. 'field' => 'promoted_object_type',
  32. 'operator' => 'EQUALS',
  33. 'values' => ['PROMOTED_OBJECT_TYPE_APP_IOS'],
  34. ],
  35. ]; // 过滤条件
  36. $fields = ['campaign_id', 'campaign_name']; // 需要返回的字段
  37. $response = $tads->campaigns()
  38. ->get([
  39. 'account_id' => static::$ACCOUNT_ID,
  40. 'filtering' => $filtering,
  41. 'fields' => $fields,
  42. ]);
  43. // 从返回里获得Campaigns信息
  44. foreach ($response->getList() as $campaignInfo) {
  45. // echo 'Campaign ID: ' . $campaignInfo->getCampaignId() . PHP_EOL;
  46. // echo 'Campaign name: ' . $campaignInfo->getCampaignName() . PHP_EOL;
  47. }
  48. } catch (TencentAdsResponseException $e) {
  49. // When Api returns an error
  50. echo 'Tencent ads returned an error: ' . $e->getMessage() . PHP_EOL;
  51. throw $e;
  52. } catch (TencentAdsSDKException $e) {
  53. // When validation fails or other local issues
  54. echo 'Tencent ads SDK returned an error: ' . $e->getMessage() . PHP_EOL;
  55. throw $e;
  56. } catch (Exception $e) {
  57. echo 'Other exception: ' . $e->getMessage() . PHP_EOL;
  58. throw $e;
  59. }
  60. }
  61. }
  62. if (basename($_SERVER['PHP_SELF']) == basename(__FILE__)) {
  63. try {
  64. $example = new GetCampaigns();
  65. $example->init();
  66. $example->main();
  67. } catch (\Exception $e) {
  68. exit(-1);
  69. }
  70. }