1
|
|
|
<?php |
2
|
|
|
namespace SEOstats\Services; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* SEOstats extension for Mozscape (f.k.a. Seomoz) metrics. |
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/11 |
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 Mozscape extends SEOstats |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
protected static $lastLoadedDomain; |
23
|
2 |
|
protected static $lastLoadedPage; |
24
|
|
|
// A normalized 100-point score representing the likelihood |
25
|
2 |
|
// of the URL to rank well in search engine results. |
26
|
2 |
|
public static function getPageAuthority($url = false) |
27
|
2 |
|
{ |
28
|
|
|
$data = static::getCols($url); |
|
|
|
|
29
|
|
|
return (parent::noDataDefaultValue() == $data) ? $data : |
|
|
|
|
30
|
|
|
$data['upa']; |
31
|
|
|
} |
32
|
2 |
|
|
33
|
|
|
// A normalized 100-point score representing the likelihood |
34
|
2 |
|
// of the domain of the URL to rank well in search engine results. |
35
|
2 |
|
public static function getDomainAuthority($url = false) |
36
|
2 |
|
{ |
37
|
|
|
$data = static::getCols(Helper\Url::parseHost($url)); |
38
|
|
|
return (parent::noDataDefaultValue() == $data) ? $data : |
|
|
|
|
39
|
|
|
$data['pda']; |
40
|
|
|
} |
41
|
2 |
|
|
42
|
|
|
// The number of external equity links to the URL. |
43
|
2 |
|
// http://apiwiki.moz.com/glossary#equity |
44
|
2 |
|
public static function getEquityLinkCount($url = false) |
45
|
2 |
|
{ |
46
|
|
|
$data = static::getCols($url); |
|
|
|
|
47
|
|
|
return (parent::noDataDefaultValue() == $data) ? $data : |
|
|
|
|
48
|
|
|
$data['uid']; |
49
|
2 |
|
} |
50
|
|
|
|
51
|
2 |
|
// The number of links (equity or nonequity or not, internal or external) to the URL. |
52
|
2 |
|
public static function getLinkCount($url = false) |
53
|
2 |
|
{ |
54
|
|
|
$data = static::getCols($url); |
|
|
|
|
55
|
|
|
return (parent::noDataDefaultValue() == $data) ? $data : |
|
|
|
|
56
|
|
|
$data['uid']; |
57
|
2 |
|
} |
58
|
|
|
|
59
|
2 |
|
// The normalized 10-point MozRank score of the URL. |
60
|
2 |
|
public static function getMozRank($url = false) |
61
|
2 |
|
{ |
62
|
|
|
$data = static::getCols($url); |
|
|
|
|
63
|
|
|
return (parent::noDataDefaultValue() == $data) ? $data : |
|
|
|
|
64
|
|
|
$data['umrp']; |
65
|
2 |
|
} |
66
|
|
|
|
67
|
2 |
|
// The raw MozRank score of the URL. |
68
|
2 |
|
public static function getMozRankRaw($url = false) |
69
|
2 |
|
{ |
70
|
|
|
$data = static::getCols($url); |
|
|
|
|
71
|
|
|
return (parent::noDataDefaultValue() == $data) ? $data : |
|
|
|
|
72
|
|
|
number_format($data['umrr'], 16); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Return Link metrics from the (free) Mozscape (f.k.a. Seomoz) API. |
77
|
|
|
* |
78
|
|
|
* @access public |
79
|
5 |
|
* @param cols string The bit flags you want returned. |
80
|
|
|
* @param url string The URL to get metrics for. |
81
|
5 |
|
*/ |
82
|
5 |
|
public static function getCols($url = false) |
83
|
|
|
{ |
84
|
|
|
if ('' == Config\ApiKeys::get('MOZSCAPE_ACCESS_ID') || |
85
|
|
|
'' == Config\ApiKeys::get('MOZSCAPE_SECRET_KEY') |
86
|
|
|
) { |
87
|
|
|
throw new E('In order to use the Mozscape API, you must obtain |
88
|
5 |
|
and set an API key first (see SEOstats\Config\ApiKeys.php).'); |
89
|
|
|
} |
90
|
5 |
|
$host = Helper\Url::parseHost(parent::getUrl($url)); |
|
|
|
|
91
|
5 |
|
|
92
|
5 |
|
if (static::$lastLoadedDomain == $host) { |
93
|
5 |
|
return static::$lastLoadedPage; |
94
|
5 |
|
} |
95
|
5 |
|
|
96
|
5 |
|
static::$lastLoadedDomain = $host; |
97
|
|
|
|
98
|
5 |
|
$expires = time() + 300; |
99
|
|
|
$apiEndpoint = sprintf(Config\Services::MOZSCAPE_API_URL, |
100
|
5 |
|
urlencode($host), |
101
|
5 |
|
Config\ApiKeys::get('MOZSCAPE_ACCESS_ID'), |
102
|
5 |
|
$expires, |
103
|
|
|
urlencode(self::_getUrlSafeSignature($expires)) |
104
|
|
|
); |
105
|
6 |
|
|
106
|
|
|
$ret = static::_getPage($apiEndpoint); |
107
|
6 |
|
|
108
|
6 |
|
static::$lastLoadedPage = (!$ret || empty($ret) || '{}' == (string)$ret) |
109
|
|
|
? parent::noDataDefaultValue() |
|
|
|
|
110
|
6 |
|
: Helper\Json::decode($ret, true); |
111
|
|
|
return static::$lastLoadedPage; |
112
|
|
|
} |
113
|
7 |
|
|
114
|
|
|
private static function _getUrlSafeSignature($expires) |
115
|
|
|
{ |
116
|
|
|
$data = Config\ApiKeys::get('MOZSCAPE_ACCESS_ID') . "\n{$expires}"; |
117
|
7 |
|
$sig = self::_hmacsha1($data, Config\ApiKeys::get('MOZSCAPE_SECRET_KEY')); |
118
|
7 |
|
|
119
|
|
|
return base64_encode($sig); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
private static function _hmacsha1($data, $key) |
123
|
|
|
{ |
124
|
1 |
|
// Use PHP's built in functionality if available |
125
|
|
|
// (~20% faster than the custom implementation below). |
126
|
1 |
|
if (function_exists('hash_hmac')) { |
127
|
1 |
|
return hash_hmac('sha1', $data, $key, true); |
128
|
|
|
} |
129
|
1 |
|
|
130
|
|
|
return self::_hmacsha1Rebuild($data, $key); |
131
|
|
|
} |
132
|
|
|
|
133
|
1 |
|
private static function _hmacsha1Rebuild($data, $key) |
134
|
1 |
|
{ |
135
|
1 |
|
$blocksize = 64; |
136
|
1 |
|
$hashfunc = 'sha1'; |
137
|
1 |
|
|
138
|
1 |
|
if (strlen($key) > $blocksize) { |
139
|
|
|
$key = pack('H*', $hashfunc($key)); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$key = str_pad($key, $blocksize, chr(0x00)); |
143
|
|
|
$ipad = str_repeat(chr(0x36), $blocksize); |
144
|
|
|
$opad = str_repeat(chr(0x5c), $blocksize); |
145
|
|
|
$hmac = pack('H*', $hashfunc(($key ^ $opad) . |
146
|
|
|
pack('H*', $hashfunc(($key ^ $ipad) . $data)))); |
147
|
|
|
return $hmac; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: