0

我正在尝试下载 Bing 广告用户位置效果报告。

我的 php 代码是:

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";
}

我收到了这个回复。

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.
                                )

                        )

                )

        )

)

我已经在列数组中包含了所有必需的列。生成了我的刷新令牌和访问令牌,并且 api 为身份验证提供了正确的响应。

为什么它显示错误。谁能帮我 ?

4

2 回答 2

0

是的,之前的答案是正确的 - 请求参数结构不正确。这是我没有 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);

并要求自己

    $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);
于 2019-11-29T11:42:04.573 回答
0

该请求是空的,即您没有按照 SubmitGenerateReport 的预期设置 ReportRequest,例如,它不应该是一个数组。我通过从报告示例助手中注释掉这一行来重现相同的错误:

//$request->ReportRequest = $reportRequest;

GitHub 上有一个创建请求的示例,并使用示例帮助程序提交报告请求等。请参阅 ServiceClient 的详细信息(示例用法+ SDK 类)以帮助映射到您自己的实现。

为了获取所有示例和帮助类,我克隆了 Bing Ads SDK 存储库:

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

然后通过 Composer 从示例目录安装 SDK(在docs.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:

根据 OAuth 同意说明将结果粘贴到上述脚本中,然后 ReportRequests.php 应继续执行,即提交报告请求。

这是 $reportRequest 的 var_dump:

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"
}

我希望这有帮助!

于 2019-08-24T12:19:41.333 回答