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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?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;   
  }   
}   
?>  
<?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;
  }
}
?>

Sample Usage: view plaincopy to clipboardprint?

1
2
3
4
5
6
$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

发表评论

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