Kenny Grage Kenny Grage - 5 months ago 24
PHP Question

Bing Ads api only returning data for the first keyword in my request

I am looking to get cpc and other data for a large dataset of keywords. However, when I send the request to the api, I am only receiving data back for the first keyword in the

Keywords
array.You will note from the second image below that the
EstimatedBids
data object for the words delivery, pizza, insurance, and lawyer are null. If I, say, put the keyword 'lawyer' first in the array, I will get data for lawyer and nothing for the rest.

My Code:

$keywords = array('flowers', 'delivery', 'pizza', 'insurance', 'lawyer'); // example array for this question
$proxy = ClientProxy::ConstructWithAccountAndCustomerId($wsdl, null, null, $DeveloperToken, $AccountId, $CustomerId, $AccessToken); //no $UserName & $Password

$matchTypes = array (MatchType::Exact);
$keywordAndMatchTypes = array();
foreach ($keywords as $keyword) {
$keywordAndMatchType = new KeywordAndMatchType();
$keywordAndMatchType->KeywordText = $keyword;
$keywordAndMatchType->MatchTypes = $matchTypes;
array_push($keywordAndMatchTypes, $keywordAndMatchType);
}

$request_cpc = new GetEstimatedBidByKeywordsRequest();
$request_cpc->Keywords = $keywordAndMatchTypes;

$response_cpc = $proxy->GetService()->GetEstimatedBidByKeywords($request_cpc)->KeywordEstimatedBids;


My Request:

Bing Ads API Request

My Response:

Bing Ads API Response

What needs to change so that I can get that data for all of the keywords? Thanks in advance for any help.

Answer

For any future googlers, I solved my problem by instantiating the $matchTypes array in the foreach loop. In other words, I changed this old code:

$matchTypes = array (MatchType::Exact);
$keywordAndMatchTypes = array();
foreach ($keywords as $keyword) {
        $keywordAndMatchType = new KeywordAndMatchType();
        $keywordAndMatchType->KeywordText = $keyword;
        $keywordAndMatchType->MatchTypes = $matchTypes;
        array_push($keywordAndMatchTypes, $keywordAndMatchType);
}

Into this new code:

$keywordAndMatchTypes = array();
foreach ($keywords as $keyword) {
        $keywordAndMatchType = new KeywordAndMatchType();
        $matchTypes = array (MatchType::Exact);
        $keywordAndMatchType->KeywordText = $keyword;
        $keywordAndMatchType->MatchTypes = $matchTypes;
        array_push($keywordAndMatchTypes, $keywordAndMatchType);
}

For some reason, the $matchTypes array has to be instantiated each time.