109 lines
3.2 KiB
PHP
109 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace app\controller;
|
|
|
|
use app\model\Ad;
|
|
use app\model\AdGroup;
|
|
use app\model\Campaign;
|
|
use app\model\CampaignBudget;
|
|
use app\service\GoogleAdsService;
|
|
use Webman\Controller;
|
|
|
|
class GoogleAdsController extends Controller
|
|
{
|
|
protected $googleAdsService;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->googleAdsService = new GoogleAdsService();
|
|
}
|
|
|
|
// 创建广告预算
|
|
public function createCampaignBudget($request)
|
|
{
|
|
$data = $request->post();
|
|
$customerId = $data['customer_id'];
|
|
$budgetName = $data['budget_name'];
|
|
$amountMicros = $data['amount_micros'];
|
|
|
|
$budgetResourceName = $this->googleAdsService->createCampaignBudget($customerId, $budgetName, $amountMicros);
|
|
|
|
// 在数据库中保存广告预算信息
|
|
$budget = new CampaignBudget([
|
|
'name' => $budgetName,
|
|
'amount_micros' => $amountMicros,
|
|
]);
|
|
$budget->save();
|
|
|
|
return json(['status' => 'success', 'data' => $budgetResourceName]);
|
|
}
|
|
|
|
// 创建广告系列
|
|
public function createCampaign($request)
|
|
{
|
|
$data = $request->post();
|
|
$customerId = $data['customer_id'];
|
|
$budgetId = $data['budget_id'];
|
|
$campaignName = $data['campaign_name'];
|
|
|
|
$campaignResourceName = $this->googleAdsService->createCampaign($customerId, $budgetId, $campaignName);
|
|
|
|
// 在数据库中保存广告系列信息
|
|
$campaign = new Campaign([
|
|
'name' => $campaignName,
|
|
'budget_id' => $budgetId,
|
|
]);
|
|
$campaign->save();
|
|
|
|
return json(['status' => 'success', 'data' => $campaignResourceName]);
|
|
}
|
|
|
|
// 创建广告组
|
|
public function createAdGroup($request)
|
|
{
|
|
$data = $request->post();
|
|
$customerId = $data['customer_id'];
|
|
$campaignId = $data['campaign_id'];
|
|
$adGroupName = $data['ad_group_name'];
|
|
$cpcBidMicros = $data['cpc_bid_micros'];
|
|
|
|
$adGroupResourceName = $this->googleAdsService->createAdGroup($customerId, $campaignId, $adGroupName, $cpcBidMicros);
|
|
|
|
// 在数据库中保存广告组信息
|
|
$adGroup = new AdGroup([
|
|
'name' => $adGroupName,
|
|
'campaign_id' => $campaignId,
|
|
'cpc_bid_micros' => $cpcBidMicros,
|
|
]);
|
|
$adGroup->save();
|
|
|
|
return json(['status' => 'success', 'data' => $adGroupResourceName]);
|
|
}
|
|
|
|
// 创建广告
|
|
public function createAd($request)
|
|
{
|
|
$data = $request->post();
|
|
$customerId = $data['customer_id'];
|
|
$adGroupId = $data['ad_group_id'];
|
|
$adName = $data['ad_name'];
|
|
$headline = $data['headline'];
|
|
$description = $data['description'];
|
|
$finalUrls = $data['final_urls'];
|
|
|
|
$adResourceName = $this->googleAdsService->createAd($customerId, $adGroupId, $adName, $headline, $description, $finalUrls);
|
|
|
|
// 在数据库中保存广告信息
|
|
$ad = new Ad([
|
|
'name' => $adName,
|
|
'ad_group_id' => $adGroupId,
|
|
'headline' => $headline,
|
|
'description' => $description,
|
|
'final_urls' => json_encode($finalUrls),
|
|
]);
|
|
$ad->save();
|
|
|
|
return json(['status' => 'success', 'data' => $adResourceName]);
|
|
}
|
|
}
|