-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.php
60 lines (55 loc) · 1.97 KB
/
util.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
<?php
error_reporting('E_WARNING');
/**
* Posts $data to $url and expects JSON data in return
*/
function json_post($url, $data) {
$json_data = json_encode($data);
$c = curl_init($url);
curl_setopt($c, CURLOPT_RETURNTRANSFER,true);
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_POSTFIELDS, $json_data);
curl_setopt($c, CURLOPT_FAILONERROR,true);
curl_setopt($c, CURLOPT_HTTPHEADER,array('Content-Type: application/json'));
$response = curl_exec($c);
$http_status = curl_getinfo($c, CURLINFO_HTTP_CODE);
$error = curl_error($c);
curl_close($c);
if ($http_status >= 200 && $http_status < 300 ) {
// 200 - 299 inclusive
return json_decode($response, true);
} else {
throw new Exception($error, $http_status);
}
}
/**
* Enable CORS for the current request
*
* See: http://www.w3.org/TR/cors/
* See: http://www.html5rocks.com/en/tutorials/cors/#toc-cors-server-flowchart
*/
function enable_cors() {
if (isset($_SERVER['HTTP_ORIGIN'])) {
// CORS detected
header('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN']);
// Uncomment to allow cookies
//header('Access-Control-Allow-Credentials: true');
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD'])) {
// Pre-flight request
header('Access-Control-Allow-Headers: Content-Type,X-Requested-With');
// Can optionally consume the following request headers:
// Access-Control-Request-Method
// Access-Control-Request-Headers
// Can optionally produce the following response headers:
// Access-Control-Allow-Methods
// Access-Control-Max-Age
}
exit;
} else {
// Regular request
// Can optionally produce the following response headers:
// Access-Control-Expose-Headers
}
}
}