if (empty($keywords)) {
// Only page URL was specified, so use a UrlSeed.
$requestOptionalArgs['urlSeed'] =
new UrlSeed(['url' => new StringValue(['value' => $link_platform_product_page])]);
} elseif (is_null($link_platform_product_page)) {
// Only keywords were specified, so use a KeywordSeed.
$requestOptionalArgs['keywordSeed'] = new KeywordSeed([
'keywords' => array_map(function ($keyword) {
return new StringValue(['value' => $keyword]);
}, $keywords)
]);
} else {
// Both page URL and keywords were specified, so use a KeywordAndUrlSeed.
$requestOptionalArgs['keywordAndUrlSeed'] = new KeywordAndUrlSeed([
'url' => new StringValue(['value' => $link_platform_product_page]),
'keywords' => array_map(function ($keyword) {
return new StringValue(['value' => $keyword]);
}, $keywords)
]);
}
// Create a list of geo target constants based on the resource name of specified location
// IDs.
$geoTargetConstants = array_map(function ($locationId) {
return new StringValue(
['value' => ResourceNames::forGeoTargetConstant($locationId)]
);
}, $locationIds);
// Generate keyword ideas based on the specified parameters.
$response = $keywordPlanIdeaServiceClient->generateKeywordIdeas(
$customerId,
// Set the language resource using the provided language ID.
new StringValue(['value' => ResourceNames::forLanguageConstant($language)]),
// Add the resource name of each location ID to the request.
$geoTargetConstants,
// Set the network. To restrict to only Google Search, change the parameter below to
// KeywordPlanNetwork::GOOGLE_SEARCH.
KeywordPlanNetwork::GOOGLE_SEARCH_AND_PARTNERS,
$requestOptionalArgs
);
$keywords = "";
$avgmonthly = "";
$i = 0;
// Iterate over the results and print its detail.
foreach ($response->getResults() as $result) {
if ($result->getKeywordIdeaMetrics()) {
$competitor = $result->getKeywordIdeaMetrics()->getCompetition();
$avgmonthly = $result->getKeywordIdeaMetrics()->getAvgMonthlySearches()->getValue();
if ($competitor == 4 && $avgmonthly > 100) {
// echo str_word_count($words);
$keywords .= $result->getText()->getValue() . ",";
if ($i++ == $limit) break;
printf(
"Keyword idea text '%s' has %d average monthly searches and competition as %d.%s",
$result->getText()->getValue(),
is_null($result->getKeywordIdeaMetrics()) ?
0 : $result->getKeywordIdeaMetrics()->getAvgMonthlySearches()->getValue(),
is_null($result->getKeywordIdeaMetrics()) ?
0 : $result->getKeywordIdeaMetrics()->getCompetition(),
PHP_EOL
);
}
}
}
return $keywords;
how to add $competitor = $result->getKeywordIdeaMetrics()->getCompetition();
$avgmonthly = $result->getKeywordIdeaMetrics()->getAvgMonthlySearches()->getValue(); before foreach as paramether so to not generate all the keywords and to filter them but to generated only keywords i need.