diff --git a/.gitignore b/.gitignore index bebd336..d1fb3d1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.idea vendor composer.phar phpunit.xml \ No newline at end of file diff --git a/index.php b/index.php index c0a3196..c5a2fd1 100644 --- a/index.php +++ b/index.php @@ -39,6 +39,8 @@ print_r($gt->interestBySubregion(['Dublin'])); print_r("\n\n

GTrends suggestionsAutocomplete

\n "); print_r($gt->suggestionsAutocomplete('Dublin')); +print_r("\n\n

GTrends latestStories

\n "); +print_r($gt->latestStories()); print_r("\n\n"); ?> diff --git a/src/Google/GTrends.php b/src/Google/GTrends.php index a022fce..9df72e0 100644 --- a/src/Google/GTrends.php +++ b/src/Google/GTrends.php @@ -15,6 +15,7 @@ class GTrends const TOP_CHARTS_URL = 'https://trends.google.com/trends/topcharts/chart'; const SUGGESTIONS_URL = 'https://trends.google.com/trends/api/autocomplete'; const INTEREST_BY_SUBREGION_URL = 'https://trends.google.com/trends/api/widgetdata/comparedgeo'; + const LATEST_STORIES = 'https://www.google.com/trends/api/stories/latest'; protected $options = [ 'hl' => 'en-US', @@ -94,13 +95,15 @@ public function relatedQueries(array $keyWordList, $category=0, $time='now 1-H', return false; } - /** - * @param $kWord - * @param int $category - * @param string $time - * @param string $property - * @return array|bool - */ + /** + * @param $kWord + * @param int $category + * @param string $time + * @param string $property + * + * @return array|bool + * @throws \Exception + */ public function interestOverTime($kWord, $category=0, $time='now 1-H', $property='') { $comparisonItem[] = ['keyword' => $kWord, 'geo' => $this->options['geo'], 'time' => $time]; @@ -139,11 +142,13 @@ public function interestOverTime($kWord, $category=0, $time='now 1-H', $property return false; } - /** - * @param $country - * @param $date - * @return array|bool - */ + /** + * @param $country + * @param $date + * + * @return array|bool + * @throws \Exception + */ public function trendingSearches($country, $date) { $params = [ @@ -163,13 +168,15 @@ public function trendingSearches($country, $date) } } - /** - * @param $date - * @param $cid - * @param string $geo - * @param string $cat - * @return array|bool - */ + /** + * @param $date + * @param $cid + * @param string $geo + * @param string $cat + * + * @return array|bool + * @throws \Exception + */ public function topCharts($date, $cid, $geo='US', $cat='') { $chartsPayload = [ @@ -188,10 +195,12 @@ public function topCharts($date, $cid, $geo='US', $cat='') return false; } - /** - * @param $kWord - * @return array|bool - */ + /** + * @param $kWord + * + * @return array|bool + * @throws \Exception + */ public function suggestionsAutocomplete($kWord) { $uri = self::SUGGESTIONS_URL . "/'$kWord'"; @@ -275,6 +284,34 @@ public function interestBySubregion(array $keyWordList, $resolution='SUBREGION', return false; } + /** + * @param string $country + * @param string $cat + * @param string $geo + * @param int $tz + * + * @return bool|mixed + * @throws \Exception + */ + public function latestStories($country='en-US', $cat='all', $geo='IE', $tz=-60) + { + $params = [ + 'hl' => $country, + 'cat' => $cat, + 'fi' => 15, + 'fs' => 15, + 'geo' => $geo, + 'ri' => 300, + 'rs' => 15, + 'tz' => $tz, + ]; + $data = $this->_getData(self::LATEST_STORIES, 'GET', $params); + if ($data) { + return Json\Json::decode(trim(substr($data, 4)), true); + } + return false; + } + /** * @return array */ @@ -384,4 +421,4 @@ private function _getData($uri, $method, array $params=[]) return false; } -} \ No newline at end of file +} diff --git a/test/GoogleTest/GTrendsTest.php b/test/GoogleTest/GTrendsTest.php index ce9ab78..eb0c4bb 100644 --- a/test/GoogleTest/GTrendsTest.php +++ b/test/GoogleTest/GTrendsTest.php @@ -140,4 +140,14 @@ public function testIfInterestBySubregionReturnsArray() $this->assertEquals(is_array($trendingSearches), true); } + + public function testIfLatestStoriesReturnsArray() + { + /* @var $gt GTrends */ + $gt = $this->gt; + + $latestStories = $gt->latestStories(); + + $this->assertEquals(is_array($latestStories), true); + } } \ No newline at end of file