-
Notifications
You must be signed in to change notification settings - Fork 4
/
functions.inc.php
144 lines (108 loc) · 3.82 KB
/
functions.inc.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
<?php
/*
* loadRequestParameters
* ---------------------
* Loop through $_POST and load parameters into an array.
*/
function loadRequestParameters () {
global $_POST;
// Initialize request array.
$parameters = array();
// Request parameters are specified by prefixes of 'param_name/param_value'.
foreach ( $_POST as $key => $value ):
if ( strpos($key, 'param_name_') === 0 ):
$param_id = substr($key, 11);
$parameters[$value] = $_POST['param_value_' . $param_id];
endif;
endforeach;
return $parameters;
}
/*
* generateRequest
* ---------------
* Compute the API signature using HMAC-SHA256. The HTTP method (e.g., "GET")
* should be concatenated with the rawurlencoded request URL, separated by an
* ampersand (“&”), then passed to the hash algorithm. The resulting signature
* should be appended to the request URL and returned.
*/
function generateRequest ( $http_method, $base_url, $api_secret, $parameters = array() ) {
// Append current time to request parameters (seconds from UNIX epoch).
$parameters['timestamp'] = time();
// Sort the request parameters.
ksort($parameters);
// Collapse the parameters into a URI query string.
$query_string = http_build_query($parameters, '', '&');
// Add the request parameters to the base URL.
$request_url = $base_url . '?' . $query_string;
// Compute the request signature (see specification).
$hash_input = $http_method . '&' . rawurlencode($request_url);
$api_signature = hash_hmac('sha256', $hash_input, $api_secret);
// Append the signature to the request.
return $request_url . '&signature=' . $api_signature;
}
/*
* sendRequest
* -----------
* Send a RESTful request to the API.
*/
function sendRequest ( $http_method, $request_url, $request_body = '' ) {
// Initialize a cURL session.
$ch = curl_init();
$headers = array('Accept: application/json');
// Set cURL options.
curl_setopt($ch, CURLOPT_URL, $request_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, false);
// curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
// Validate certificates.
if ( substr($request_url, 0, 23) === "https://apidev.mla.org/" ):
// openssl x509 -in /path/to/self-signed.crt -text > self-signed.pem
curl_setopt($ch, CURLOPT_CAINFO, getcwd() . '/ssl/self-signed.pem');
elseif ( substr($request_url, 0, 20) === "https://api.mla.org/" ):
curl_setopt($ch, CURLOPT_CAINFO, getcwd() . '/ssl/self-signed-production.pem');
//elseif ( substr($request_url, 0, 20) === "https://api.mla.org/" ):
// curl_setopt($ch, CURLOPT_CAINFO, getcwd() . '/ssl/cacert.pem');
endif;
// Set HTTP method.
if ( $http_method === 'PUT' || $http_method === 'DELETE' ):
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $http_method);
elseif ($http_method === 'POST'):
curl_setopt($ch, CURLOPT_POST, 1);
endif;
// Add request body.
if ( strlen($request_body) ):
$headers[] = 'Content-Length: ' . strlen($request_body);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request_body);
endif;
// Add HTTP headers.
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// Send request.
$response_text = curl_exec($ch);
// Describe error if request failed.
if ( !$response_text ):
$response = array(
'code' => '500',
'body' => curl_error($ch)
);
else:
$response = array(
'code' => curl_getinfo($ch, CURLINFO_HTTP_CODE),
'body' => $response_text
);
endif;
// Close cURL session.
curl_close($ch);
return $response;
}
/*
* isValidJSON
* -----------
* Determines if string can be parsed as JSON.
* http://stackoverflow.com/questions/6041741/
*/
function isValidJSON ( $str ) {
json_decode($str);
return ( json_last_error() === JSON_ERROR_NONE );
}