-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #100 from BingAds/v12-13-1
add v13 proxies and samples
- Loading branch information
Showing
902 changed files
with
56,366 additions
and
19 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,243 @@ | ||
<?php | ||
|
||
namespace Microsoft\BingAds\Samples\V13; | ||
|
||
require_once __DIR__ . "/../vendor/autoload.php"; | ||
|
||
require_once __DIR__ . "/CustomerManagementExampleHelper.php"; | ||
|
||
// Specify the Microsoft\BingAds\Auth classes that will be used. | ||
|
||
use Microsoft\BingAds\Auth\OAuthDesktopMobileAuthCodeGrant; | ||
use Microsoft\BingAds\Auth\OAuthWebAuthCodeGrant; | ||
use Microsoft\BingAds\Auth\AuthorizationData; | ||
use Microsoft\BingAds\Auth\OAuthTokenRequestException; | ||
use Microsoft\BingAds\Auth\ApiEnvironment; | ||
use Microsoft\BingAds\Auth\ServiceClient; | ||
use Microsoft\BingAds\Auth\ServiceClientType; | ||
|
||
// Specify the Microsoft\BingAds\Samples\V13 classes that will be used. | ||
use Microsoft\BingAds\Samples\V13\CustomerManagementExampleHelper; | ||
|
||
// Specify the Microsoft\BingAds\V13\CampaignManagement classes that will be used. | ||
use Microsoft\BingAds\V13\CampaignManagement\AdGroupCriterionType; | ||
use Microsoft\BingAds\V13\CampaignManagement\CampaignCriterionType; | ||
use Microsoft\BingAds\V13\CampaignManagement\CampaignType; | ||
|
||
// Specify the Microsoft\BingAds\V13\CustomerManagement classes that will be used. | ||
|
||
use Microsoft\BingAds\V13\CustomerManagement\GetUserRequest; | ||
use Microsoft\BingAds\V13\CustomerManagement\SearchAccountsRequest; | ||
use Microsoft\BingAds\V13\CustomerManagement\Paging; | ||
use Microsoft\BingAds\V13\CustomerManagement\Predicate; | ||
use Microsoft\BingAds\V13\CustomerManagement\PredicateOperator; | ||
|
||
use Exception; | ||
|
||
/** | ||
* Defines global settings that you can use for testing your application. | ||
* Your production implementation may vary, and you should always store sensitive information securely. | ||
*/ | ||
final class AuthHelper { | ||
|
||
const DeveloperToken = 'BBD37VB98'; // For sandbox use BBD37VB98 | ||
const ApiEnvironment = ApiEnvironment::Sandbox; | ||
const OAuthRefreshTokenPath = 'refresh.txt'; | ||
const ClientId = 'ClientIdGoesHere'; | ||
|
||
const CampaignTypes = | ||
CampaignType::Audience . ' ' . | ||
CampaignType::Search . ' ' . | ||
CampaignType::Shopping . ' ' . | ||
CampaignType::DynamicSearchAds; | ||
|
||
const AllTargetCampaignCriterionTypes = | ||
CampaignCriterionType::Age . ' ' . | ||
CampaignCriterionType::DayTime . ' ' . | ||
CampaignCriterionType::Device . ' ' . | ||
CampaignCriterionType::Gender . ' ' . | ||
CampaignCriterionType::Location . ' ' . | ||
CampaignCriterionType::LocationIntent . ' ' . | ||
CampaignCriterionType::Radius; | ||
|
||
const AllTargetAdGroupCriterionTypes = | ||
AdGroupCriterionType::Age . ' ' . | ||
AdGroupCriterionType::DayTime . ' ' . | ||
AdGroupCriterionType::Device . ' ' . | ||
AdGroupCriterionType::Gender . ' ' . | ||
AdGroupCriterionType::Location . ' ' . | ||
AdGroupCriterionType::LocationIntent . ' ' . | ||
AdGroupCriterionType::Radius; | ||
|
||
static function Authenticate() | ||
{ | ||
// Disable WSDL caching. | ||
ini_set("soap.wsdl_cache_enabled", "0"); | ||
ini_set("soap.wsdl_cache_ttl", "0"); | ||
|
||
// Authenticate with a Microsoft Account. | ||
AuthHelper::AuthenticateWithOAuth(); | ||
|
||
$GLOBALS['CustomerManagementProxy'] = new ServiceClient( | ||
ServiceClientType::CustomerManagementVersion13, | ||
$GLOBALS['AuthorizationData'], | ||
AuthHelper::GetApiEnvironment() | ||
); | ||
|
||
// Set to an empty user identifier to get the current authenticated user, | ||
// and then search for accounts the user can access. | ||
$user = CustomerManagementExampleHelper::GetUser( | ||
null, | ||
true | ||
)->User; | ||
|
||
// To retrieve more than 100 accounts, increase the page size up to 1,000. | ||
// To retrieve more than 1,000 accounts you'll need to implement paging. | ||
$accounts = AuthHelper::SearchAccountsByUserId( | ||
$user->Id, | ||
0, | ||
100 | ||
)->Accounts; | ||
|
||
// We'll use the first account by default for the examples. | ||
|
||
$GLOBALS['AuthorizationData']->AccountId = $accounts->AdvertiserAccount[0]->Id; | ||
$GLOBALS['AuthorizationData']->CustomerId = $accounts->AdvertiserAccount[0]->ParentCustomerId; | ||
|
||
$GLOBALS['AdInsightProxy'] = new ServiceClient( | ||
ServiceClientType::AdInsightVersion13, | ||
$GLOBALS['AuthorizationData'], | ||
AuthHelper::GetApiEnvironment() | ||
); | ||
|
||
$GLOBALS['BulkProxy'] = new ServiceClient( | ||
ServiceClientType::BulkVersion13, | ||
$GLOBALS['AuthorizationData'], | ||
AuthHelper::GetApiEnvironment() | ||
); | ||
|
||
$GLOBALS['CampaignManagementProxy'] = new ServiceClient( | ||
ServiceClientType::CampaignManagementVersion13, | ||
$GLOBALS['AuthorizationData'], | ||
AuthHelper::GetApiEnvironment() | ||
); | ||
|
||
$GLOBALS['CustomerManagementProxy'] = new ServiceClient( | ||
ServiceClientType::CustomerManagementVersion13, | ||
$GLOBALS['AuthorizationData'], | ||
AuthHelper::GetApiEnvironment() | ||
); | ||
|
||
$GLOBALS['ReportingProxy'] = new ServiceClient( | ||
ServiceClientType::ReportingVersion13, | ||
$GLOBALS['AuthorizationData'], | ||
AuthHelper::GetApiEnvironment() | ||
); | ||
} | ||
|
||
static function SearchAccountsByUserId($userId, $pageIndex, $pageSize) | ||
{ | ||
$GLOBALS['Proxy'] = $GLOBALS['CustomerManagementProxy']; | ||
|
||
// Specify the page index and number of account results per page. | ||
|
||
$pageInfo = new Paging(); | ||
$pageInfo->Index = $pageIndex; | ||
$pageInfo->Size = $pageSize; | ||
|
||
$predicate = new Predicate(); | ||
$predicate->Field = "UserId"; | ||
$predicate->Operator = PredicateOperator::Equals; | ||
$predicate->Value = $userId; | ||
|
||
$request = new SearchAccountsRequest(); | ||
$request->Ordering = null; | ||
$request->PageInfo = $pageInfo; | ||
$request->Predicates = array($predicate); | ||
|
||
return $GLOBALS['Proxy']->GetService()->SearchAccounts($request); | ||
} | ||
|
||
// Sets the global authorization data instance with OAuthDesktopMobileAuthCodeGrant. | ||
|
||
static function AuthenticateWithOAuth() | ||
{ | ||
$authentication = (new OAuthDesktopMobileAuthCodeGrant()) | ||
->withEnvironment(AuthHelper::ApiEnvironment) | ||
->withClientId(AuthHelper::ClientId); | ||
|
||
$GLOBALS['AuthorizationData'] = (new AuthorizationData()) | ||
->withAuthentication($authentication) | ||
->withDeveloperToken(AuthHelper::DeveloperToken); | ||
|
||
try | ||
{ | ||
$refreshToken = AuthHelper::ReadOAuthRefreshToken(); | ||
|
||
if($refreshToken != null) | ||
{ | ||
$GLOBALS['AuthorizationData']->Authentication->RequestOAuthTokensByRefreshToken($refreshToken); | ||
AuthHelper::WriteOAuthRefreshToken( | ||
$GLOBALS['AuthorizationData']->Authentication->OAuthTokens->RefreshToken | ||
); | ||
} | ||
else | ||
{ | ||
AuthHelper::RequestUserConsent(); | ||
} | ||
} | ||
catch(OAuthTokenRequestException $e) | ||
{ | ||
printf("Error: %s\n", $e->Error); | ||
printf("Description: %s\n", $e->Description); | ||
|
||
AuthHelper::RequestUserConsent(); | ||
} | ||
} | ||
|
||
static function RequestUserConsent() | ||
{ | ||
print "You need to provide consent for the application to access your Bing Ads Bing Ads accounts. " . | ||
"Copy and paste this authorization endpoint into a web browser and sign in with a Microsoft account " . | ||
"with access to a Bing Ads account: \n\n" . $GLOBALS['AuthorizationData']->Authentication->GetAuthorizationEndpoint() . | ||
"\n\nAfter you have granted consent in the web browser for the application to access your Bing Ads accounts, " . | ||
"please enter the response URI that includes the authorization 'code' parameter: \n\n"; | ||
|
||
$responseUri = fgets(STDIN); | ||
print "\n"; | ||
|
||
$GLOBALS['AuthorizationData']->Authentication->RequestOAuthTokensByResponseUri(trim($responseUri)); | ||
AuthHelper::WriteOAuthRefreshToken($GLOBALS['AuthorizationData']->Authentication->OAuthTokens->RefreshToken); | ||
} | ||
|
||
static function GetApiEnvironment() | ||
{ | ||
return AuthHelper::ApiEnvironment; | ||
} | ||
|
||
static function ReadOAuthRefreshToken() | ||
{ | ||
$refreshToken = null; | ||
|
||
if (file_exists(AuthHelper::OAuthRefreshTokenPath) && filesize(AuthHelper::OAuthRefreshTokenPath) > 0) | ||
{ | ||
$refreshTokenfile = @\fopen(AuthHelper::OAuthRefreshTokenPath,"r"); | ||
$refreshToken = fread($refreshTokenfile, filesize(AuthHelper::OAuthRefreshTokenPath)); | ||
fclose($refreshTokenfile); | ||
} | ||
|
||
return $refreshToken; | ||
} | ||
|
||
static function WriteOAuthRefreshToken($refreshToken) | ||
{ | ||
$refreshTokenfile = @\fopen(AuthHelper::OAuthRefreshTokenPath,"wb"); | ||
if (file_exists(AuthHelper::OAuthRefreshTokenPath)) | ||
{ | ||
fwrite($refreshTokenfile, $refreshToken); | ||
fclose($refreshTokenfile); | ||
} | ||
|
||
return; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
namespace Microsoft\BingAds\Samples\V13; | ||
|
||
// For more information about installing and using the Bing Ads PHP SDK, | ||
// see https://go.microsoft.com/fwlink/?linkid=838593. | ||
|
||
require_once __DIR__ . "/../vendor/autoload.php"; | ||
|
||
include __DIR__ . "/AuthHelper.php"; | ||
include __DIR__ . "/AdInsightExampleHelper.php"; | ||
include __DIR__ . "/CampaignManagementExampleHelper.php"; | ||
|
||
use SoapVar; | ||
use SoapFault; | ||
use Exception; | ||
|
||
// Specify the Microsoft\BingAds\Auth classes that will be used. | ||
use Microsoft\BingAds\Auth\ServiceClient; | ||
use Microsoft\BingAds\Auth\ServiceClientType; | ||
|
||
// Specify the Microsoft\BingAds\Samples classes that will be used. | ||
use Microsoft\BingAds\Samples\V13\AuthHelper; | ||
use Microsoft\BingAds\Samples\V13\AdInsightExampleHelper; | ||
use Microsoft\BingAds\Samples\V13\CampaignManagementExampleHelper; | ||
|
||
// Specify the Microsoft\BingAds\V13\CampaignManagement classes that will be used. | ||
use Microsoft\BingAds\V13\CampaignManagement\CampaignAdditionalField; | ||
|
||
try | ||
{ | ||
// Authenticate user credentials and set the account ID for the sample. | ||
AuthHelper::Authenticate(); | ||
|
||
print("-----\r\nGetCampaignsByAccountId:\r\n"); | ||
$getCampaignsByAccountIdResponse = CampaignManagementExampleHelper::GetCampaignsByAccountId( | ||
$GLOBALS['AuthorizationData']->AccountId, | ||
AuthHelper::CampaignTypes | ||
); | ||
$campaigns = $getCampaignsByAccountIdResponse->Campaigns; | ||
print("Campaigns:\r\n"); | ||
CampaignManagementExampleHelper::OutputArrayOfCampaign($campaigns); | ||
|
||
// Get the budget opportunities for each campaign in the current account. | ||
|
||
foreach ($campaigns->Campaign as $campaign) | ||
{ | ||
print("-----\r\nGetBudgetOpportunities:\r\n"); | ||
$opportunities = AdInsightExampleHelper::GetBudgetOpportunities( | ||
$campaign->Id | ||
)->Opportunities; | ||
AdInsightExampleHelper::OutputArrayOfBudgetOpportunity($opportunities); | ||
} | ||
} | ||
catch (SoapFault $e) | ||
{ | ||
printf("-----\r\nFault Code: %s\r\nFault String: %s\r\nFault Detail: \r\n", $e->faultcode, $e->faultstring); | ||
var_dump($e->detail); | ||
print "-----\r\nLast SOAP request/response:\r\n"; | ||
print $GLOBALS['Proxy']->GetWsdl() . "\r\n"; | ||
print $GLOBALS['Proxy']->GetService()->__getLastRequest()."\r\n"; | ||
print $GLOBALS['Proxy']->GetService()->__getLastResponse()."\r\n"; | ||
} | ||
catch (Exception $e) | ||
{ | ||
// Ignore fault exceptions that we already caught. | ||
if ($e->getPrevious()) | ||
{ ; } | ||
else | ||
{ | ||
print $e->getCode()." ".$e->getMessage()."\n\n"; | ||
print $e->getTraceAsString()."\n\n"; | ||
} | ||
} |
Oops, something went wrong.