forked from tylerhall/php-aws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.awis.php
123 lines (110 loc) · 3.49 KB
/
class.awis.php
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
<?PHP
class AWIS
{
var $_key = "";
var $_secret = "";
var $_server = "http://awis.amazonaws.com";
var $_pathToCurl = "";
var $_date = null;
var $_error = null;
var $_version = "2005-07-11";
function AWIS($key = null, $secret = null)
{
if($key && $secret)
{
$this->_key = $key;
$this->_secret = $secret;
}
// If the path to curl isn't set, try and auto-detect it
if($this->_pathToCurl == "")
{
$path = trim(shell_exec("which curl"), "\n ");
if(is_executable($path))
$this->_pathToCurl = $path;
else
{
$this->_error = "Couldn't auto-detect path to curl";
return false;
}
}
return true;
}
function urlInfo($url, $responseGroup)
{
$req = array( "Url" => $url, "ResponseGroup" => $responseGroup );
$result = $this->go("UrlInfo", $req);
return $result->Response->UrlInfoResult->Alexa;
}
function trafficHistory($url, $range=30, $start="")
{
if (!$start) {
$start = date("Ymd", mktime(0,0,0,date("m"),date("d")-31,date("Y")));
}
$req = array( "Url" => $url, "ResponseGroup" => "History", "Start" => $start, "Range" => $range );
$result = $this->go("TrafficHistory", $req);
return $result->Response->TrafficHistoryResult->Alexa->TrafficHistory;
}
function sitesLinkingIn($url, $start=0, $count=10)
{
$req = array( "Url" => $url, "ResponseGroup" => "SitesLinkingIn", "Start" => $start, "Count" => $count );
$result = $this->go("SitesLinkingIn", $req);
return $result->Response->SitesLinkingInResult->Alexa->SitesLinkingIn;
}
function go($action, $params, $url = null)
{
if(!is_array($params)) $params = array();
$params['Action'] = $action;
$params['Timestamp'] = gmdate("Y-m-d\TH:i:s.\\0\\0\\0\\Z", time());
$params['Version'] = $this->_version;
$params['AWSAccessKeyId'] = $this->_key;
$params['Signature'] = $this->calculate_RFC2104HMAC($params['Action'] . $params['Timestamp'],$this->_secret);
if(!isset($url)) $url = $this->_server;
$url .= "?";
foreach($params as $key => $val)
$url .= "$key=" . urlencode($val)."&";
$xmlstr = $this->geturl($url);
$xmlstr = preg_replace("/<(\/?)aws:/","<$1",$xmlstr);
$xml = simplexml_load_string($xmlstr);
if(isset($xml->Errors))
return false;
else
return $xml;
}
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))))
);
}
function geturl($url, $username = "", $password = "")
{
if(function_exists("curl_init"))
{
$ch = curl_init();
if(!empty($username) && !empty($password)) curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Basic ' . base64_encode("$username:$password")));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
$html = curl_exec($ch);
curl_close($ch);
return $html;
}
elseif(ini_get("allow_url_fopen") == true)
{
if(!empty($username) && !empty($password))
{
$url = str_replace("http://", "http://$username:$password@", $url);
$url = str_replace("https://", "https://$username:$password@", $url);
}
$html = file_get_contents($url);
return $html;
}
else
{
// Cannot open url. Either install curl-php or set allow_url_fopen = true in php.ini
return false;
}
}
}