0

I'm trying to download the Bing ads user location performance report.

my php code is :

define("ADCENTER_API_ENDPOINT", "https://reporting.api.bingads.microsoft.com/Api/Advertiser/Reporting/v13"); 
define("ADCENTER_API_NAMESPACE", "https://bingads.microsoft.com/Reporting/v13");
define("ADCENTER_API_REPORT_SERVICE", ADCENTER_API_ENDPOINT . "/ReportingService.svc?wsdl");

try {
    $headers = array(
            new SoapHeader(ADCENTER_API_NAMESPACE, 'DeveloperToken', DEVELOPER_TOKEN, false),
            new SoapHeader(ADCENTER_API_NAMESPACE, 'AuthenticationToken', $access_token, false),
            new SoapHeader(ADCENTER_API_NAMESPACE, 'CustomerAccountId', CUSTOMER_ACCOUNT_ID, false),
            new SoapHeader(ADCENTER_API_NAMESPACE, 'CustomerId', CUSTOMER_ID, false),
            new SoapHeader(ADCENTER_API_NAMESPACE, 'Password', USER_PASS, false),
            new SoapHeader(ADCENTER_API_NAMESPACE, 'UserName', USER_EMAIL, false)
        );

$request = array(
        "Format" => 'Csv',  
        "ReportName" => 'UserLocationPerformanceReport',
        "Aggregation" => 'Hourly',
        "Columns" => array('CampaignName','CampaignStatus','Country','LocationId','TimePeriod','AccountName','Ctr','Spend','AverageCpc'),   
        "Scope" => array('AccountIds' => [CUSTOMER_ACCOUNT_ID]),
         "Time" => array('PredefinedTime' => 'Today')
    );


    // Specify the type of report
    // $reportType = "AdPerformanceReportRequest";
    $reportType = "UserLocationPerformanceReport";
    // Create the SOAP client
    $client = new SOAPClient(ADCENTER_API_REPORT_SERVICE);

    // Encode the request
    // $params = array('ReportRequest' => new SoapVar($request, SOAP_ENC_ARRAY, $reportType, ADCENTER_API_NAMESPACE));
    $params = array('UserLocationPerformanceReportRequest' => new SoapVar($request, SOAP_ENC_ARRAY, $reportType, ADCENTER_API_NAMESPACE));

    // Schedule report
    $result = $client->__soapCall("SubmitGenerateReport", array("SubmitGenerateReportRequest" => $params), null, $headers); 

    // Get the report ID
    $reportRequestId = $result->ReportRequestId;

    // Wait for the report to complete
    $pollDelay = 20;
    $params = array('ReportRequestId' => $reportRequestId);
    $reportStatus = "Pending";

    while ($reportStatus == "Pending") {
        sleep($pollDelay);
        $result = $client->__soapCall("PollGenerateReport", array('PollGenerateReportRequest' => $params), null, $headers);
        $reportStatus = $result->ReportRequestStatus->Status;
    }

    // Download the report
    if ($reportStatus == 'Success') {
        $downloadURL = $result->ReportRequestStatus->ReportDownloadUrl;
        $filename = CUSTOMER_ACCOUNT_ID.".zip";

        file_put_contents($filename, file_get_contents($downloadURL));
    } else {
        // Error occured
        print "Report download failed";
    }
} catch (Exception $e) {
    print_r($e);
    print $client->__getLastRequest() . "\n";
    print $client->__getLastResponse() . "\n";
}

I'm getting this response.

SoapFault Object
(
    [message:protected] => Invalid client data. Check the SOAP fault details for more information
    [string:Exception:private] => 
    [code:protected] => 0
    [file:protected] => E:\xampp\htdocs\searchops\crons\bingapicron.php
    [line:protected] => 145
    [trace:Exception:private] => Array
        (
            [0] => Array
                (
                    [file] => E:\xampp\htdocs\searchops\crons\bingapicron.php
                    [line] => 145
                    [function] => __soapCall
                    [class] => SoapClient
                    [type] => ->
                    [args] => Array
                        (
                            [0] => SubmitGenerateReport
                            [1] => Array
                                (
                                    [SubmitGenerateReportRequest] => Array
                                        (
                                            [UserLocationPerformanceReportRequest] => SoapVar Object
                                                (
                                                    [enc_type] => 300
                                                    [enc_value] => Array
                                                        (
                                                            [Format] => Csv
                                                            [ReportName] => UserLocationPerformanceReport
                                                            [Aggregation] => Hourly
                                                            [Columns] => Array
                                                                (
                                                                    [0] => CampaignName
                                                                    [1] => CampaignStatus
                                                                    [2] => Country
                                                                    [3] => LocationId
                                                                    [4] => TimePeriod
                                                                    [5] => AccountName
                                                                    [6] => Ctr
                                                                    [7] => Spend
                                                                    [8] => AverageCpc
                                                                )

                                                            [Scope] => Array
                                                                (
                                                                    [AccountIds] => Array
                                                                        (
                                                                            [0] => id_here
                                                                        )

                                                                )

                                                            [Time] => Array
                                                                (
                                                                    [PredefinedTime] => Today
                                                                )

                                                        )

                                                    [enc_stype] => UserLocationPerformanceReport
                                                    [enc_ns] => https://bingads.microsoft.com/Reporting/v13
                                                )

                                        )

                                )

                            [2] => 
                            [3] => Array
                                (
                                    [0] => SoapHeader Object
                                        (
                                            [namespace] => https://bingads.microsoft.com/Reporting/v13
                                            [name] => DeveloperToken
                                            [data] => token_here
                                            [mustUnderstand] => 
                                        )

                                    [1] => SoapHeader Object
                                        (
                                            [namespace] => https://bingads.microsoft.com/Reporting/v13
                                            [name] => AuthenticationToken
                                            [data] => auth_token_here
                                            [mustUnderstand] => 
                                        )

                                    [2] => SoapHeader Object
                                        (
                                            [namespace] => https://bingads.microsoft.com/Reporting/v13
                                            [name] => CustomerAccountId
                                            [data] => c_a_id_here
                                            [mustUnderstand] => 
                                        )

                                    [3] => SoapHeader Object
                                        (
                                            [namespace] => https://bingads.microsoft.com/Reporting/v13
                                            [name] => CustomerId
                                            [data] => c_id_here
                                            [mustUnderstand] => 
                                        )

                                    [4] => SoapHeader Object
                                        (
                                            [namespace] => https://bingads.microsoft.com/Reporting/v13
                                            [name] => Password
                                            [data] => pwd_here
                                            [mustUnderstand] => 
                                        )

                                    [5] => SoapHeader Object
                                        (
                                            [namespace] => https://bingads.microsoft.com/Reporting/v13
                                            [name] => UserName
                                            [data] => email_here
                                            [mustUnderstand] => 
                                        )

                                )

                        )

                )

        )

    [previous:Exception:private] => 
    [faultstring] => Invalid client data. Check the SOAP fault details for more information
    [faultcode] => s:Server
    [detail] => stdClass Object
        (
            [ApiFaultDetail] => stdClass Object
                (
                    [TrackingId] => eeb24fc5-9267-4987-9239-d2575cd8d7e6
                    [BatchErrors] => 
                    [OperationErrors] => stdClass Object
                        (
                            [OperationError] => stdClass Object
                                (
                                    [Code] => 100
                                    [Details] => 
                                    [ErrorCode] => NullRequest
                                    [Message] => The request message is null.
                                )

                        )

                )

        )

)

I've included all the required columns also in columns array. my refresh toke and access token are generated and api is giving correct response for authentication.

why it is showing error. can anyone help me ?

2 Answers2

0

The request is empty i.e., you are not setting the ReportRequest as SubmitGenerateReport expects e.g., it should not be an array. I reproduced the same error by commenting out this line from the report sample helper:

//$request->ReportRequest = $reportRequest;

There is an example on GitHub that creates the request and uses the sample helper to submit the report request, etc. See the details of ServiceClient (sample usage + SDK class) in case it helps map to your own implementation.

To get all of the samples and helper classes I cloned the Bing Ads SDK repository:

git clone https://github.com/BingAds/BingAds-PHP-SDK.git

Then installed the SDK from the samples directory via Composer (install instructions at learn.microsoft.com):

PS C:\dev\github\BingAds-PHP-SDK\samples> composer require microsoft/bingads
No composer.json in current directory, do you want to use the one at C:\dev\github\BingAds-PHP-SDK? [Y,n]? n
Using version v0.12.13.4 for microsoft/bingads
./composer.json has been created
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
  - Installing microsoft/bingads (v0.12.13.4): Downloading (100%)
Writing lock file
Generating autoload files
PS C:\dev\github\BingAds-PHP-SDK\samples> php.exe .\v13\ReportRequests.php
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:

https://login.live-int.com/oauth20_authorize.srf?scope=bingads.manage&prompt=login&client_id=db41b09d-6e50-4f4a-90ac-5a99caefb52f&response_type=code&redirect_uri=https://login.live-int.com/oauth20_desktop.srf

After 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:

Paste the result per OAuth consent instructions in the script above and then ReportRequests.php should continue to execute i.e., submit the report request.

Here is a var_dump of the $reportRequest:

object(SoapVar)#79 (4) {
  ["enc_type"]=>
  int(301)
  ["enc_value"]=>
  object(Microsoft\BingAds\V13\Reporting\AccountPerformanceReportRequest)#35 (11) {
    ["Aggregation"]=>
    string(6) "Weekly"
    ["Columns"]=>
    array(9) {
      [0]=>
      string(10) "TimePeriod"
      [1]=>
      string(9) "AccountId"
      [2]=>
      string(11) "AccountName"
      [3]=>
      string(6) "Clicks"
      [4]=>
      string(11) "Impressions"
      [5]=>
      string(3) "Ctr"
      [6]=>
      string(10) "AverageCpc"
      [7]=>
      string(5) "Spend"
      [8]=>
      string(8) "DeviceOS"
    }
    ["Filter"]=>
    NULL
    ["Scope"]=>
    object(Microsoft\BingAds\V13\Reporting\AccountReportScope)#77 (1) {
      ["AccountIds"]=>
      array(1) {
        [0]=>
        int(MyAccountIdWasHere)
      }
    }
    ["Time"]=>
    object(Microsoft\BingAds\V13\Reporting\ReportTime)#80 (4) {
      ["CustomDateRangeEnd"]=>
      NULL
      ["CustomDateRangeStart"]=>
      NULL
      ["PredefinedTime"]=>
      string(9) "Yesterday"
      ["ReportTimeZone"]=>
      NULL
    }
    ["ExcludeColumnHeaders"]=>
    NULL
    ["ExcludeReportFooter"]=>
    NULL
    ["ExcludeReportHeader"]=>
    NULL
    ["Format"]=>
    string(3) "Tsv"
    ["ReportName"]=>
    string(29) "My Account Performance Report"
    ["ReturnOnlyCompleteData"]=>
    bool(false)
  }
  ["enc_stype"]=>
  string(31) "AccountPerformanceReportRequest"
  ["enc_ns"]=>
  string(43) "https://bingads.microsoft.com/Reporting/v13"
}

I hope this helps!

Eric Urban
  • 582
  • 2
  • 7
  • actually **$request->ReportRequest = $reportRequest; ** is not a problem. the problem was with this : [SubmitGenerateReportRequest] => Array ( [UserLocationPerformanceReportRequest] => SoapVar Object ( It should as : [SubmitGenerateReportRequest] => Array ( [ReportRequest] => SoapVar Object ( – DINESH Kumar SISODIYA Aug 26 '19 at 07:11
0

Yes, previous answer was right - request param structure was incorrect. This is a part of my working code w/o bing sdk.

    $headers = [
        new SoapHeader(
                'https://bingads.microsoft.com/Reporting/v13',
                'CustomerAccountId',
                ...
            ),
            new SoapHeader(
                'https://bingads.microsoft.com/Reporting/v13',
                'CustomerId',
                ...
            ),
            new SoapHeader(
                'https://bingads.microsoft.com/Reporting/v13',
                'DeveloperToken',
                ...
            ),
            new SoapHeader(
                'https://bingads.microsoft.com/Reporting/v13',
                'AuthenticationToken',
                $this->getToken()
            ),
    ];

    $context = [
        'http' => [
            'header' => "Authorization: Bearer {$this->getOAuthToken()}",
        ],
        'ssl' => [
            'verify_peer' => false,
            'verify_peer_name' => false,
        ],
    ];

    $options = array(
            'stream_context' => stream_context_create($context),
      'trace' => TRUE,
      'exceptions' => TRUE,
      'features' => SOAP_SINGLE_ELEMENT_ARRAYS,
      // Disable keep-alive to avoid 'Process open FD table is full'
      'keep-alive' => FALSE,
      'user_agent' => 'BingAdsSDKPHP ' . '13.0.1 ' . PHP_VERSION,

      /**
       * Map long type to string type. For details, see
       * from_long_xml and to_long_xml callbacks.
       */
      'typemap' => array(
        array(
            'type_ns' => 'http://www.w3.org/2001/XMLSchema',
            'type_name' => 'xs:long',
            'to_xml' => 'to_long_xml',
            'from_xml' => 'from_long_xml'
        ),
      )
    );

    $SoapClient = new SOAPClient(static::WSDL_REPORTING, $options);

    $SoapClient->__setSoapHeaders($headers);

and request itself

    $request = [
        'ReportRequest' => new SoapVar(
        [
            'Format' => 'Tsv',
            'ReportName' => 'My Account Performance Report',
            'ReturnOnlyCompleteData' => false,
            'Aggregation' => 'Weekly',
            'Scope' => ['AccountIds' => [...]],
            'Time' => ['PredefinedTime' => 'Yesterday'],
            'Columns' => [
                  "TimePeriod",
                  "AccountId",
                  "AccountName",
                  "Clicks",
                  "Impressions",
                  "Ctr",
                  "AverageCpc",
                  "Spend",
                  "DeviceOS"
            ]
        ],
        SOAP_ENC_OBJECT,
        'AccountPerformanceReportRequest',
         "https://bingads.microsoft.com/Reporting/v13"
    )];

    $response = $SoapClient->SubmitGenerateReport($request);