-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyticsAPI.php
150 lines (133 loc) · 3.57 KB
/
analyticsAPI.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
/**
* Contains the AdobeAnalyticsAPI class used to interact with the Adobe Reporting API
* @author Jusitn Grover
* @version 1.0
* @package AdobeAPI
*/
/**
* Interface for the Adobe Reporting API
*
* @package AdobeAPI
*/
Class analyticsAPI
{
private $username = "";
private $password = "";
private $endpoint = "";
private $defaultEndpoint = "https://api.omniture.com/admin/1.3/rest/";
private $companyEndpoint = "";
/**
* Returns the username
* @return string
*/
function getUsername()
{
return $this->username;
}
/**
* Sets the username
* @returns boolean will return false if no endpoint can be determined. This usually means that the username is invalid
*/
function setUsername($username)
{
$this->username = $username;
$endpoint = $this->getEndpoint($username);
if (strpos($endpoint, "Invalid company specified.") == false)
{
$this->companyEndpoint = $endpoint;
return true;
}
else
return false;
}
/**
* Returns the password (Shared Secret)
* @return string
*/
function getPassword()
{
return $this->password;
}
/**
* Sets the password (Shared Secret)
* @returns null
*/
function setPassword($password)
{
$this->password = $password;
return true;
}
/**
* Sets the username and password.
*
* This method only needs to be called once since it will store the username and password in the object. The only time you would need to call it again
* @returns boolean
*/
function config($username, $password)
{
$validUser = $this->setUsername($username);
$this->setPassword($password);
return $validUser;
}
/**
* Figures out what the endpoint is for a particular user
* @return boolean|string
*/
private function getEndpoint($username)
{
$delimiter = strpos($username, ":");
$length = strlen($username) - $delimiter;
$company = substr($username, $length);
return $this->invoke("Company.GetEndpoint", array("company"=>$company));
}
/**
* Returns the wsse authentication header string
* @return string
*/
function generateAuthHeader ($username, $password)
{
$header = "";
$nonce = md5(rand());
$created = date("Y-m-d H:i:s");
$passwordDigest = base64_encode(sha1($nonce . $created . $password));
$header .= 'UsernameToken Username="'.$username.'", ';
$header .= 'PasswordDigest="'.$passwordDigest.'", ';
$header .= 'Nonce="'.$nonce.'", ';
$header .= 'Created="'.$created.'" ';
return $header;
}
/**
* Invokes the API
*
* Calls the API with the specified method.
* @param string $method The fully qualified API method
* @param obj $params OPTIONAL The object that contains the parameters. Generally specified as array("name"=>"value")
*
*/
function invoke($method, $params )
{
if($this->companyEndpoint)
$url = $this->companyEndpoint."?method=".$method;
else //default is used befor Company.GetEndpoint is called
$url = $this->defaultEndpoint."?method=".$method;
$ch = curl_init($url);
if($this->username && $this->password)
{
$wsseHeader = "X-WSSE : ".$this->generateAuthHeader($this->username, $this->password);
//set the security header
curl_setopt($ch, CURLOPT_HTTPHEADER, array($wsseHeader));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$apiResponse = curl_exec($ch);
curl_close($ch);
return $apiResponse;
}
else //return false if no username or password
{
return false;
}
}
}
?>