Object of class MonthlySearchVolume could not be converted to string

71 views
Skip to first unread message

Eric Jorge Seguí Parejo

unread,
Nov 24, 2015, 8:44:14 AM11/24/15
to AdWords API Forum
Hi! 
I try with the example api GetKeywordsIdeas.php from folder 'v201509' get the MonthlySearchVolume, but when i want to show, appears that error: 

Catchable fatal error: Object of class MonthlySearchVolume could not be converted to string in C:\xampp\htdocs\adwords\examples\AdWords\v201509\Optimization\Get KeywordIdeas.php on line 98

The code its:
<?php
/**
 * This example gets keyword ideas related to a seed keyword.
 *
 * Restriction: adwords-only
 *
 * Copyright 2014, Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * @package    GoogleApiAdsAdWords
 * @subpackage v201509
 * @category   WebServices
 * @copyright  2014, Google Inc. All Rights Reserved.
 * @license    http://www.apache.org/licenses/LICENSE-2.0 Apache License,
 *             Version 2.0
 */
// Include the initialization file
require_once dirname(dirname(__FILE__)) . '/init.php';
require_once UTIL_PATH . '/MapUtils.php';
/**
 * Runs the example.
 * @param AdWordsUser $user the user to run the example with
 */
function GetKeywordIdeasExample(AdWordsUser $user) {
  // Get the service, which loads the required classes.
  $targetingIdeaService =
      $user->GetService('TargetingIdeaService', ADWORDS_VERSION);
  // Create seed keyword.
  $keyword = 'benidorm holidays';
  // Create selector.
  $selector = new TargetingIdeaSelector();
  //$selector->requestType = 'IDEAS';
  $selector->requestType = 'STATS';
  $selector->ideaType = 'KEYWORD';
  $selector->requestedAttributeTypes = array('KEYWORD_TEXT', 'SEARCH_VOLUME',
      'COMPETITION','AVERAGE_CPC','TARGETED_MONTHLY_SEARCHES');
  // Create language search parameter (optional).
  // The ID can be found in the documentation:
  //   https://developers.google.com/adwords/api/docs/appendix/languagecodes
  // Note: As of v201302, only a single language parameter is allowed.
  $languageParameter = new LanguageSearchParameter();
  $english = new Language();
  $english->id = 1000;
  $languageParameter->languages = array($english);
  // Create related to query search parameter.
  $relatedToQuerySearchParameter = new RelatedToQuerySearchParameter();
  $relatedToQuerySearchParameter->queries = array($keyword);
  $selector->searchParameters[] = $relatedToQuerySearchParameter;
  $selector->searchParameters[] = $languageParameter;
  // Set selector paging (required by this service).
  $selector->paging = new Paging(0, AdWordsConstants::RECOMMENDED_PAGE_SIZE);
$f = fopen("reporte.csv","w");
$sep = ","; //separador
  do {
    // Make the get request.
    $page = $targetingIdeaService->get($selector);
    // Display results.
    if (isset($page->entries)) {
      foreach ($page->entries as $targetingIdea) {
        $data = MapUtils::GetMap($targetingIdea->data);
        $keyword = $data['KEYWORD_TEXT']->value;
        $search_volume = isset($data['SEARCH_VOLUME']->value)
            ? $data['SEARCH_VOLUME']->value : 0;
        $competition = isset($data['COMPETITION']->value)
            ? $data['COMPETITION']->value : 0;
$averagecpc = isset($data['AVERAGE_CPC']->value)
            ? $data['AVERAGE_CPC']->value : 0;
$monthlysearches = isset($data['TARGETED_MONTHLY_SEARCHES']->value)
            ? $data['TARGETED_MONTHLY_SEARCHES']->value : 0;
/*$competition=(string)$competition;
$averagecpc=(string)$averagecpc;
$monthlysearches=(string)$monthlysearches;
$linea = $keyword./*$sep.$categoryIds.$sep.$search_volume.$sep.$competition./*$sep.$averagecpc.$sep./*$monthlysearches."\n";
fwrite($f,$linea);*/
//I HAVE THE PROBLEM THERE
        printf("Keyword idea with text '%s', category IDs (%s) and average "
            . "monthly search volume '%s' was found.\n",
            $keyword, $monthlysearches[1], $search_volume);
      }
    } else {
      print "No keywords ideas were found.\n";
    }
    // Advance the paging index.
    $selector->paging->startIndex += AdWordsConstants::RECOMMENDED_PAGE_SIZE;
  } while ($page->totalNumEntries > $selector->paging->startIndex);
  fclose($f); 
}
// Don't run the example if the file is being included.
if (__FILE__ != realpath($_SERVER['PHP_SELF'])) {
  return;
}
try {
  // Get AdWordsUser from credentials in "../auth.ini"
  // relative to the AdWordsUser.php file's directory.
  $user = new AdWordsUser();
  // Log every SOAP XML request and response.
  $user->LogAll();
  // Run the example.
  GetKeywordIdeasExample($user);
} catch (Exception $e) {
  printf("An error has occurred: %s\n", $e->getMessage());

Someone can help me? i have the same problem for 'AVERAGE_CPC' 
Thanks for the help! ^^

Umesh Dengale

unread,
Nov 24, 2015, 4:57:29 PM11/24/15
to AdWords API Forum
Hello,

The MonthlySearchVolumeAttribute.value is of type array of MonthlySearchVolume. The MonthlySearchVolume is a type and to access its properties (e.g countmonthyear etc.) you could specify like $monthlysearches[1].count.

Regards,
Umesh, AdWords API Team.

Eric Jorge Seguí Parejo

unread,
Nov 26, 2015, 6:59:55 PM11/26/15
to AdWords API Forum
Hi Umesh! Thanks for your help =) finally the same day i can use this:

$var=print_r($monthlysearches['x'],true);
echo $var; 

To show the data about last 'x' months

=)

Umesh Dengale

unread,
Nov 30, 2015, 12:22:31 PM11/30/15
to AdWords API Forum
Hi Eric,

I am glad that you found the fix. Let us know if you need help with any other issues.

Thanks,
Umesh, AdWords API Team.
Reply all
Reply to author
Forward
0 new messages