Search code examples
phpapireportgoogle-ads-apiclient-library

Reporting w/ Bing Ads API & PHP Client Library "Bad Request' Error


I'm trying to pull a keyword report from my Bing Ads account using the PHP client library. I'm getting a 'bad request' error and the report doesn't download.

I'm trying to use version 9 of the Bing Ads API.

The following is the error (using print_r($e) in the middle of the script).

SoapFault Object
(
    [message:protected] => Bad Request
    [string:Exception:private] => 
    [code:protected] => 0
    [file:protected] => /home/ubuntu/workspace/BING/PHP/Bing Ads API in PHP/v9/ReportRequests.php
    [line:protected] => 303
    [trace:Exception:private] => Array
        (
            [0] => Array
                (
                    [function] => __doRequest
                    [class] => SoapClient
                    [type] => ->
                    [args] => Array
                        (
                        [0] => 
#myaccountID#[email protected] Keyword Performance ReportfalseDailyTimePeriodAccountIdCampaignIdKeywordKeywordIdDeviceTypeBidMatchTypeClicksImpressionsCtrAverageCpcSpendQualityScore10#myaccountID#ClicksAscendingToday
                        [1] => https://reporting.api.bingads.microsoft.com/Api/Advertiser/Reporting/V9/ReportingService.svc
                        [2] => SubmitGenerateReport
                        [3] => 1
                        [4] => 0
                    )
            )
        [1] => Array
            (
                [file] => /home/ubuntu/workspace/BING/PHP/Bing Ads API in PHP/v9/ReportRequests.php
                [line] => 303
                [function] => __call
                [class] => SoapClient
                [type] => ->
                [args] => Array
                    (
                        [0] => SubmitGenerateReport
                        [1] => Array
                            (
                                [0] => BingAds\Reporting\SubmitGenerateReportRequest Object
                                    (
                                        [ReportRequest] => SoapVar Object
                                            (
                                                [enc_type] => 301
                                                [enc_value] => BingAds\Reporting\KeywordPerformanceReportRequest Object
                                                    (
                                                        [Aggregation] => Daily
                                                        [Columns] => Array
                                                            (
                                                                [0] => TimePeriod
                                                                [1] => AccountId
                                                                [2] => CampaignId
                                                                [3] => Keyword
                                                                [4] => KeywordId
                                                                [5] => DeviceType
                                                                [6] => BidMatchType
                                                                [7] => Clicks
                                                                [8] => Impressions
                                                                [9] => Ctr
                                                                [10] => AverageCpc
                                                                [11] => Spend
                                                                [12] => QualityScore
                                                            )

                                                        [Filter] => 
                                                        [MaxRows] => 10
                                                        [Scope] => BingAds\Reporting\AccountThroughAdGroupReportScope Object
                                                            (             [AccountIds] => Array
                                                                    (   [0] => #myaccountID#
                                                                    )[AdGroups] => 
                                                                [Campaigns] => 
                                                            )

                                                        [Sort] => Array
                                                            (
                                                                [0] => BingAds\Reporting\KeywordPerformanceReportSort Object
                                                                    (
                                                                        [SortColumn] => Clicks
                                                                        [SortOrder] => Ascending
                                                                    )

                                                            )

                                                        [Time] => BingAds\Reporting\ReportTime Object
                                                            (
                                                                [CustomDateRangeEnd] => 
                                                                [CustomDateRangeStart] => 
                                                                [PredefinedTime] => Today
                                                            )

                                                        [Format] => Csv
                                                        [Language] => 
                                                        [ReportName] => My Keyword Performance Report
                                                        [ReturnOnlyCompleteData] => 
                                                    )

                                                [enc_stype] => KeywordPerformanceReportRequest
                                                [enc_ns] => https://bingads.microsoft.com/Reporting/v9
                                            )

                                    )

                            )

                    )

            )

        [2] => Array
            (
                [file] => /home/ubuntu/workspace/BING/PHP/Bing Ads API in PHP/v9/ReportRequests.php
                [line] => 303
                [function] => SubmitGenerateReport
                [class] => SoapClient
                [type] => ->
                [args] => Array
                    (
                        [0] => BingAds\Reporting\SubmitGenerateReportRequest Object
                            (
                                [ReportRequest] => SoapVar Object
                                    (
                                        [enc_type] => 301
                                        [enc_value] => BingAds\Reporting\KeywordPerformanceReportRequest Object
                                            (
                                                [Aggregation] => Daily
                                                [Columns] => Array
                                                    (
                                                        [0] => TimePeriod
                                                        [1] => AccountId
                                                        [2] => CampaignId
                                                        [3] => Keyword
                                                        [4] => KeywordId
                                                        [5] => DeviceType
                                                        [6] => BidMatchType
                                                        [7] => Clicks
                                                        [8] => Impressions
                                                        [9] => Ctr
                                                        [10] => AverageCpc
                                                        [11] => Spend
                                                        [12] => QualityScore
                                                    )

                                                [Filter] => 
                                                [MaxRows] => 10
                                                [Scope] => BingAds\Reporting\AccountThroughAdGroupReportScope Object
                                                    (
                                                        [AccountIds] => Array
                                                            (
                                                                [0] => #myaccountID#
                                                            )

                                                        [AdGroups] => 
                                                        [Campaigns] => 
                                                    )

                                                [Sort] => Array
                                                    (
                                                        [0] => BingAds\Reporting\KeywordPerformanceReportSort Object
                                                            (
                                                                [SortColumn] => Clicks
                                                                [SortOrder] => Ascending
                                                            )

                                                    )

                                                [Time] => BingAds\Reporting\ReportTime Object
                                                    (
                                                        [CustomDateRangeEnd] => 
                                                        [CustomDateRangeStart] => 
                                                        [PredefinedTime] => Today
                                                    )

                                                [Format] => Csv
                                                [Language] => 
                                                [ReportName] => My Keyword Performance Report
                                                [ReturnOnlyCompleteData] => 
                                            )

                                        [enc_stype] => KeywordPerformanceReportRequest
                                        [enc_ns] => https://bingads.microsoft.com/Reporting/v9
                                    )

                            )

                    )

            )

        [3] => Array
            (
                [file] => /home/ubuntu/workspace/BING/PHP/Bing Ads API in PHP/v9/ReportRequests.php
                [line] => 103
                [function] => SubmitGenerateReport
                [args] => Array
                    (
                        [0] => BingAds\Proxy\ClientProxy Object
                            (
                                [authenticationToken:BingAds\Proxy\ClientProxy:private] => 
                                [username:BingAds\Proxy\ClientProxy:private] => [email protected]
                                [password:BingAds\Proxy\ClientProxy:private] => mypassword
                                [developerToken:BingAds\Proxy\ClientProxy:private] => 041F63U87G121059
                                [wsdlUrl:BingAds\Proxy\ClientProxy:private] => https://api.bingads.microsoft.com/Api/Advertiser/Reporting/V9/ReportingService.svc?singleWsdl
                                [accountId:BingAds\Proxy\ClientProxy:private] => #myaccountID#
                                [customerId:BingAds\Proxy\ClientProxy:private] => 
                                [service:BingAds\Proxy\ClientProxy:private] => SoapClient Object
                                    (
                                        [trace] => 1
                                        [_features] => 1
                                        [_soap_version] => 1
                                        [sdl] => Resource id #6
                                        [typemap] => Resource id #7
                                        [__default_headers] => Array
                                            (
                                                [0] => SoapHeader Object
                                                    (
                                                        [namespace] => https://bingads.microsoft.com/Reporting/v9
                                                        [name] => #myaccountID#
                                                        [data] => #myaccountID#
                                                        [mustUnderstand] => 
                                                    )

                                                [1] => SoapHeader Object
                                                    (
                                                        [namespace] => https://bingads.microsoft.com/Reporting/v9
                                                        [name] => #mycustID#
                                                        [data] => 
                                                        [mustUnderstand] => 
                                                    )

                                                [2] => SoapHeader Object
                                                    (
                                                        [namespace] => https://bingads.microsoft.com/Reporting/v9
                                                        [name] => 041F63U87G121059
                                                        [data] => 041F63U87G121059
                                                        [mustUnderstand] => 
                                                    )

                                                [3] => SoapHeader Object
                                                    (
                                                        [namespace] => https://bingads.microsoft.com/Reporting/v9
                                                        [name] => [email protected]
                                                        [data] => [email protected]
                                                        [mustUnderstand] => 
                                                    )

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

                                                [5] => SoapHeader Object
                                                    (
                                                        [namespace] => https://bingads.microsoft.com/Reporting/v9
                                                        [name] => EwBoAnhlBAAUxT83/QvqiAZEx5SuwyhZqHzk21oAAZOYUlCZ8fHq%2bAPC1k3PVSu8c8EXUqNZW8qb%2b52zO2/YZKkXopE7MvVZO7lWS69iMgHWTJO21LhVFN93oPs2KLl/dp7ZZBXqnf/LwqkIKys5hMpr%2b5XvExI/nywKmFbsHDcT9dyXnUCpQf4q14GGTdtQGPmTh9xsDo0sMIdoQtNovU8pk1/kWriQDLn%2bGaBnMr0RIH6h1tvjbCql2MyWhr207gqvTap8tuWnkDW75rUR6QrSCPifyC54dygFTzXKq0ggN/kMmdImOSn/8BxZ6KoUUTLVVBbxayEwrrHuh1QeuBfnh4PRMIwX7BwHUdVTyjB%2bAzJXFzOKlUD39Oyszn4DZgAACA3FE6D1xZKXOAH6f0KgK8gFEhnH7Wi3Q1LqjnocaIoY/royWIFGyGlatJQiKJK4N0XYquNcMfFwFeRX6pto6GumUm%2bhOCT/yV3%2bFdSFxvX4ZjRFP/lldQvPG4dULM%2b1uvZiegv5upygYR1r2l03zMlJ2MU28jE4F%2b34RxbNZVXxtpckAHed5YAUZgcJVjg7jK5nkShwjjqnz4iuqofn8T/KBc0hlsHBzBYVoTwNqUX2OOel3XNGQQS8a3rT1I5R2%2bQ%2bxmIxWbyAtI8hHtLIiF6tHp6fj/JkSm4Euo43ckxFpdWL%2bKAfWGZPnVkCqVXV4pZl3HbkPfaIqjCnhVHj9xn1yHhbQbStCkXmLK%2bpN9Xa0inIJgqzRFobRAtzZa1umvqe7yIbBgCobDYNsyVYCxQkkhJ9jXYbQfNG041trWNLaMBRAQ%3d%3d
                                                        [data] => 
                                                        [mustUnderstand] => 
                                                    )
                                            )
                                        [__last_request] => 
#myaccountID#[email protected] Keyword Performance ReportfalseDailyTimePeriodAccountIdCampaignIdKeywordKeywordIdDeviceTypeBidMatchTypeClicksImpressionsCtrAverageCpcSpendQualityScore10#myaccountID#ClicksAscendingToday

                                            [httpsocket] => Resource id #8
                                            [_use_proxy] => 0
                                            [httpurl] => Resource id #9
                                            [__last_request_headers] => POST /Api/Advertiser/Reporting/V9/ReportingService.svc HTTP/1.1
Host: reporting.api.bingads.microsoft.com
Connection: Keep-Alive
User-Agent: PHP-SOAP/5.5.9-1ubuntu4.13
Content-Type: text/xml; charset=utf-8
SOAPAction: "SubmitGenerateReport"
Content-Length: 3474


                                            [__last_response_headers] => HTTP/1.1 400 Bad Request
Server: Microsoft-IIS/8.0
X-Powered-By: ASP.NET
Date: Wed, 17 Feb 2016 23:18:17 GMT
Content-Length: 0

                                            [__soap_fault] => SoapFault Object
 *RECURSION*
                                        )

                                    [namespace:BingAds\Proxy\ClientProxy:private] => https://bingads.microsoft.com/Reporting/v9
                                )

                            [1] => SoapVar Object
                                (
                                    [enc_type] => 301
                                    [enc_value] => BingAds\Reporting\KeywordPerformanceReportRequest Object
                                        (
                                            [Aggregation] => Daily
                                            [Columns] => Array
                                                (
                                                    [0] => TimePeriod
                                                    [1] => AccountId
                                                    [2] => CampaignId
                                                    [3] => Keyword
                                                    [4] => KeywordId
                                                    [5] => DeviceType
                                                    [6] => BidMatchType
                                                    [7] => Clicks
                                                    [8] => Impressions
                                                    [9] => Ctr
                                                    [10] => AverageCpc
                                                    [11] => Spend
                                                    [12] => QualityScore
                                                )

                                            [Filter] => 
                                            [MaxRows] => 10
                                            [Scope] => BingAds\Reporting\AccountThroughAdGroupReportScope Object
                                                (
                                                    [AccountIds] => Array
                                                        (
                                                            [0] => #myaccountID#
                                                        )

                                                    [AdGroups] => 
                                                    [Campaigns] => 
                                                )

                                            [Sort] => Array
                                                (
                                                    [0] => BingAds\Reporting\KeywordPerformanceReportSort Object
                                                        (
                                                            [SortColumn] => Clicks
                                                            [SortOrder] => Ascending
                                                        )

                                                )

                                            [Time] => BingAds\Reporting\ReportTime Object
                                                (
                                                    [CustomDateRangeEnd] => 
                                                    [CustomDateRangeStart] => 
                                                    [PredefinedTime] => Today
                                                )

                                            [Format] => Csv
                                            [Language] => 
                                            [ReportName] => My Keyword Performance Report
                                            [ReturnOnlyCompleteData] => 
                                        )

                                    [enc_stype] => KeywordPerformanceReportRequest
                                    [enc_ns] => https://bingads.microsoft.com/Reporting/v9
                                )

                        )

                )

            [4] => Array
                (
                    [file] => /home/ubuntu/workspace/index.php
                    [line] => 4
                    [args] => Array
                        (
                            [0] => /home/ubuntu/workspace/BING/PHP/Bing Ads API in PHP/v9/ReportRequests.php
                        )

                    [function] => require_once
                )

        )

    [previous:Exception:private] => 
    [faultstring] => Bad Request
    [faultcode] => HTTP
    [xdebug_message] => ( ! ) SoapFault: Bad Request in /home/ubuntu/workspace/BING/PHP/Bing Ads API in PHP/v9/ReportRequests.php on line 303
Call Stack
#TimeMemoryFunctionLocation
10.0003234016{main}(  ).../index.php:0
20.0019239592require_once( '/home/ubuntu/workspace/BING/PHP/Bing Ads API in PHP/v9/ReportRequests.php' ).../index.php:4
30.27911470864SubmitGenerateReport(  ).../ReportRequests.php:103
40.27911471200SubmitGenerateReport
(  ).../ReportRequests.php:303
50.27911471600__call
(  ).../ReportRequests.php:303

)

Solution

  • It's not quite clear, are you using the Reports in PHP example provided on MSDN, or writing your own from scratch? It appears you might be trying to set the UserName header element to an email address which is not supported. To authenticate a Microsoft account user, you must use OAuth e.g. please see Server Side OAuth Authentication in PHP. The "Bad Request" error is likely the result of something else though, so please share the SOAP request and response if available, and I'm happy to take a look.