作者:studytime(有問題可以到作者部落格下留言)
原文連結:https://www.studytime.xin
概述
easy-amazon-advertising
easy-amazon-advertising 是一個開源的非官方的亞馬遜廣告業務sdk。
環境需求
easy-amazon-advertising 的安裝非常簡單,因為它是一個標準的 Composer 包,這意味著任何滿足下列安裝條件的 PHP 專案支援 Composer 都可以使用它。
php: >=7.2.0
guzzlehttp/guzzle: 6.4.*
pimple/pimple: ~3.0
安裝
使用composer安裝
composer require mystudytime/easy-amazon-advertising
說明
亞馬遜廣告業務目前的劃分為三種:
- Sponsored Display 推廣展示廣告
- Sponsored Products 商品推廣廣告
- Sponsored Brands 品牌推廣廣告
亞馬遜廣告的api文件也大體是如此,針對此種情況本sdk也將模組做了類似的劃分。
下面是sdk廣告業務模組的劃分:
- SponsoredProducts
- SponsoredDisplay
- SponsoredBrands
- BaseService
特殊的 BaseService 模組包含了亞馬遜廣告api中未做劃分的部分,包括AccessToken、Portfolios、Profiles等。
BaseService
入門
use easyAmazonAdv\Factory;
$config = [
'clientId' => 'amzn1.application-oa2-client.xxx',
'clientSecret' => '3b758af8d13ee02355764e4e864fxxxx',
'refreshToken' => 'Atzr|IwEBIL5GyUh_H2fdRJnFOk7mmpluKnm7WWUW0kf0tBxAXFMG5VHyiZuIhrspq6hZRHzPM03RMX7v64KrhXaO9xCtylMUQPcN0LolQhH8PNff76Ve5pS6PO9jtNG9kW1K9YtV1WcJDN3KnGpHjk0dUBtbbrn5uh8EGDDVUR_gpqaHXqqdvZ7vuUXTNzstH-tybiAzspzIPxPf7Ans-uyqPNyNTIIM61vA66fvPkH3-c33iLCzrgRtg_jhzWrZuH4K6INBfR8CjKHcK5oNun4DRTiOnCinvUdbHQPwAeI9m581FzUk2Hp4g5KaXO60A6-4me5EMuGSagWfUvTs1MkB1DgPAs_Do3v0TKKN1L6Vab4YgDu6k-bb5pgor_H_LQ24kbfc2Jdyq9BJkSVAAIP5Hh4y3i1qAh18g94Qq_yLWQOxu8ySawTUSDWOkM3_AH5qFS_Yaegfpc96nZE6_eDnycnnheFnXj14-ghaNsROG4LO2RE4n4xnpj9vx9o5aOFVGhf148Cz_VZjiZCg2t_V0Mru7uYRkwz1OUOxxxxxxxxxxx',
'region' => 'NA',
];
$app = Factory::BaseService($config);
$app 在所有相關BaseService的文件都是指 Factory::BaseService 得到的例項,就不在每個頁面單獨寫了。
RefreshToken
您可以使用RefreshToken方法在訪問令牌到期時重新整理訪問令牌。新的訪問令牌將在請求的響應中。
use easyAmazonAdv\Factory;
$config = [
'clientId' => 'amzn1.application-oa2-client.xxx',
'clientSecret' => '3b758af8d13ee02355764e4e864fxxxx',
'refreshToken' => 'Atzr|IwEBIL5GyUh_H2fdRJnFOk7mmpluKnm7WWUW0kf0tBxAXFMG5VHyiZuIhrspq6hZRHzPM03RMX7v64KrhXaO9xCtylMUQPcN0LolQhH8PNff76Ve5pS6PO9jtNG9kW1K9YtV1WcJDN3KnGpHjk0dUBtbbrn5uh8EGDDVUR_gpqaHXqqdvZ7vuUXTNzstH-tybiAzspzIPxPf7Ans-uyqPNyNTIIM61vA66fvPkH3-c33iLCzrgRtg_jhzWrZuH4K6INBfR8CjKHcK5oNun4DRTiOnCinvUdbHQPwAeI9m581FzUk2Hp4g5KaXO60A6-4me5EMuGSagWfUvTs1MkB1DgPAs_Do3v0TKKN1L6Vab4YgDu6k-bb5pgor_H_LQ24kbfc2Jdyq9BJkSVAAIP5Hh4y3i1qAh18g94Qq_yLWQOxu8ySawTUSDWOkM3_AH5qFS_Yaegfpc96nZE6_eDnycnnheFnXj14-ghaNsROG4LO2RE4n4xnpj9vx9o5aOFVGhf148Cz_VZjiZCg2t_V0Mru7uYRkwz1OUOxxxxxxxxxxx',
'region' => 'NA',
];
$app = Factory::BaseService($config);
$result = $app->access_token->RefreshToken();
Profiles
Marketplaces 市場通過位置為各個商戶分配了特定的配置屬性資訊。包含countryCode, currencyCode, timezone等。profileID在呼叫api介面時在header(Amazon-Advertising-API-Scope)中傳入。
listProfiles
$app->profiles->listProfiles();
[[
"profileId":1234567890,
"countryCode":"US",
"currencyCode":"USD",
"dailyBudget":10.00,
"timezone":"America/Los_Angeles",
"accountInfo":{
"marketplaceStringId":"ABC123",
"sellerStringId":"DEF456"
]]
設定profileId
$app->client->profileId = 1234567890;
Portfolios 廣告組合
listPortfolios 廣告組合列表
$app->portfolios->listPortfolios(['portfolioId'=>12,'portfolioState'=>'enable']);
[
[
"portfolioId": 1234567890,
"name": "My Portfolio One",
"budget": [
"amount": 100.0,
"currencyCode": "USD",
"policy": "dateRange"
"startDate": "20181001",
"endDate": "20181229"
],
"inBudget": true,
"state": "enabled",
],
[
"portfolioId": 0123456789,
"name": "My Portfolio Two",
"budget": [
"amount": 50.0,
"currencyCode": "USD",
"policy": "dateRange",
"startDate": "20181001",
"endDate": "20181229"
],
"inBudget": true,
"state": "enabled",
]
]
listPortfoliosEx 廣告組合列表擴充套件欄位
$app->portfolios->listPortfoliosEx(['portfolioId'=>12,'portfolioState'=>'enable']);
[
[
"portfolioId": 1234567890,
"name": "My Portfolio One",
"budget": [
"amount": 100.0,
"currencyCode": "USD",
"policy": "dateRange"
"startDate": "20181001",
"endDate": "20181229"
],
"inBudget": true,
"state": "enabled",
"creationDate": 1526510030,
"lastUpdatedDate": 1526510030,
"servingStatus": "PENDING_START_DATE"
],
[
"portfolioId": 0123456789,
"name": "My Portfolio Two",
"budget": [
"amount": 50.0,
"currencyCode": "USD",
"policy": "dateRange",
"startDate": "20181001",
"endDate": "20181229"
],
"inBudget": true,
"state": "enabled",
"creationDate": 1526510030,
"lastUpdatedDate": 1526510030,
"servingStatus": "PENDING_START_DATE"
]
]
getPortfolio 廣告組合詳情
$app->portfolios->getPortfolio(1234567890);
{
"portfolioId": 1234567890,
"name": "My Portfolio One",
"budget": [
"amount": 100.0,
"currencyCode": "USD",
"policy": "dateRange"
"startDate": "20181231"
"endDate": "null"
],
"inBudget": true,
"state": "enabled"
]
getPortfolioEx 廣告組合擴充套件欄位
$app->portfolios->getPortfolioEx(1234567890);
[
"portfolioId": 1234567890,
"name": "My Portfolio One",
"budget": [
"amount": 100.0,
"policy": "dateRange"
"startDate": "20190131"
"endDate": "20190331"
],
"inBudget": true,
"state": "enabled",
"creationDate": 1526510030,
"lastUpdatedDate": 1526510030,
"servingStatus": "PENDING_START_DATE"
]
createPortfolios 建立廣告組合(批量)
$params = [
[
'name' => 'My Portfolios name',
],
[
'name' => 'My Portfolios two',
'budget'=>[
'amount' => 1.0,
'policy' => 'dateRange',
'startDate' => '20191220',
'endDate' => '20191221',
],
'state'=>'enable'
],
[
'name' => 'My Portfolios three',
'budget'=>[
'amount' => 1.0,
'policy' => 'monthlyRecurring',
'endDate' => '20191221',
],
'state'=>'enable'
],
];
$result = $app->portfolios->createPortfolios($params);
[
[
"code": "SUCCESS",
"portfolioId": 1234567890
],
[
"code": "SUCCESS",
"portfolioId": 0123456789
],
[
"code": "SUCCESS",
"portfolioId": 1234567891
]
]
updatePortfolios 更新廣告組合(批量)
$params = [
[
'portfolioId'=>1234567890,
'name' => 'My Portfolios name',
],
[
'portfolioId'=>0123456789,
'name' => 'My Portfolios two',
'budget'=>[
'amount' => 1.0,
'policy' => 'dateRange',
'startDate' => '20191220',
'endDate' => '20191221',
],
'state'=>'enable'
],
[
'portfolioId'=>1234567891,
'name' => 'My Portfolios three',
'budget'=>[
'amount' => 1.0,
'policy' => 'monthlyRecurring',
'endDate' => '20191221',
],
'state'=>'enable'
],
];
$result = $app->portfolios->updatePortfolios($params);
[
[
"code": "SUCCESS",
"portfolioId": 1234567890
],
[
"code": "SUCCESS",
"portfolioId": 0123456789
],
[
"code": "SUCCESS",
"portfolioId": 1234567891
]
]
SponsoredDisplay
入門
use easyAmazonAdv\Factory;
$config = [
'clientId' => 'amzn1.application-oa2-client.xxx',
'clientSecret' => '3b758af8d13ee02355764e4e864fxxxx',
'refreshToken' => 'Atzr|IwEBIL5GyUh_H2fdRJnFOk7mmpluKnm7WWUW0kf0tBxAXFMG5VHyiZuIhrspq6hZRHzPM03RMX7v64KrhXaO9xCtylMUQPcN0LolQhH8PNff76Ve5pS6PO9jtNG9kW1K9YtV1WcJDN3KnGpHjk0dUBtbbrn5uh8EGDDVUR_gpqaHXqqdvZ7vuUXTNzstH-tybiAzspzIPxPf7Ans-uyqPNyNTIIM61vA66fvPkH3-c33iLCzrgRtg_jhzWrZuH4K6INBfR8CjKHcK5oNun4DRTiOnCinvUdbHQPwAeI9m581FzUk2Hp4g5KaXO60A6-4me5EMuGSagWfUvTs1MkB1DgPAs_Do3v0TKKN1L6Vab4YgDu6k-bb5pgor_H_LQ24kbfc2Jdyq9BJkSVAAIP5Hh4y3i1qAh18g94Qq_yLWQOxu8ySawTUSDWOkM3_AH5qFS_Yaegfpc96nZE6_eDnycnnheFnXj14-ghaNsROG4LO2RE4n4xnpj9vx9o5aOFVGhf148Cz_VZjiZCg2t_V0Mru7uYRkwz1OUOxxxxxxxxxxx',
'region' => 'NA',
];
$app = Factory::SponsoredDisplay($config);
設定profileId
$app->client->profileId = 1234567890;
Campaigns 廣告活動
getCampaign 廣告活動詳情
$result = $app->campaigns->listCampaigns(1234567890);
[
"campaignId": 1234567890,
"name": "CampaignOne",
"tactic": "remarketing",
"budgetType": "daily",
"budget": "3.00",
"startDate": "20190101",
"endDate": null,
"state": "enabled"
]
getCampaignEx 廣告活動詳情(支援擴充套件欄位展示)
$result = $app->campaigns->listCampaigns(1234567890);
[
"campaignId": 1234567890,
"name": "CampaignOne",
"tactic": "remarketing",
"budgetType": "daily",
"budget": 0,
"startDate": "20190101",
"endDate": "20190102",
"state": "enabled",
"servingStatus": "ADVERTISER_STATUS_ENABLED",
"creationDate": 0,
"lastUpdatedDate": 0
]
createCampaigns 建立廣告活動
$params = [
[
"name" => "My Campaign One",
"tactic" => "remarketing",
"budgetType": "daily",
"budget": 3.00,
"startDate": "20190101",
"endDate": null,
"state": "enabled"
]
];
$result = $app->campaigns->createCampaigns(params);
[
[
"code": "SUCCESS",
"campaignId": 173284463890123
]
]
updateCampaigns
$params = [
[
"campaignId" => 173284463890123,
"name" => "Update Campaign One",
"state" => "enabled",
"budget" => 10.99
]
];
$result = $app->campaigns->updateCampaigns(params);
[
[
"code": "SUCCESS",
"campaignId": 173284463890123
]
]
archiveCampaign 歸檔廣告活動
$result = $app->campaigns->archiveCampaign(173284463890123);
[
"code": "SUCCESS",
"campaignId": 1234567890
]
listCampaigns
$result = = $app->campaigns->listCampaigns(["stateFilter" => "enabled"]);
[
[
"campaignId": 1234567890,
"name": "Campaign one",
"tactic": "remarketing",
"budgetType": "daily",
"budget": "3.00",
"startDate": "20190101",
"endDate": null,
"state": "enabled"
]
]
listCampaignsEx
$result = = $app->campaigns->listCampaignsEx(["stateFilter" => "enabled"]);
[
[
"campaignId": 1234567890,
"name": "Campaign one",
"tactic": "remarketing",
"budgetType": "daily",
"budget": 0,
"startDate": "20190101",
"endDate": "20190102",
"state": "enabled",
"servingStatus": "ADVERTISER_STATUS_ENABLED",
"creationDate": 0,
"lastUpdatedDate": 0
]
]
本作品採用《CC 協議》,轉載必須註明作者和本文連結