|
1
|
|
|
<?php |
|
2
|
|
|
namespace SEOstats\Services; |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* SEOstats extension for Google data. |
|
6
|
|
|
* |
|
7
|
|
|
* @package SEOstats |
|
8
|
|
|
* @author Stephan Schmitz <[email protected]> |
|
9
|
|
|
* @copyright Copyright (c) 2010 - present Stephan Schmitz |
|
10
|
|
|
* @license http://eyecatchup.mit-license.org/ MIT License |
|
11
|
|
|
* @updated 2013/12/17 |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
use SEOstats\Common\SEOstatsException as E; |
|
15
|
|
|
use SEOstats\SEOstats as SEOstats; |
|
16
|
|
|
use SEOstats\Config as Config; |
|
17
|
|
|
use SEOstats\Helper as Helper; |
|
18
|
|
|
|
|
19
|
|
|
class Google extends SEOstats |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* Gets the Google Pagerank |
|
23
|
|
|
* |
|
24
|
|
|
* @param string $url String, containing the query URL. |
|
25
|
|
|
* @return integer Returns the Google PageRank. |
|
26
|
|
|
*/ |
|
27
|
2 |
|
public static function getPageRank($url = false) |
|
28
|
|
|
{ |
|
29
|
|
|
// Composer autoloads classes out of the SEOstats namespace. |
|
30
|
|
|
// The custom autolader, however, does not. So we need to include it first. |
|
31
|
2 |
|
if(!class_exists('\GTB_PageRank')) { |
|
32
|
|
|
require_once realpath(__DIR__ . '/3rdparty/GTB_PageRank.php'); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
2 |
|
$gtb = new \GTB_PageRank(parent::getUrl($url)); |
|
|
|
|
|
|
36
|
2 |
|
$result = $gtb->getPageRank(); |
|
37
|
|
|
|
|
38
|
2 |
|
return $result != "" ? $result : static::noDataDefaultValue(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Returns the total amount of results for a Google 'site:'-search for the object URL. |
|
43
|
|
|
* |
|
44
|
|
|
* @param string $url String, containing the query URL. |
|
45
|
|
|
* @return integer Returns the total site-search result count. |
|
46
|
|
|
*/ |
|
47
|
1 |
View Code Duplication |
public static function getSiteindexTotal($url = false) |
|
|
|
|
|
|
48
|
|
|
{ |
|
49
|
1 |
|
$url = parent::getUrl($url); |
|
|
|
|
|
|
50
|
1 |
|
$query = urlencode("site:{$url}"); |
|
51
|
|
|
|
|
52
|
1 |
|
return self::getSearchResultsTotal($query); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Returns the total amount of results for a Google 'link:'-search for the object URL. |
|
57
|
|
|
* |
|
58
|
|
|
* @param string $url String, containing the query URL. |
|
59
|
|
|
* @return integer Returns the total link-search result count. |
|
60
|
|
|
*/ |
|
61
|
1 |
View Code Duplication |
public static function getBacklinksTotal($url = false) |
|
|
|
|
|
|
62
|
|
|
{ |
|
63
|
1 |
|
$url = parent::getUrl($url); |
|
|
|
|
|
|
64
|
1 |
|
$query = urlencode("link:{$url}"); |
|
65
|
|
|
|
|
66
|
1 |
|
return self::getSearchResultsTotal($query); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Returns total amount of results for any Google search, |
|
71
|
|
|
* requesting the deprecated Websearch API. |
|
72
|
|
|
* |
|
73
|
|
|
* @param string $url String, containing the query URL. |
|
74
|
|
|
* @return integer Returns the total search result count. |
|
75
|
|
|
*/ |
|
76
|
3 |
|
public static function getSearchResultsTotal($url = false) |
|
77
|
|
|
{ |
|
78
|
3 |
|
$url = parent::getUrl($url); |
|
|
|
|
|
|
79
|
3 |
|
$url = sprintf(Config\Services::GOOGLE_APISEARCH_URL, 1, $url); |
|
80
|
|
|
|
|
81
|
3 |
|
$ret = static::_getPage($url); |
|
82
|
|
|
|
|
83
|
3 |
|
$obj = Helper\Json::decode($ret); |
|
84
|
3 |
|
return !isset($obj->responseData->cursor->estimatedResultCount) |
|
85
|
3 |
|
? parent::noDataDefaultValue() |
|
|
|
|
|
|
86
|
3 |
|
: intval($obj->responseData->cursor->estimatedResultCount); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
4 |
|
public static function getPagespeedAnalysis($url = false, $strategy = 'desktop') |
|
90
|
|
|
{ |
|
91
|
4 |
|
if ('' == Config\ApiKeys::getGoogleSimpleApiAccessKey()) { |
|
92
|
|
|
throw new E('In order to use the PageSpeed API, you must obtain |
|
93
|
|
|
and set an API key first (see SEOstats\Config\ApiKeys.php).'); |
|
94
|
|
|
exit(0); |
|
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
4 |
|
$url = parent::getUrl($url); |
|
|
|
|
|
|
98
|
4 |
|
$url = sprintf(Config\Services::GOOGLE_PAGESPEED_URL, |
|
99
|
4 |
|
$url, $strategy, Config\ApiKeys::getGoogleSimpleApiAccessKey()); |
|
100
|
|
|
|
|
101
|
4 |
|
$ret = static::_getPage($url); |
|
102
|
|
|
|
|
103
|
4 |
|
return Helper\Json::decode($ret); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
2 |
|
public static function getPagespeedScore($url = false, $strategy = 'desktop') |
|
107
|
|
|
{ |
|
108
|
2 |
|
$url = parent::getUrl($url); |
|
|
|
|
|
|
109
|
2 |
|
$ret = self::getPagespeedAnalysis($url, $strategy); |
|
|
|
|
|
|
110
|
|
|
|
|
111
|
|
|
// Check if $ret->score exists for backwards compatibility with v1. |
|
112
|
2 |
|
if (!isset($ret->score)) { |
|
113
|
1 |
|
return !isset($ret->ruleGroups->SPEED->score) || !$ret->ruleGroups->SPEED->score ? parent::noDataDefaultValue() : |
|
|
|
|
|
|
114
|
1 |
|
intval($ret->ruleGroups->SPEED->score); |
|
115
|
|
|
} |
|
116
|
|
|
else { |
|
117
|
1 |
|
return $ret->score; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Returns array, containing detailed results for any Google search. |
|
123
|
|
|
* |
|
124
|
|
|
* @param string $query String, containing the search query. |
|
125
|
|
|
* @param string $tld String, containing the desired Google top level domain. |
|
|
|
|
|
|
126
|
|
|
* @return array Returns array, containing the keys 'URL', 'Title' and 'Description'. |
|
127
|
|
|
*/ |
|
128
|
|
|
public static function getSerps($query, $maxResults=100, $domain=false) |
|
129
|
|
|
{ |
|
130
|
|
|
return Google\Search::getSerps($query, $maxResults, $domain); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()method in theSoncalls the wrong method in the parent class.