创建异步任务
<?php
require_once __DIR__ . '/../../../vendor/autoload.php'; // change path as needed
use TencentAds\TencentAds;
use TencentAds\Exception\TencentAdsResponseException;
use TencentAds\Exception\TencentAdsSDKException;
/*****
* 本文件提供了一个创建异步任务(Async task)的简单示例
*/
class AddAsyncTasks
{
public static $tads;
public static $ACCESS_TOKEN = 'YOUR ACCESS TOKEN';
public static $ACCOUNT_ID = 'YOUR ACCOUNT ID';
public static $TASK_TYPE = 'TASK_TYPE_AD_HOURLY_REPORT'; // 广告小时报表数据
public static $DATE = 'REPORT DATE'; // 报表查询日期
public function init()
{
$tads = TencentAds::init([
'access_token' => static::$ACCESS_TOKEN,
'is_debug' => true,
]);
$tads->useSandbox(); // 默认访问沙箱环境,如访问正式环境,请切换为$tads->useProduction()
static::$tads = $tads;
return $tads;
}
public function main()
{
try {
/* @var TencentAds $tads */
$tads = static::$tads;
$taskName = 'SDK异步任务' . uniqid();
$taskSpec = [
'task_type_ad_hourly_report_spec' => [
'date' => static::$DATE,
],
];
$response = $tads->asyncTasks()
->add([
'account_id' => static::$ACCOUNT_ID,
'task_name' => $taskName,
'task_type' => static::$TASK_TYPE,
'task_spec' => $taskSpec,
]);
$taskId = $response->getTaskId();
return $taskId;
} catch (TencentAdsResponseException $e) {
// When Api returns an error
echo 'Tencent ads returned an error: ' . $e->getMessage() . PHP_EOL;
throw $e;
} catch (TencentAdsSDKException $e) {
// When validation fails or other local issues
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;
}
}
}
if (basename($_SERVER['PHP_SELF']) == basename(__FILE__)) {
try {
$example = new AddAsyncTasks();
$example->init();
$example->main();
} catch (\Exception $e) {
exit(-1);
}
}
package com.tencent.ads.examples.BasicOperations.Tools;
import com.tencent.ads.ApiContextConfig;
import com.tencent.ads.TencentAds;
import com.tencent.ads.exception.TencentAdsResponseException;
import com.tencent.ads.exception.TencentAdsSDKException;
import com.tencent.ads.model.*;
import com.tencent.ads.model.AsyncTasksAddRequest;
public class AddAsyncTasks {
/** YOUR ACCESS TOKEN */
public String ACCESS_TOKEN = "YOUR ACCESS TOKEN";
/** TencentAds */
public TencentAds tencentAds;
public String taskName = "SDK异步任务5ede252e2f1db";
public AsyncTasksAddRequest data = new AsyncTasksAddRequest();
public Long accountId = null;
public TaskType taskType = TaskType.TASK_TYPE_AD_HOURLY_REPORT;
public String date = "REPORT DATE";
public void init() {
this.tencentAds = TencentAds.getInstance();
this.tencentAds.init(
new ApiContextConfig().accessToken(ACCESS_TOKEN).isDebug(true)); // debug==true 会打印请求详细信息
this.buildParams();
}
public void buildParams() {
data.setTaskName(taskName);
data.setAccountId(accountId);
data.setTaskType(taskType);
TaskTypeAdHourlyReportSpec taskTypeAdHourlyReportSpec = new TaskTypeAdHourlyReportSpec();
taskTypeAdHourlyReportSpec.setDate(date);
TaskSpec taskSpec = new TaskSpec();
taskSpec.setTaskTypeAdHourlyReportSpec(taskTypeAdHourlyReportSpec);
data.setTaskSpec(taskSpec);
}
public AsyncTasksAddResponseData addAsyncTasks() throws Exception {
AsyncTasksAddResponseData response = tencentAds.asyncTasks().asyncTasksAdd(data);
return response;
}
public static void main(String[] args) {
try {
AddAsyncTasks addAsyncTasks = new AddAsyncTasks();
addAsyncTasks.init();
AsyncTasksAddResponseData response = addAsyncTasks.addAsyncTasks();
} catch (TencentAdsResponseException e) {
e.printStackTrace();
} catch (TencentAdsSDKException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
/*
* Marketing API
*
* Marketing API
*
* API version: 1.3
* Generated by: Swagger Codegen (https://github.com/swagger-api/swagger-codegen.git)
*/
package main
import (
"encoding/json"
"fmt"
"net/http"
"github.com/tencentad/marketing-api-go-sdk/pkg/ads"
"github.com/tencentad/marketing-api-go-sdk/pkg/config"
"github.com/tencentad/marketing-api-go-sdk/pkg/errors"
"github.com/tencentad/marketing-api-go-sdk/pkg/model"
)
type AsyncTasksAddExample struct {
TAds *ads.SDKClient
AccessToken string
Data model.AsyncTasksAddRequest
}
func (e *AsyncTasksAddExample) Init() {
e.AccessToken = "YOUR ACCESS TOKEN"
e.TAds = ads.Init(&config.SDKConfig{
AccessToken: e.AccessToken,
IsDebug: true,
})
e.Data = model.AsyncTasksAddRequest{
TaskName: "SDK异步任务5ede252e2f1db",
AccountId: int64(0),
TaskType: model.TaskType_TASK_TYPE_AD_HOURLY_REPORT,
TaskSpec: &model.TaskSpec{
TaskTypeAdHourlyReportSpec: &model.TaskTypeAdHourlyReportSpec{
Date: "REPORT DATE",
},
},
}
}
func (e *AsyncTasksAddExample) RunExample() (model.AsyncTasksAddResponseData, http.Header, error) {
tads := e.TAds
// change ctx as needed
ctx := *tads.Ctx
return tads.AsyncTasks().Add(ctx, e.Data)
}
func main() {
e := &AsyncTasksAddExample{}
e.Init()
response, headers, err := e.RunExample()
if err != nil {
if resErr, ok := err.(errors.ResponseError); ok {
errStr, _ := json.Marshal(resErr)
fmt.Println("Response error:", string(errStr))
} else {
fmt.Println("Error:", err)
}
}
fmt.Println("Response data:", response)
fmt.Println("Headers:", headers)
}