PHP5 class to get Alexa traffic rank

SpaceshipSpaceship
Watch

123finder.com

Established Member
Impact
2
This PHP5 class utilizes the web service provided by Alexa/Amazon to get the traffic information about the site. You'll need to sign up for an account at Alexa to get the pair (access key and secret key). The first 10,000 queries per month are free.

PHP:
<?php
/** 
* @author Son Nguyen
* @since 3/31/2006
* @package Framework
* @subpackage WebServices
*/
class CAlexaAPI {
  private $mParams;
  private $mSecretKey = 'YOUR SECRET KEY'; 
  /** constructor */
  function __construct() {
    $this->mParams = array(
      'Service'=>'AlexaWebInfoService',
      'AWSAccessKeyId'=>'YOUR ACCESS KEY', // access key id
      'Operation'=>'UrlInfo',
      'ResponseGroup'=>'Rank',
      'Url'=>NULL,
      'Timestamp'=>gmdate('Y-m-d\TH:i:s.000\Z'),
      'Signature'=>NULL,
    );
    $this->prepareSignature();
  }

  /** ref: http://developer.amazonwebservices.com/connect/thread.jspa?threadID=9675 */
  private function calculate_RFC2104HMAC ($data, $key) {
    return base64_encode(
      pack("H*", sha1((str_pad($key, 64, chr(0x00))
      ^(str_repeat(chr(0x5c), 64))) .
      pack("H*", sha1((str_pad($key, 64, chr(0x00))
      ^(str_repeat(chr(0x36), 64))) . $data))))
    );
  }


  /** have the signature ready */
  private function prepareSignature() {
    $vData = $this->mParams['Service'].$this->mParams['Operation'].$this->mParams['Timestamp'];
    $this->mParams['Signature'] = $this->calculate_RFC2104HMAC($vData,$this->mSecretKey);
  }
  

  /** make the rest call and return result as XML object */
  function call() {
    $vURL = 'http://awis.amazonaws.com/onca/xml?';
    $vTmp = array();
    foreach ($this->mParams AS $vKey=>$vValue) {
      $vTmp[] = $vKey.'='.$vValue;
    } // rof
    $vURL .= implode('&',$vTmp);
    $vFile = file_get_contents($vURL);
    $vXML = simplexml_load_string($vFile);
    return $vXML;
  }

  /** get the rank for a domain */
  function getRank($pDomain) {
    $this->mParams['Url'] = $pDomain;
    $vResult = $this->call();
    $vResult = $vResult->UrlInfoResult->Alexa->TrafficData->Rank+0;
    return $vResult;
  }
}
?>
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
Dynadot — .com TransferDynadot — .com Transfer
CatchedCatched

We're social

Escrow.com
Spaceship
Rexus Domain
CryptoExchange.com
Domain Recover
CatchDoms
DomainEasy — Payment Flexibility
DomDB
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back