forked from TribeHR/AppDirect-PHP-Wrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppDirectConnector.php
120 lines (96 loc) · 3.22 KB
/
AppDirectConnector.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
<?php
class AppDirectConnector
{
var $key = APPDIRECT_CONSUMER_KEY;
var $secret = APPDIRECT_CONSUMER_SECRET;
var $endpoint = 'https://www.appdirect.com/rest/api/';
var $consumer = false;
function __construct($key = null, $secret = null)
{
if($key !== null)
$this->key = $key;
if($secret !== null)
$this->secret = $secret;
// Create our OAuth Consumer
$this->consumer = new OAuthConsumer($this->key, $this->secret);
}
function getSignedUrl($url)
{
$request = OAuthRequest::from_consumer_and_token($this->consumer, NULL, 'GET', $url, NULL);
$request->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $this->consumer, NULL);
$url = $request->to_url();
return $url;
}
function verifySignature() {
$request = OAuthRequest::from_request();
$requestParams = $request->get_parameters();
$signer = new OAuthSignatureMethod_HMAC_SHA1();
return $signer->check_signature($request, $this->consumer, null, $requestParams['oauth_signature']);
}
// Decide if the parameter given is a legacy Token, or a modern EventUrl
function isEventUrl($apiToken)
{
return (strtolower(substr($apiToken, 0, 4)) == 'http');
}
// Fetch data from AppDirect using an OAuth signed request
function get($path, $data = array())
{
return $this->request($path, $data, 'GET');
}
function post($path, $data = array())
{
return $this->request($path, $data, 'POST');
}
function request($url, $data = array(), $method = 'GET')
{
if (strtoupper($method) == 'POST')
$method = 'POST';
else
$method = 'GET';
// We need to know where to request against.
// Legacy API calls require the hard enpoint, new ones provide a full URL
if (!$this->isEventUrl($url))
$url = $this->endpoint . $url;
// Create a signed request for this request
$request = OAuthRequest::from_consumer_and_token($this->consumer, NULL, $method, $url, null);
$request->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $this->consumer, NULL);
$auth_header = $request->to_header();
// Set up curl
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FAILONERROR, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
if ($method == 'POST')
{
curl_setopt($curl, CURLOPT_HTTPHEADER, array($auth_header, 'Content-type: application/xml'));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
else
{
curl_setopt($curl, CURLOPT_HTTPHEADER, array($auth_header));
}
// Fetch the data
$response = curl_exec($curl);
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
// Do some verification on it
$curl_error = ($code > 0 ? null : curl_error($curl) . ' (' . curl_errno($curl) . ')');
curl_close($curl);
if ($curl_error) // Connection Error?
{
throw new AppDirectConnectionException('An error occurred while connecting to AppDirect: ' . $curl_error);
return;
}
elseif ($code == 200) // Created Successfully
{
$result = simplexml_load_string($response);
return $result;
}
elseif ($code == 422) { // Unprocessible Entity
$errors = new SimpleXMLElement($response);
throw new AppDirectValidationException($code, $errors);
return;
}
}
}
?>