如何使用Google Ads API获取AdSchedule对象

时间:2018-08-16 12:54:10

标签: php ads google-api-php-client

我正在尝试通过Google Ads API将所有广告明细表放置在Google Ads中,并获取开始时间和结束时间(小时和分钟),以将其与某些现有值进行比较,并取决于它们是否相应地更新。

这是我的代码,其中显示了我在哪里对返回的广告时间表进行迭代。

foreach($campaigns as $camp) {
        // Get restaurant and details
        $res = RestaurantsService::getRestaurantByName($camp->getName());
        $hours =$res->getHours()->dequeue();
        $start = explode("-",$hours)[0];
        $end = explode("-",$hours)[1];

        // Get current ad schedules as they are now
        $campaignAdSchedules = self::getCampaignAdSchedule($campaignCriterionService,$camp->getId());
        if ($campaignAdSchedules == null){
            $operations = [];

            $schedule = new AdSchedule();
            $schedule->setDayOfWeek(self::DAYS[date("N")-1]);
            $schedule->setStartHour((int)substr($start,0,2));
            $schedule->setStartMinute(MinuteOfHour::ZERO);
            $schedule->setEndHour((int)substr($end,0,2));
            $schedule->setEndMinute(MinuteOfHour::ZERO);

            $operation = new CampaignCriterionOperation();

            $criterion = new CampaignCriterion();
            $criterion->setCampaignId($camp->getId());
            $criterion->setCriterion($schedule);

            $operation->setOperand($criterion);
            $operation->setOperator(Operator::ADD);
            $operations[] = $operation;

            $campaignCriterionService->mutate($operations);
        } else {
            foreach($campaignAdSchedules as $adSchedule){
                ---> $schedule = $adSchedule->getCriterion(); <---
            }
        }
    }

这里用箭头标记的线是我遇到的线。 getCriterion()函数返回一个Criterion对象,该对象没有方法getStartHour()等。我尝试过强制转换它,但是找不到正确的方法。

非常感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

尝试检查实例:

$result = $campaignCriterionService->get($serviceSelector);
$campaignAdSchedules = $result->getEntries();
foreach ($campaignAdSchedules as $criterion) {
    $adSchedule =  $criterion->getCriterion();
    if ($adSchedule instanceof AdSchedule) {
        $adSchedule->getStartHour();
    }
}