-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-archery-logbook-integration.php
95 lines (82 loc) · 2.89 KB
/
class-archery-logbook-integration.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
<?php
if (!defined('ABSPATH')) exit;
class Archery_Logbook_Integration {
public function __construct() {
}
public function milliseconds() {
date_default_timezone_set("Pacific/Auckland");
$mt = explode(' ', microtime());
return ((int)$mt[1]) * 1000 + ((int)round($mt[0] * 1000));
}
public function prepare_http_headers($accessKey, $secret, $path) {
$nonce = uniqid();
$timestamp = $this->milliseconds();
// Build the signature string from the parameters.
$signatureString = $path . $accessKey . $nonce . $timestamp;
// Create the HMAC SHA-256 Hash from the signature string.
$signature = base64_encode(hash_hmac('sha256', $signatureString, $secret, true));
$resultMap = array(
'Content-type: application/json',
'key: '. $accessKey,
'nonce: ' . $nonce,
'timestamp: ' . $timestamp,
'signature: ' . $signature
);
return $resultMap;
}
public function send_request($url, $method, $headers, $data) {
$curl = curl_init();
$params = array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_URL => $url,
CURLOPT_POST => true,
CURLOPT_SSL_VERIFYHOST => false,//to ignore self-signed cert
CURLOPT_SSL_VERIFYPEER => false,//to ignore self-signed cert
CURLOPT_VERBOSE => 1,
CURLOPT_TIMEOUT, 300
);
curl_setopt_array($curl, $params);
switch ($method) {
case "POST":
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
case "GET":
curl_setopt($curl, CURLOPT_HTTPGET, true);
break;
case "PUT":
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT'); //(CURLOPT_PUT, true) does not work for some reason. Probably beacuse of older version of PHP
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
case "DELETE":
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
break;
default:
break;
}
//curl_setopt($ch, CURLOPT_USERPWD, $user . ':' . $secret);
$result = null;
$error = null;
$httpcode = null;
try {
$result = curl_exec($curl);
$httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($result) {
$errno = curl_errno($curl);
$error = curl_error($curl);
if ($errno != 0) {
error_log('Backend Error: errorCode=' . $errno . ' ErrorMessage: ' . $error);
}
}
curl_close($curl);
} catch (HttpException $ex) {
error_log('HTTP Error: ' . $ex);
}
return array(
'response' => $result,
'httpcode' => $httpcode,
'error' => $error);
}
}
?>