x-fran / g-trends

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

Death of app by JSON's "Decoding failed: Syntax error" by exception #36

Closed DrLightman closed 3 years ago

DrLightman commented 3 years ago

For example in ::getDailySearchTrends

/**
 * @param int $ns
 * @return mixed
 */
public function getDailySearchTrends($ns=15)
{
    $params =[
        'hl'    => $this->options['hl'],
        'tz'    => $this->options['tz'],
        'geo'   => $this->options['geo'],
        'ns'    => $ns,
    ];

    try {

        $dataJson = $this->_getData(self::DAILY_SEARCH_TRENDS_ENDPOINT, 'GET', $params);
        return Json\Json::decode(trim(substr($dataJson, 5)), Json\Json::TYPE_ARRAY);
    } catch (\Exception $e) {

        die($e->getMessage());
    }
}

Isn't this too much? I would be fine to handle the false and provide the user with some sort of UI message saying there is something wrong with the server or other.

ryankaka92 commented 3 years ago

Hi Can you take a look at my problem? https://github.com/x-fran/g-trends/issues/37

x-fran commented 3 years ago

The function is returning an array or false if no results. If an exception is raised, then there is a bug an needs to be fixed. Just tested getDailySearchTrends and works as expected.