-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_users.php
65 lines (56 loc) · 2.34 KB
/
get_users.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
<?php
// Settings
$account_alias = 'demo'; // From Envoy, e.g., for http://example.envoyapp.com/ this would be "example".
$api_key = 'CB4BH8FF2ZH9BD333HTVDD85NYMCPMC1'; // Generate this in Envoy (Settings > API).
// Build URL.
$url = 'http://' . $account_alias . '.envoyapp.com/api/users/';
// Sign the request
$request_time = date("U"); // Used to sign request
$api_signature = md5($api_key . $request_time);
// Get and decode data
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('X_API_SIGNATURE: ' . $api_signature, 'X_API_REQUEST_TIME: ' . $request_time));
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$file = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode != 200) {
// Code other than 200 means there was an error. Handle as appropriate.
echo '<p>There was an error.</p>';
die();
}
// Convert JSON to array.
$remote = json_decode(trim($file), true);
// If remote isn't set, there was something wrong with the response.
if (!$remote) {
echo '<p>There was a problem with the response.</p>';
/*echo '<pre>';
var_dump($file);
echo '</pre>';*/
die();
}
// Check response is OK
if ($remote['result'] == 'error') {
// There was a problem. $remote['messages'] will contain information about what went wrong.
echo '<p>Error fetching users.</p><ul>';
foreach ($remote['messages'] as $message) {
echo '<li>' . $message . '</li>';
}
echo '</ul>';
die();
} elseif ($remote['result'] != 'success') {
// There was a problem, and the response was not in the right format.
echo '<p>Error fetching users. Response in wrong format.</p>';
/*echo '<pre>';
var_dump($remote);
echo '</pre>';*/
die();
}
// Handle data as required ...
echo '<pre>';
var_dump($remote);
echo '</pre>';