forked from jeteokeeffe/php-hmac-rest-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient-connect.php
63 lines (46 loc) · 1.33 KB
/
client-connect.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
<?php
if (!function_exists('curl_init')) {
die('Curl module not installed!' . PHP_EOL);
}
$route = '/ping';
//$route = '/test/4';
//$route = '/doesntexist';
//$route = '/skip/auth';
if (isset($argv[1])) {
$host = 'http://' . $argv[1] . $route;
} else {
$host = "http://api.example.com" . $route;
}
$privateKey = '593fe6ed77014f9507761028801aa376f141916bd26b1b3f0271b5ec3135b989';
$time = time();
$id = 1;
$data = ['name' => 'bob'];
$message = buildMessage($time, $id, $data);
$hash = hash_hmac('sha256', $message, $privateKey);
$headers = ['API_ID: ' . $id, 'API_TIME: ' . $time, 'API_HASH: ' . $hash];
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
curl_setopt($ch, CURLOPT_URL, $host);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLINFO_HEADER_OUT, TRUE);
$result = curl_exec($ch);
if ($result === FALSE) {
echo "Curl Error: " . curl_error($ch);
} else {
echo PHP_EOL;
echo "Request: " . PHP_EOL;
echo curl_getinfo($ch, CURLINFO_HEADER_OUT);
echo PHP_EOL;
echo "Response:" . PHP_EOL;
echo $result;
echo PHP_EOL;
}
curl_close($ch);
function buildMessage($time, $id, array $data) {
return $time . $id . implode($data);
}
?>