Skip to content

Commit eb5533b

Browse files
authored
Merge pull request #100 from BingAds/v12-13-1
add v13 proxies and samples
2 parents fdfe741 + 6dd47d9 commit eb5533b

File tree

902 files changed

+56366
-19
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

902 files changed

+56366
-19
lines changed

samples/V12/CampaignManagementExampleHelper.php

Lines changed: 126 additions & 8 deletions
Large diffs are not rendered by default.

samples/V13/AdExtensions.php

Lines changed: 519 additions & 0 deletions
Large diffs are not rendered by default.

samples/V13/AdInsightExampleHelper.php

Lines changed: 2630 additions & 0 deletions
Large diffs are not rendered by default.

samples/V13/AuthHelper.php

Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
1+
<?php
2+
3+
namespace Microsoft\BingAds\Samples\V13;
4+
5+
require_once __DIR__ . "/../vendor/autoload.php";
6+
7+
require_once __DIR__ . "/CustomerManagementExampleHelper.php";
8+
9+
// Specify the Microsoft\BingAds\Auth classes that will be used.
10+
11+
use Microsoft\BingAds\Auth\OAuthDesktopMobileAuthCodeGrant;
12+
use Microsoft\BingAds\Auth\OAuthWebAuthCodeGrant;
13+
use Microsoft\BingAds\Auth\AuthorizationData;
14+
use Microsoft\BingAds\Auth\OAuthTokenRequestException;
15+
use Microsoft\BingAds\Auth\ApiEnvironment;
16+
use Microsoft\BingAds\Auth\ServiceClient;
17+
use Microsoft\BingAds\Auth\ServiceClientType;
18+
19+
// Specify the Microsoft\BingAds\Samples\V13 classes that will be used.
20+
use Microsoft\BingAds\Samples\V13\CustomerManagementExampleHelper;
21+
22+
// Specify the Microsoft\BingAds\V13\CampaignManagement classes that will be used.
23+
use Microsoft\BingAds\V13\CampaignManagement\AdGroupCriterionType;
24+
use Microsoft\BingAds\V13\CampaignManagement\CampaignCriterionType;
25+
use Microsoft\BingAds\V13\CampaignManagement\CampaignType;
26+
27+
// Specify the Microsoft\BingAds\V13\CustomerManagement classes that will be used.
28+
29+
use Microsoft\BingAds\V13\CustomerManagement\GetUserRequest;
30+
use Microsoft\BingAds\V13\CustomerManagement\SearchAccountsRequest;
31+
use Microsoft\BingAds\V13\CustomerManagement\Paging;
32+
use Microsoft\BingAds\V13\CustomerManagement\Predicate;
33+
use Microsoft\BingAds\V13\CustomerManagement\PredicateOperator;
34+
35+
use Exception;
36+
37+
/**
38+
* Defines global settings that you can use for testing your application.
39+
* Your production implementation may vary, and you should always store sensitive information securely.
40+
*/
41+
final class AuthHelper {
42+
43+
const DeveloperToken = 'BBD37VB98'; // For sandbox use BBD37VB98
44+
const ApiEnvironment = ApiEnvironment::Sandbox;
45+
const OAuthRefreshTokenPath = 'refresh.txt';
46+
const ClientId = 'ClientIdGoesHere';
47+
48+
const CampaignTypes =
49+
CampaignType::Audience . ' ' .
50+
CampaignType::Search . ' ' .
51+
CampaignType::Shopping . ' ' .
52+
CampaignType::DynamicSearchAds;
53+
54+
const AllTargetCampaignCriterionTypes =
55+
CampaignCriterionType::Age . ' ' .
56+
CampaignCriterionType::DayTime . ' ' .
57+
CampaignCriterionType::Device . ' ' .
58+
CampaignCriterionType::Gender . ' ' .
59+
CampaignCriterionType::Location . ' ' .
60+
CampaignCriterionType::LocationIntent . ' ' .
61+
CampaignCriterionType::Radius;
62+
63+
const AllTargetAdGroupCriterionTypes =
64+
AdGroupCriterionType::Age . ' ' .
65+
AdGroupCriterionType::DayTime . ' ' .
66+
AdGroupCriterionType::Device . ' ' .
67+
AdGroupCriterionType::Gender . ' ' .
68+
AdGroupCriterionType::Location . ' ' .
69+
AdGroupCriterionType::LocationIntent . ' ' .
70+
AdGroupCriterionType::Radius;
71+
72+
static function Authenticate()
73+
{
74+
// Disable WSDL caching.
75+
ini_set("soap.wsdl_cache_enabled", "0");
76+
ini_set("soap.wsdl_cache_ttl", "0");
77+
78+
// Authenticate with a Microsoft Account.
79+
AuthHelper::AuthenticateWithOAuth();
80+
81+
$GLOBALS['CustomerManagementProxy'] = new ServiceClient(
82+
ServiceClientType::CustomerManagementVersion13,
83+
$GLOBALS['AuthorizationData'],
84+
AuthHelper::GetApiEnvironment()
85+
);
86+
87+
// Set to an empty user identifier to get the current authenticated user,
88+
// and then search for accounts the user can access.
89+
$user = CustomerManagementExampleHelper::GetUser(
90+
null,
91+
true
92+
)->User;
93+
94+
// To retrieve more than 100 accounts, increase the page size up to 1,000.
95+
// To retrieve more than 1,000 accounts you'll need to implement paging.
96+
$accounts = AuthHelper::SearchAccountsByUserId(
97+
$user->Id,
98+
0,
99+
100
100+
)->Accounts;
101+
102+
// We'll use the first account by default for the examples.
103+
104+
$GLOBALS['AuthorizationData']->AccountId = $accounts->AdvertiserAccount[0]->Id;
105+
$GLOBALS['AuthorizationData']->CustomerId = $accounts->AdvertiserAccount[0]->ParentCustomerId;
106+
107+
$GLOBALS['AdInsightProxy'] = new ServiceClient(
108+
ServiceClientType::AdInsightVersion13,
109+
$GLOBALS['AuthorizationData'],
110+
AuthHelper::GetApiEnvironment()
111+
);
112+
113+
$GLOBALS['BulkProxy'] = new ServiceClient(
114+
ServiceClientType::BulkVersion13,
115+
$GLOBALS['AuthorizationData'],
116+
AuthHelper::GetApiEnvironment()
117+
);
118+
119+
$GLOBALS['CampaignManagementProxy'] = new ServiceClient(
120+
ServiceClientType::CampaignManagementVersion13,
121+
$GLOBALS['AuthorizationData'],
122+
AuthHelper::GetApiEnvironment()
123+
);
124+
125+
$GLOBALS['CustomerManagementProxy'] = new ServiceClient(
126+
ServiceClientType::CustomerManagementVersion13,
127+
$GLOBALS['AuthorizationData'],
128+
AuthHelper::GetApiEnvironment()
129+
);
130+
131+
$GLOBALS['ReportingProxy'] = new ServiceClient(
132+
ServiceClientType::ReportingVersion13,
133+
$GLOBALS['AuthorizationData'],
134+
AuthHelper::GetApiEnvironment()
135+
);
136+
}
137+
138+
static function SearchAccountsByUserId($userId, $pageIndex, $pageSize)
139+
{
140+
$GLOBALS['Proxy'] = $GLOBALS['CustomerManagementProxy'];
141+
142+
// Specify the page index and number of account results per page.
143+
144+
$pageInfo = new Paging();
145+
$pageInfo->Index = $pageIndex;
146+
$pageInfo->Size = $pageSize;
147+
148+
$predicate = new Predicate();
149+
$predicate->Field = "UserId";
150+
$predicate->Operator = PredicateOperator::Equals;
151+
$predicate->Value = $userId;
152+
153+
$request = new SearchAccountsRequest();
154+
$request->Ordering = null;
155+
$request->PageInfo = $pageInfo;
156+
$request->Predicates = array($predicate);
157+
158+
return $GLOBALS['Proxy']->GetService()->SearchAccounts($request);
159+
}
160+
161+
// Sets the global authorization data instance with OAuthDesktopMobileAuthCodeGrant.
162+
163+
static function AuthenticateWithOAuth()
164+
{
165+
$authentication = (new OAuthDesktopMobileAuthCodeGrant())
166+
->withEnvironment(AuthHelper::ApiEnvironment)
167+
->withClientId(AuthHelper::ClientId);
168+
169+
$GLOBALS['AuthorizationData'] = (new AuthorizationData())
170+
->withAuthentication($authentication)
171+
->withDeveloperToken(AuthHelper::DeveloperToken);
172+
173+
try
174+
{
175+
$refreshToken = AuthHelper::ReadOAuthRefreshToken();
176+
177+
if($refreshToken != null)
178+
{
179+
$GLOBALS['AuthorizationData']->Authentication->RequestOAuthTokensByRefreshToken($refreshToken);
180+
AuthHelper::WriteOAuthRefreshToken(
181+
$GLOBALS['AuthorizationData']->Authentication->OAuthTokens->RefreshToken
182+
);
183+
}
184+
else
185+
{
186+
AuthHelper::RequestUserConsent();
187+
}
188+
}
189+
catch(OAuthTokenRequestException $e)
190+
{
191+
printf("Error: %s\n", $e->Error);
192+
printf("Description: %s\n", $e->Description);
193+
194+
AuthHelper::RequestUserConsent();
195+
}
196+
}
197+
198+
static function RequestUserConsent()
199+
{
200+
print "You need to provide consent for the application to access your Bing Ads Bing Ads accounts. " .
201+
"Copy and paste this authorization endpoint into a web browser and sign in with a Microsoft account " .
202+
"with access to a Bing Ads account: \n\n" . $GLOBALS['AuthorizationData']->Authentication->GetAuthorizationEndpoint() .
203+
"\n\nAfter you have granted consent in the web browser for the application to access your Bing Ads accounts, " .
204+
"please enter the response URI that includes the authorization 'code' parameter: \n\n";
205+
206+
$responseUri = fgets(STDIN);
207+
print "\n";
208+
209+
$GLOBALS['AuthorizationData']->Authentication->RequestOAuthTokensByResponseUri(trim($responseUri));
210+
AuthHelper::WriteOAuthRefreshToken($GLOBALS['AuthorizationData']->Authentication->OAuthTokens->RefreshToken);
211+
}
212+
213+
static function GetApiEnvironment()
214+
{
215+
return AuthHelper::ApiEnvironment;
216+
}
217+
218+
static function ReadOAuthRefreshToken()
219+
{
220+
$refreshToken = null;
221+
222+
if (file_exists(AuthHelper::OAuthRefreshTokenPath) && filesize(AuthHelper::OAuthRefreshTokenPath) > 0)
223+
{
224+
$refreshTokenfile = @\fopen(AuthHelper::OAuthRefreshTokenPath,"r");
225+
$refreshToken = fread($refreshTokenfile, filesize(AuthHelper::OAuthRefreshTokenPath));
226+
fclose($refreshTokenfile);
227+
}
228+
229+
return $refreshToken;
230+
}
231+
232+
static function WriteOAuthRefreshToken($refreshToken)
233+
{
234+
$refreshTokenfile = @\fopen(AuthHelper::OAuthRefreshTokenPath,"wb");
235+
if (file_exists(AuthHelper::OAuthRefreshTokenPath))
236+
{
237+
fwrite($refreshTokenfile, $refreshToken);
238+
fclose($refreshTokenfile);
239+
}
240+
241+
return;
242+
}
243+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
namespace Microsoft\BingAds\Samples\V13;
4+
5+
// For more information about installing and using the Bing Ads PHP SDK,
6+
// see https://go.microsoft.com/fwlink/?linkid=838593.
7+
8+
require_once __DIR__ . "/../vendor/autoload.php";
9+
10+
include __DIR__ . "/AuthHelper.php";
11+
include __DIR__ . "/AdInsightExampleHelper.php";
12+
include __DIR__ . "/CampaignManagementExampleHelper.php";
13+
14+
use SoapVar;
15+
use SoapFault;
16+
use Exception;
17+
18+
// Specify the Microsoft\BingAds\Auth classes that will be used.
19+
use Microsoft\BingAds\Auth\ServiceClient;
20+
use Microsoft\BingAds\Auth\ServiceClientType;
21+
22+
// Specify the Microsoft\BingAds\Samples classes that will be used.
23+
use Microsoft\BingAds\Samples\V13\AuthHelper;
24+
use Microsoft\BingAds\Samples\V13\AdInsightExampleHelper;
25+
use Microsoft\BingAds\Samples\V13\CampaignManagementExampleHelper;
26+
27+
// Specify the Microsoft\BingAds\V13\CampaignManagement classes that will be used.
28+
use Microsoft\BingAds\V13\CampaignManagement\CampaignAdditionalField;
29+
30+
try
31+
{
32+
// Authenticate user credentials and set the account ID for the sample.
33+
AuthHelper::Authenticate();
34+
35+
print("-----\r\nGetCampaignsByAccountId:\r\n");
36+
$getCampaignsByAccountIdResponse = CampaignManagementExampleHelper::GetCampaignsByAccountId(
37+
$GLOBALS['AuthorizationData']->AccountId,
38+
AuthHelper::CampaignTypes
39+
);
40+
$campaigns = $getCampaignsByAccountIdResponse->Campaigns;
41+
print("Campaigns:\r\n");
42+
CampaignManagementExampleHelper::OutputArrayOfCampaign($campaigns);
43+
44+
// Get the budget opportunities for each campaign in the current account.
45+
46+
foreach ($campaigns->Campaign as $campaign)
47+
{
48+
print("-----\r\nGetBudgetOpportunities:\r\n");
49+
$opportunities = AdInsightExampleHelper::GetBudgetOpportunities(
50+
$campaign->Id
51+
)->Opportunities;
52+
AdInsightExampleHelper::OutputArrayOfBudgetOpportunity($opportunities);
53+
}
54+
}
55+
catch (SoapFault $e)
56+
{
57+
printf("-----\r\nFault Code: %s\r\nFault String: %s\r\nFault Detail: \r\n", $e->faultcode, $e->faultstring);
58+
var_dump($e->detail);
59+
print "-----\r\nLast SOAP request/response:\r\n";
60+
print $GLOBALS['Proxy']->GetWsdl() . "\r\n";
61+
print $GLOBALS['Proxy']->GetService()->__getLastRequest()."\r\n";
62+
print $GLOBALS['Proxy']->GetService()->__getLastResponse()."\r\n";
63+
}
64+
catch (Exception $e)
65+
{
66+
// Ignore fault exceptions that we already caught.
67+
if ($e->getPrevious())
68+
{ ; }
69+
else
70+
{
71+
print $e->getCode()." ".$e->getMessage()."\n\n";
72+
print $e->getTraceAsString()."\n\n";
73+
}
74+
}

0 commit comments

Comments
 (0)