Skip to content

Commit

Permalink
add cookies persistence and some minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
x-fran committed Jun 16, 2018
1 parent 5f19dac commit ab6bcfa
Showing 1 changed file with 85 additions and 82 deletions.
167 changes: 85 additions & 82 deletions src/Google/GTrends.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ class GTrends
'geo' => 'US',
];

/**
* GTrends constructor.
* @param array $options
* @throws \Exception
*/
public function __construct(array $options=[])
{
if ($options) {
Expand Down Expand Up @@ -61,7 +66,7 @@ public function relatedQueries(array $keyWordList, $category=0, $time='now 1-H',
$data = $this->_getData(self::GENERAL_URL, 'GET', $payload);
if ($data) {

$widgetsArray = Json\Json::decode(trim(substr($data, 5)), true)['widgets'];
$widgetsArray = Json\Json::decode(trim(substr($data, 5)), Json\Json::TYPE_ARRAY)['widgets'];
$results = [];
foreach ($widgetsArray as $widget) {

Expand All @@ -75,7 +80,7 @@ public function relatedQueries(array $keyWordList, $category=0, $time='now 1-H',
$data = $this->_getData(self::RELATED_QUERIES_URL, 'GET', $relatedPayload);
if ($data) {

$queriesArray = Json\Json::decode(trim(substr($data, 5)), true);
$queriesArray = Json\Json::decode(trim(substr($data, 5)), Json\Json::TYPE_ARRAY);
$results[$kWord] = $queriesArray;

if (count($keyWordList)>1) {
Expand All @@ -95,15 +100,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
* @throws \Exception
*/
/**
* @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 All @@ -116,7 +121,7 @@ public function interestOverTime($kWord, $category=0, $time='now 1-H', $property
$data = $this->_getData(self::GENERAL_URL, 'GET', $payload);
if ($data) {

$widgetsArray = Json\Json::decode(trim(substr($data, 4)), true)['widgets'];
$widgetsArray = Json\Json::decode(trim(substr($data, 4)), Json\Json::TYPE_ARRAY)['widgets'];

foreach ($widgetsArray as $widget) {

Expand All @@ -130,7 +135,7 @@ public function interestOverTime($kWord, $category=0, $time='now 1-H', $property
$data = $this->_getData(self::INTEREST_OVER_TIME_URL, 'GET', $interestOverTimePayload);
if ($data) {

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

return false;
Expand All @@ -142,13 +147,13 @@ public function interestOverTime($kWord, $category=0, $time='now 1-H', $property
return false;
}

/**
* @param $country
* @param $date
*
* @return array|bool
* @throws \Exception
*/
/**
* @param $country
* @param $date
*
* @return array|bool
* @throws \Exception
*/
public function trendingSearches($country, $date)
{
$params = [
Expand All @@ -161,22 +166,22 @@ public function trendingSearches($country, $date)
$data = $this->_getData(self::TRENDING_SEARCHES_URL, 'POST', $params);
if ($data) {

return Json\Json::decode($data, true);
return Json\Json::decode($data, Json\Json::TYPE_ARRAY);
} else {

return false;
}
}

/**
* @param $date
* @param $cid
* @param string $geo
* @param string $cat
*
* @return array|bool
* @throws \Exception
*/
/**
* @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 @@ -190,25 +195,25 @@ public function topCharts($date, $cid, $geo='US', $cat='')
$data = $this->_getData(self::TOP_CHARTS_URL, 'GET', $chartsPayload);
if ($data) {

return Json\Json::decode(trim($data), true);
return Json\Json::decode(trim($data), Json\Json::TYPE_ARRAY);
}
return false;
}

/**
* @param $kWord
*
* @return array|bool
* @throws \Exception
*/
/**
* @param $kWord
*
* @return array|bool
* @throws \Exception
*/
public function suggestionsAutocomplete($kWord)
{
$uri = self::SUGGESTIONS_URL . "/'$kWord'";
$param = ['hl' => $this->options['hl']];
$data = $this->_getData($uri, 'GET', $param);
if ($data) {

return Json\Json::decode(trim(substr($data, 5)), true);
return Json\Json::decode(trim(substr($data, 5)), Json\Json::TYPE_ARRAY);
}
return false;
}
Expand Down Expand Up @@ -264,7 +269,7 @@ public function interestBySubregion(array $keyWordList, $resolution='SUBREGION',
$data = $this->_getData(self::INTEREST_BY_SUBREGION_URL, 'GET', $interestBySubregionPayload);
if ($data) {

$queriesArray = Json\Json::decode(trim(substr($data, 5)), true);
$queriesArray = Json\Json::decode(trim(substr($data, 5)), Json\Json::TYPE_ARRAY);
$results[$kWord] = $queriesArray;

if (count($keyWordList)>1) {
Expand All @@ -284,32 +289,32 @@ 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)
/**
* @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;
$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)), Json\Json::TYPE_ARRAY);
}
return false;
}

/**
Expand Down Expand Up @@ -358,8 +363,12 @@ private function _getData($uri, $method, array $params=[])
}

$client = new Http\Client();
$cookieJar = tempnam('/tmp','cookie');
$client->setOptions([
'adapter' => Http\Client\Adapter\Curl::class,
'curloptions' => [
CURLOPT_COOKIEJAR => $cookieJar,
],
'maxredirects' => 10,
'timeout' => 100]);
$client->setUri($uri);
Expand Down Expand Up @@ -387,38 +396,32 @@ private function _getData($uri, $method, array $params=[])
}

$client->send();
$statusCode = $client->getResponse()->getStatusCode();
$client->setOptions([
'curloptions' => [
CURLOPT_COOKIEFILE => $cookieJar,
]]);
$client->send();
unlink($cookieJar);

$statusCode = $client->getResponse()->getStatusCode();
if ($statusCode == 200) {

$headers = $client->getResponse()->getHeaders()->toArray();
$getData = false;
foreach ($headers as $header => $value) {

if ($header == 'Content-Type') {

if (
stripos($value, 'application/json') !== false OR
stripos($value, 'application/javascript') !== false OR
stripos($value, 'text/javascript') !== false
(stripos($value, 'application/json') !== false OR
stripos($value, 'application/javascript') !== false OR
stripos($value, 'text/javascript') !== false) AND $client->getResponse()->getBody()
) {

$getData = true;

break;
return $client->getResponse()->getBody();
}
}
}

if ($getData) {

if ($body = $client->getResponse()->getBody()) {

return $body;
}
}
}

return false;
}
}
}

0 comments on commit ab6bcfa

Please sign in to comment.