PageRenderTime 23ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/SEOstats/Services/Mozscape.php

http://github.com/eyecatchup/SEOstats
PHP | 140 lines | 93 code | 20 blank | 27 comment | 14 complexity | 2892098277e3901965b78bd50b0b8292 MD5 | raw file
  1. <?php
  2. namespace SEOstats\Services;
  3. /**
  4. * SEOstats extension for Mozscape (f.k.a. Seomoz) metrics.
  5. *
  6. * @package SEOstats
  7. * @author Stephan Schmitz &lt;eyecatchup@gmail.com&gt;
  8. * @copyright Copyright (c) 2010 - present Stephan Schmitz
  9. * @license http://eyecatchup.mit-license.org/ MIT License
  10. * @updated 2013/12/11
  11. */
  12. use SEOstats\Common\SEOstatsException as E;
  13. use SEOstats\SEOstats as SEOstats;
  14. use SEOstats\Config as Config;
  15. use SEOstats\Helper as Helper;
  16. class Mozscape extends SEOstats
  17. {
  18. // A normalized 100-point score representing the likelihood
  19. // of the URL to rank well in search engine results.
  20. public static function getPageAuthority($url = false)
  21. {
  22. $data = static::getCols('34359738368', $url);
  23. return (parent::noDataDefaultValue() == $data) ? $data :
  24. $data['upa'];
  25. }
  26. // A normalized 100-point score representing the likelihood
  27. // of the domain of the URL to rank well in search engine results.
  28. public static function getDomainAuthority($url = false)
  29. {
  30. $data = static::getCols('68719476736', Helper\Url::parseHost($url));
  31. return (parent::noDataDefaultValue() == $data) ? $data :
  32. $data['pda'];
  33. }
  34. // The number of external equity links to the URL.
  35. // http://apiwiki.moz.com/glossary#equity
  36. public static function getEquityLinkCount($url = false)
  37. {
  38. $data = static::getCols('2048', $url);
  39. return (parent::noDataDefaultValue() == $data) ? $data :
  40. $data['uid'];
  41. }
  42. // The number of links (equity or nonequity or not, internal or external) to the URL.
  43. public static function getLinkCount($url = false)
  44. {
  45. $data = static::getCols('2048', $url);
  46. return (parent::noDataDefaultValue() == $data) ? $data :
  47. $data['uid'];
  48. }
  49. // The normalized 10-point MozRank score of the URL.
  50. public static function getMozRank($url = false)
  51. {
  52. $data = static::getCols('16384', $url);
  53. return (parent::noDataDefaultValue() == $data) ? $data :
  54. $data['umrp'];
  55. }
  56. // The raw MozRank score of the URL.
  57. public static function getMozRankRaw($url = false)
  58. {
  59. $data = static::getCols('16384', $url);
  60. return (parent::noDataDefaultValue() == $data) ? $data :
  61. number_format($data['umrr'], 16);
  62. }
  63. /**
  64. * Return Link metrics from the (free) Mozscape (f.k.a. Seomoz) API.
  65. *
  66. * @access public
  67. * @param cols string The bit flags you want returned.
  68. * @param url string The URL to get metrics for.
  69. */
  70. public static function getCols($cols, $url = false)
  71. {
  72. if ('' == Config\ApiKeys::getMozscapeAccessId() ||
  73. '' == Config\ApiKeys::getMozscapeSecretKey()) {
  74. throw new E('In order to use the Mozscape API, you must obtain
  75. and set an API key first (see SEOstats\Config\ApiKeys.php).');
  76. exit(0);
  77. }
  78. $expires = time() + 300;
  79. $apiEndpoint = sprintf(Config\Services::MOZSCAPE_API_URL,
  80. urlencode(Helper\Url::parseHost(parent::getUrl($url))),
  81. $cols,
  82. Config\ApiKeys::getMozscapeAccessId(),
  83. $expires,
  84. urlencode(self::_getUrlSafeSignature($expires))
  85. );
  86. $ret = static::_getPage($apiEndpoint);
  87. return (!$ret || empty($ret) || '{}' == (string)$ret)
  88. ? parent::noDataDefaultValue()
  89. : Helper\Json::decode($ret, true);
  90. }
  91. private static function _getUrlSafeSignature($expires)
  92. {
  93. $data = Config\ApiKeys::getMozscapeAccessId() . "\n{$expires}";
  94. $sig = self::_hmacsha1($data, Config\ApiKeys::getMozscapeSecretKey());
  95. return base64_encode($sig);
  96. }
  97. private static function _hmacsha1($data, $key)
  98. {
  99. // Use PHP's built in functionality if available
  100. // (~20% faster than the custom implementation below).
  101. if (function_exists('hash_hmac')) {
  102. return hash_hmac('sha1', $data, $key, true);
  103. }
  104. return self::_hmacsha1Rebuild($data, $key);
  105. }
  106. private static function _hmacsha1Rebuild($data, $key)
  107. {
  108. $blocksize = 64;
  109. $hashfunc = 'sha1';
  110. if (strlen($key) > $blocksize) {
  111. $key = pack('H*', $hashfunc($key));
  112. }
  113. $key = str_pad($key, $blocksize, chr(0x00));
  114. $ipad = str_repeat(chr(0x36), $blocksize);
  115. $opad = str_repeat(chr(0x5c), $blocksize);
  116. $hmac = pack('H*', $hashfunc(($key^$opad) .
  117. pack('H*', $hashfunc(($key^$ipad) . $data))));
  118. return $hmac;
  119. }
  120. }