Alexa Traffic Rank PHP5 Class

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. view plaincopy to clipboardprint?

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;   
  }   
}   
?>  
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;
  }
}
?>

Sample Usage: view plaincopy to clipboardprint?

$vAlexa = new CAlexaAPI();   
$vResult = $vAlexa->getRank('www.adspeed.com');   
var_dump($vResult); // return int 23974  
$vAlexa = new CAlexaAPI();
$vResult = $vAlexa->getRank('www.adspeed.com');
var_dump($vResult); // return int 23974

原文http://www.adspeed.org/2006/03/alexa-traffic-rank-php5-class.html

发表评论

电子邮件地址不会被公开。 必填项已用*标注