-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient-connect.php
executable file
·80 lines (62 loc) · 1.74 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
//api
//for test api
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];
$method = 'POST';
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
curl_setopt($ch, CURLOPT_URL, $host);
switch($method) {
case 'POST':
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
break;
case 'GET':
break;
default:
$data = http_build_query($data);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
break;
}
// curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_HEADER, TRUE);
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, $data) {
return $time . $id . http_build_query($data, '', '&');
}
?>