none
How do I retrieve campaign clicks using REST API in PHP? RRS feed

  • Question

  • Hello. 

    I have just started with Bing Ads API. 

    Environment

    PHP Laravel Framework

    Problem

    I am trying to retrieve campaign clicks, impressions, assists, revenue, spend. manual call, etc. 

    1. How do I retrieve the data using REST API call?

    2. How do I retrieve the data in JSON format?

    3. Can I use CURL while retrieving the data?

    4. Where do I get some examples?

    I am looking for an easiest solution ASAP. Please help.

    Thanks,

    Amit 

    Tuesday, April 14, 2020 7:10 PM

All replies

  • There isn't a REST API available, but to get started using Bing Ads API with PHP I suggest this guide

    Then you can modify ReportRequests.php as needed and run in place of SearchAccounts.php. 

    Please reach out if you have any follow up questions. 

    Eric

    Saturday, April 18, 2020 5:54 PM
    Owner
  • Hi Eric. 

    Thanks for the reply. URL you have referred is for desktop application. I want to use it in PHP web application. If there is no REST is available, then how do I retrieve the data. I wish to retrieve the data of account details, plus, the campaign metrics such as Clicks, Client Cost, Conversions, Impress, Revenue, Spent, Total Visits, etc. 

    What is current process is called? Is it called SOAP? Can I get data in JSON format? Is there any alternative library in Git/Packagist that can help me to retrieve these metrices?

    I am waiting for your feedback and reply. 

    Regards & Thanks, 

    Amit 

    Tuesday, May 12, 2020 4:38 PM
  • Yes the reports can be downloaded via SOAP operations i.e., SubmitGenerateReport and PollGenerateReport. Then you can download from the provisioned URL and extract/parse the report data. Here is a PHP example:

    https://github.com/BingAds/BingAds-PHP-SDK/blob/master/samples/V13/ReportRequests.php#L78

    Please let me know if you have any other questions. 

    Best regards,

    Eric

    Wednesday, May 13, 2020 1:17 PM
    Owner
  • Can you please clear my below doubts?

    1. How do I get all the campaigns under an account id?

    2. How do I get performance and statistics of a campaign using campaign id?

    Regards & Thanks,

    Amit 

    Thursday, May 14, 2020 6:33 PM
  • Hi Eric.

    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';

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

    }

    Please help me to resolve the issue. 

    Regards & Thanks,

    Amit 

    Friday, May 15, 2020 7:10 PM
  • Here's an example call to GetCampaignsByAccountId, although it uses the sample helper classes.

    https://github.com/BingAds/BingAds-PHP-SDK/blob/master/samples/V13/BudgetOpportunities.php#L36

    The helper uses "Microsoft\BingAds\V13\CampaignManagement\GetCampaignsByAccountIdRequest" here:

    https://github.com/BingAds/BingAds-PHP-SDK/blob/master/samples/V13/CampaignManagementExampleHelper.php#L68

    https://github.com/BingAds/BingAds-PHP-SDK/blob/master/samples/V13/CampaignManagementExampleHelper.php#L886

    Does this help?

    Thanks,

    Eric

    Saturday, May 16, 2020 1:45 PM
    Owner