1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SandCage; |
4
|
|
|
|
5
|
|
|
class SandCage { |
6
|
|
|
// Your SandCage API Key |
7
|
|
|
// This can be retrieved from https://www.sandcage.com/panel/api_key |
8
|
|
|
protected $sandcage_api_key = '[YOUR SANDCAGE API KEY]'; |
9
|
|
|
|
10
|
|
|
// SandCage API version |
11
|
|
|
protected $sandcage_api_version = '0.2'; |
12
|
|
|
protected $sandcage_api_endpoint_base; |
13
|
|
|
protected $user_agent; |
14
|
|
|
protected $follow_location = false; |
15
|
|
|
protected $timeout = 30; |
16
|
|
|
protected $post_fields; |
17
|
|
|
public $status; |
18
|
|
|
public $response; |
19
|
|
|
|
20
|
|
|
public function __construct($sandcage_api_key = null) { |
21
|
|
|
if (!is_null($sandcage_api_key)) { |
22
|
|
|
$this->sandcage_api_key = $sandcage_api_key; |
23
|
|
|
} |
24
|
|
|
// Handle open_basedir & safe mode |
25
|
|
|
if (!ini_get('safe_mode') && !ini_get('open_basedir')) { |
26
|
|
|
$this->follow_location = true; |
27
|
|
|
} |
28
|
|
|
$this->sandcage_api_endpoint_base = 'https://api.sandcage.com/' . $this->sandcage_api_version . '/'; |
29
|
|
|
$this->user_agent = 'SandCage - ' . $this->sandcage_api_version; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Send a requst using cURL |
34
|
|
|
* @param string $service endpoint to request |
35
|
|
|
* @param array $payload values to send |
36
|
|
|
* @param string $callback_endpoint to send the callback to. Optional |
37
|
|
|
*/ |
38
|
|
|
public function call($service, $payload, $callback_endpoint = '') { |
39
|
|
|
$this->payloadArray($payload, $callback_endpoint); |
40
|
|
|
$this->curlCall($this->sandcage_api_endpoint_base . $service); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Send a requst using cURL |
45
|
|
|
* @param string $service_endpoint to request |
46
|
|
|
*/ |
47
|
|
|
public function curlCall($service_endpoint) { |
48
|
|
|
$ch = curl_init($service_endpoint); |
49
|
|
|
curl_setopt($ch, CURLOPT_USERAGENT, $this->user_agent); |
50
|
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $this->follow_location); |
51
|
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout); |
52
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); |
53
|
|
|
curl_setopt($ch, CURLOPT_POST, TRUE); |
54
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($this->post_fields)); |
55
|
|
|
$this->response = curl_exec($ch); |
56
|
|
|
// Retry if certificates are missing. |
57
|
|
|
if (curl_errno($ch) == CURLE_SSL_CACERT) { |
58
|
|
|
curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . '/cacert.pem'); |
59
|
|
|
// Retry execution after setting CURLOPT_CAINFO |
60
|
|
|
$this->response = curl_exec($ch); |
61
|
|
|
} |
62
|
|
|
$this->status = curl_getinfo($ch); |
63
|
|
|
curl_close($ch); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Build the payload array |
68
|
|
|
* @param array $payload values to send |
69
|
|
|
* @param string $callback_endpoint to send the callback to |
70
|
|
|
*/ |
71
|
|
|
private function payloadArray($payload, $callback_endpoint) { |
72
|
|
|
$this->post_fields = array('key'=>$this->sandcage_api_key) + $payload; |
73
|
|
|
|
74
|
|
|
if ($callback_endpoint != '') { |
75
|
|
|
$this->post_fields['callback_url'] = $callback_endpoint; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|