ggalan
06-14-2011, 01:08 PM
i have this code that works for a single entry
$user = new AdWordsUser();
$user->LogDefaults();
$targetingIdeaService = $user->GetTargetingIdeaService('v201008');
$keyword = new Keyword();
$keyword->text = $kWord;
$keyword->matchType = 'EXACT';//BROAD , "Phrase"
$selector = new TargetingIdeaSelector();
$selector->requestType = 'IDEAS';
$selector->ideaType = 'KEYWORD';
$selector->requestedAttributeTypes = array('KEYWORD', 'AVERAGE_TARGETED_MONTHLY_SEARCHES');
$paging = new Paging();
$paging->startIndex = 0;
$paging->numberResults = 10;
$selector->paging = $paging;
$relatedToKeywordSearchParameter = new RelatedToKeywordSearchParameter();
$relatedToKeywordSearchParameter->keywords = array($keyword);
$keywordMatchTypeSearchParameter = new KeywordMatchTypeSearchParameter();
$keywordMatchTypeSearchParameter->keywordMatchTypes = array('EXACT');//BROAD
$selector->searchParameters =
array($relatedToKeywordSearchParameter, $keywordMatchTypeSearchParameter);
$page = $targetingIdeaService->get($selector);
if (isset($page->entries)) {
echo '<div id="output"><ul>';
foreach ($page-> entries as $targetingIdea) {
echo '<li><a href="#">';
$data = MapUtils::GetMap($targetingIdea->data);//
$keyword = $data['KEYWORD']->value;
$averageMonthlySearches = isset($data['AVERAGE_TARGETED_MONTHLY_SEARCHES']->value)
? $data['AVERAGE_TARGETED_MONTHLY_SEARCHES']->value : 0;
echo $keyword->text . '</a>';
echo ' match types:' . $keyword->matchType;
echo ' / ' . $averageMonthlySearches . ' monthly searches';
echo '</li>';
}
echo '</ul></div>';
however i would like to use a different class and pass in associative arrays like this (attempt but code does not work)
error_reporting(E_STRICT | E_ALL);
$path = dirname(__FILE__) . '/../../src';
set_include_path(get_include_path() . PATH_SEPARATOR . $path);
require_once 'Google/Api/Ads/AdWords/Lib/AdWordsUser.php';
require_once 'Google/Api/Ads/Common/Util/MapUtils.php';
if(isset($_POST['SearchIdeasFormField']) && $kWord != '') {
try {
$user = new AdWordsUser();
$user->LogDefaults();
$targetingIdeaService = $user->GetTargetingIdeaService('v201008');
$keyword = new Keyword();
$keyword->text = $kWord;
$keyword->matchType = 'EXACT';//BROAD , "Phrase"
$selector = new TargetingIdeaSelector();
$selector->requestType = 'IDEAS';
$selector->ideaType = 'KEYWORD';
$selector->requestedAttributeTypes = array('KEYWORD'=>'AVERAGE_TARGETED_MONTHLY_SEARCHES', 'KEYWORD'=>'COMPETITION','KEYWORD'=>'SEARCH_SHARE');
$paging = new Paging();
$paging->startIndex = 0;
$paging->numberResults = 10;
$selector->paging = $paging;
$relatedToKeywordSearchParameter = new RelatedToKeywordSearchParameter();
$relatedToKeywordSearchParameter->keywords = array($keyword);
$keywordMatchTypeSearchParameter = new KeywordMatchTypeSearchParameter();
$keywordMatchTypeSearchParameter->keywordMatchTypes = array('EXACT');//BROAD
$selector->searchParameters =
array($relatedToKeywordSearchParameter, $keywordMatchTypeSearchParameter);
$page = $targetingIdeaService->get($selector);
if (isset($page->entries)) {
// THIS PART NEEDS WORK
echo '<div id="output"><ul>';
foreach ($page-> entries as $targetingIdea=>$val) {
echo '<li><a href="#">';
$data = MapUtils::GetMapEntries($val->data);//NEW CLASS
$keyword = $data['KEYWORD']->value;
$averageMonthlySearches = isset($data['AVERAGE_TARGETED_MONTHLY_SEARCHES']->value)
? $data['AVERAGE_TARGETED_MONTHLY_SEARCHES']->value : 0;
$competition = isset($data2['COMPETITION']->value)
? $data2['COMPETITION']->value : 0;
$searchShare = isset($data3['SEARCH_SHARE']->value)
? $data3['SEARCH_SHARE']->value : 0;
echo $keyword->text . '</a>';
echo ' match types:' . $keyword->matchType;
echo ' / ' . $averageMonthlySearches . ' monthly searches';
echo ' / ' . $competition . ' competition';
echo ' / ' . $searchShare . ' search share';
echo '</li>';
}
echo '</ul></div>';
} else {
print "No related keywords were found.\n";
}
} catch (Exception $e) {
print $e->getMessage();
}
}
here are the 2 class
/**
* Gets a map (associative array) from an array of map entries. A map entry
* is any object that has a key and value field.
* @param array $mapEntries an array of map entries
* @return array a map built from the keys and values of the map entries
*/
public static function GetMap(array $mapEntries) {
$result = array();
foreach ($mapEntries as $mapEntry) {
$result[$mapEntry->key] = $mapEntry->value;
}
return $result;
}
/**
* Gets an array of map entries from a map (associative array). A map entry
* is any object that has a key and value field. An optional map entry class
* name can be specified for constructing the entries, otherise the stdClass
* is used.
* @param array $map a map from key to value
* @param string $mapEntryClassName an optional class name to use when
* constructing the map entries
* @return array an array of map entries built from the key-value pairs in
* the map
*/
public static function GetMapEntries(array $map, $mapEntryClassName = NULL) {
$result = array();
foreach($map as $key => $value) {
if (isset($mapEntryClassName)) {
$entry = new $mapEntryClassName();
} else {
$entry = (object) array();
}
$entry->key = $key;
$entry->value = $value;
$result[] = $entry;
}
return $result;
}
$user = new AdWordsUser();
$user->LogDefaults();
$targetingIdeaService = $user->GetTargetingIdeaService('v201008');
$keyword = new Keyword();
$keyword->text = $kWord;
$keyword->matchType = 'EXACT';//BROAD , "Phrase"
$selector = new TargetingIdeaSelector();
$selector->requestType = 'IDEAS';
$selector->ideaType = 'KEYWORD';
$selector->requestedAttributeTypes = array('KEYWORD', 'AVERAGE_TARGETED_MONTHLY_SEARCHES');
$paging = new Paging();
$paging->startIndex = 0;
$paging->numberResults = 10;
$selector->paging = $paging;
$relatedToKeywordSearchParameter = new RelatedToKeywordSearchParameter();
$relatedToKeywordSearchParameter->keywords = array($keyword);
$keywordMatchTypeSearchParameter = new KeywordMatchTypeSearchParameter();
$keywordMatchTypeSearchParameter->keywordMatchTypes = array('EXACT');//BROAD
$selector->searchParameters =
array($relatedToKeywordSearchParameter, $keywordMatchTypeSearchParameter);
$page = $targetingIdeaService->get($selector);
if (isset($page->entries)) {
echo '<div id="output"><ul>';
foreach ($page-> entries as $targetingIdea) {
echo '<li><a href="#">';
$data = MapUtils::GetMap($targetingIdea->data);//
$keyword = $data['KEYWORD']->value;
$averageMonthlySearches = isset($data['AVERAGE_TARGETED_MONTHLY_SEARCHES']->value)
? $data['AVERAGE_TARGETED_MONTHLY_SEARCHES']->value : 0;
echo $keyword->text . '</a>';
echo ' match types:' . $keyword->matchType;
echo ' / ' . $averageMonthlySearches . ' monthly searches';
echo '</li>';
}
echo '</ul></div>';
however i would like to use a different class and pass in associative arrays like this (attempt but code does not work)
error_reporting(E_STRICT | E_ALL);
$path = dirname(__FILE__) . '/../../src';
set_include_path(get_include_path() . PATH_SEPARATOR . $path);
require_once 'Google/Api/Ads/AdWords/Lib/AdWordsUser.php';
require_once 'Google/Api/Ads/Common/Util/MapUtils.php';
if(isset($_POST['SearchIdeasFormField']) && $kWord != '') {
try {
$user = new AdWordsUser();
$user->LogDefaults();
$targetingIdeaService = $user->GetTargetingIdeaService('v201008');
$keyword = new Keyword();
$keyword->text = $kWord;
$keyword->matchType = 'EXACT';//BROAD , "Phrase"
$selector = new TargetingIdeaSelector();
$selector->requestType = 'IDEAS';
$selector->ideaType = 'KEYWORD';
$selector->requestedAttributeTypes = array('KEYWORD'=>'AVERAGE_TARGETED_MONTHLY_SEARCHES', 'KEYWORD'=>'COMPETITION','KEYWORD'=>'SEARCH_SHARE');
$paging = new Paging();
$paging->startIndex = 0;
$paging->numberResults = 10;
$selector->paging = $paging;
$relatedToKeywordSearchParameter = new RelatedToKeywordSearchParameter();
$relatedToKeywordSearchParameter->keywords = array($keyword);
$keywordMatchTypeSearchParameter = new KeywordMatchTypeSearchParameter();
$keywordMatchTypeSearchParameter->keywordMatchTypes = array('EXACT');//BROAD
$selector->searchParameters =
array($relatedToKeywordSearchParameter, $keywordMatchTypeSearchParameter);
$page = $targetingIdeaService->get($selector);
if (isset($page->entries)) {
// THIS PART NEEDS WORK
echo '<div id="output"><ul>';
foreach ($page-> entries as $targetingIdea=>$val) {
echo '<li><a href="#">';
$data = MapUtils::GetMapEntries($val->data);//NEW CLASS
$keyword = $data['KEYWORD']->value;
$averageMonthlySearches = isset($data['AVERAGE_TARGETED_MONTHLY_SEARCHES']->value)
? $data['AVERAGE_TARGETED_MONTHLY_SEARCHES']->value : 0;
$competition = isset($data2['COMPETITION']->value)
? $data2['COMPETITION']->value : 0;
$searchShare = isset($data3['SEARCH_SHARE']->value)
? $data3['SEARCH_SHARE']->value : 0;
echo $keyword->text . '</a>';
echo ' match types:' . $keyword->matchType;
echo ' / ' . $averageMonthlySearches . ' monthly searches';
echo ' / ' . $competition . ' competition';
echo ' / ' . $searchShare . ' search share';
echo '</li>';
}
echo '</ul></div>';
} else {
print "No related keywords were found.\n";
}
} catch (Exception $e) {
print $e->getMessage();
}
}
here are the 2 class
/**
* Gets a map (associative array) from an array of map entries. A map entry
* is any object that has a key and value field.
* @param array $mapEntries an array of map entries
* @return array a map built from the keys and values of the map entries
*/
public static function GetMap(array $mapEntries) {
$result = array();
foreach ($mapEntries as $mapEntry) {
$result[$mapEntry->key] = $mapEntry->value;
}
return $result;
}
/**
* Gets an array of map entries from a map (associative array). A map entry
* is any object that has a key and value field. An optional map entry class
* name can be specified for constructing the entries, otherise the stdClass
* is used.
* @param array $map a map from key to value
* @param string $mapEntryClassName an optional class name to use when
* constructing the map entries
* @return array an array of map entries built from the key-value pairs in
* the map
*/
public static function GetMapEntries(array $map, $mapEntryClassName = NULL) {
$result = array();
foreach($map as $key => $value) {
if (isset($mapEntryClassName)) {
$entry = new $mapEntryClassName();
} else {
$entry = (object) array();
}
$entry->key = $key;
$entry->value = $value;
$result[] = $entry;
}
return $result;
}