Skip to content

Commit

Permalink
add google trends latest stories
Browse files Browse the repository at this point in the history
  • Loading branch information
x-fran committed Dec 4, 2017
1 parent a6dbf3c commit 5f19dac
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 24 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
vendor
composer.phar
phpunit.xml
2 changes: 2 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
print_r($gt->interestBySubregion(['Dublin']));
print_r("\n\n <h1>GTrends suggestionsAutocomplete</h1>\n ");
print_r($gt->suggestionsAutocomplete('Dublin'));
print_r("\n\n <h1>GTrends latestStories</h1>\n ");
print_r($gt->latestStories());
print_r("\n\n");

?>
Expand Down
85 changes: 61 additions & 24 deletions src/Google/GTrends.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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 = [
Expand All @@ -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 = [
Expand All @@ -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'";
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -384,4 +421,4 @@ private function _getData($uri, $method, array $params=[])

return false;
}
}
}
10 changes: 10 additions & 0 deletions test/GoogleTest/GTrendsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 5f19dac

Please sign in to comment.