-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathapi_request.php
More file actions
70 lines (62 loc) · 2.34 KB
/
Copy pathapi_request.php
File metadata and controls
70 lines (62 loc) · 2.34 KB
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
<?php
$access_token = 'ENTER_YOUR_ACCESS_TOKEN';
$api_url = 'https://api.cryptohopper.com';
$operation = 'hopper';
$method = 'GET';
$data_string = '{}';
$path = '/v1/'.$operation;
// Cryptohopper Public API v1 uses the `access-token` header (with a
// hyphen), NOT `access_token` and NOT `Authorization: Bearer`. The AWS
// API Gateway in front of the production API rejects anything else.
// See https://www.cryptohopper.com/api-documentation/how-the-api-works
$headers = array(
'access-token: '.$access_token
);
$ch = curl_init($api_url.$path);
if($method == 'POST'){
$headers[] = 'Content-Type: application/json';
$headers[] = 'Content-Length: ' . strlen($data_string);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}elseif($method == 'GET'){
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}elseif($method == 'PATCH'){
$headers[] = 'Content-Type: application/json';
$headers[] = 'Content-Length: ' . strlen($data_string);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PATCH");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}elseif($method == 'DELETE'){
$headers[] = 'Content-Type: application/json';
$headers[] = 'Content-Length: ' . strlen($data_string);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}
$result = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);
$result_json = json_decode($result, true);
if(is_array($result_json)){
$result_json = json_encode($result_json, JSON_PRETTY_PRINT);
}
echo '<h3>Endpoint: <strong>'.$operation.'</strong></h3>';
echo '<h3>Method: <strong>'.$method.'</strong></h3>';
echo '<h3>Status code: <strong>'.$httpcode.'</strong></h3><br><hr>';
echo '<h3>Result</h3><br><br><pre>';
if(!empty($result_json)){
echo $result_json;
}else{
echo $result;
}
echo '</pre><br><br>';
if(!empty($error)){
echo '<h3>Error</h3><br><br>';
echo $error;
}
curl_close($ch);