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