x-fran / g-trends

Google Trends API for PHP
https://tarlabs.es
MIT License
113 stars 47 forks source link

no results for interestOverTime with empty geo location #24

Closed sansebi closed 4 years ago

sansebi commented 4 years ago

The widget data seems to be correct after retrieving the result of the GENERAL_ENDPOINT.

But the problem appears after decoding the data as an array.

My solution was to access the widget data as an object. Tested it succesful for an empty geo location and the default one.


$data = $this->_getData(self::GENERAL_ENDPOINT, 'GET', $payload);
        if ($data) {

            $widgetsArray = Json\Json::decode(trim(substr($data, 4)), Json\Json::TYPE_OBJECT)->widgets;

            foreach ($widgetsArray as $widget) {

                if ($widget->id == 'TIMESERIES') {

                    $interestOverTimePayload['hl'] = $this->options['hl'];
                    $interestOverTimePayload['tz'] = $this->options['tz'];
                    $interestOverTimePayload['req'] = Json\Json::encode($widget->request);
                    $interestOverTimePayload['token'] = $widget->token;

                    $data = $this->_getData(self::INTEREST_OVER_TIME_ENDPOINT, 'GET', $interestOverTimePayload);
                    if ($data) {

                        return Json\Json::decode(trim(substr($data, 5)), Json\Json::TYPE_ARRAY)['default']['timelineData'];
                    } else {

                        return false;
                    }
                }
            }
        }
x-fran commented 4 years ago

Thank you for your suggestion @sansebi. However I don't quite understand why is this and no time to investigate. Issue fixed on the last commit dcdf95d1a5d1729faf1ef27e5772c219b75e5f97