BingAds / BingAds-PHP-SDK

Other
56 stars 46 forks source link

Get Campaigns by Account id #128

Closed amitklahiri closed 4 years ago

amitklahiri commented 4 years ago

Hi.

Thanks for the reply so far.

I am trying to retrieve campaigns id and name by account id. I have got the error. I have written the below code.

Error Fatal error: Uncaught Error: Class 'BingAds\GetCampaignsByAccountIdRequest' not found in E:\wamp64\www\bingads\test.php on line 136 Error: Class 'BingAds\GetCampaignsByAccountIdRequest' not found in E:\wamp64\www\bingads\test.php on line 136

Code

$GLOBALS['CustomerManagementProxy'] = new ServiceClient(
     ServiceClientType::CustomerManagementVersion13, 
     $_SESSION['AuthorizationData'], 
     'Production'
);

// Set the GetUser request parameter to an empty user identifier to get the current 
// authenticated Microsoft Advertising user, and then search for all accounts the user can access.
$getUserRequest = new GetUserRequest();
$getUserRequest->UserId = null;

$user = $GLOBALS['CustomerManagementProxy']->GetService()->GetUser($getUserRequest)->User;

// Search for the Microsoft Advertising accounts that the user can access.
$pageInfo = new Paging();
$pageInfo->Index = 0;    // The first page
$pageInfo->Size = 1000;   // The first 1,000 accounts for this page of results    
$predicate = new Predicate();
$predicate->Field = "UserId";
$predicate->Operator = PredicateOperator::Equals;
$predicate->Value = $user->Id; 

$searchAccountsRequest = new SearchAccountsRequest();
$searchAccountsRequest->Predicates = array($predicate);
$searchAccountsRequest->Ordering = null;
$searchAccountsRequest->PageInfo = $pageInfo;

$accounts = $GLOBALS['CustomerManagementProxy']->GetService()->SearchAccounts($searchAccountsRequest)->Accounts;

foreach ($accounts->AdvertiserAccount as $account) {
     $GLOBALS['CampaignManagementProxy'] = new ServiceClient(
          ServiceClientType::CampaignManagementVersion13,
          $_SESSION['AuthorizationData'],
         'Production'
     );

     $request = new GetCampaignsByAccountIdRequest();
     $request->AccountId = $account->Id;
     $request->CampaignType = 'Search';

     // Error line number. 
     $campaigns = $GLOBALS['CampaignManagementProxy']->GetService()->GetCampaignsByAccountId($request)->Campaigns; 
}

Please help me to resolve the issue.

Regards & Thanks, Amit

javiernunez commented 4 years ago

It seems you are not including the class in your file.

eric-urban commented 4 years ago

Hi @amitklahiri -

@javiernunez is correct. You'll need to add as shown here.

Thanks for your patience, and please let me know if you have any follow up questions.

Best regards, Eric