-
Notifications
You must be signed in to change notification settings - Fork 5
/
logicboxes_api.php
133 lines (120 loc) · 3.7 KB
/
logicboxes_api.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . "logicboxes_response.php";
/**
* LogicBoxes API processor
*
* Documentation on the LogicBoxes API: http://manage.logicboxes.com/kb/answer/744
*
* @copyright Copyright (c) 2013, Phillips Data, Inc.
* @license http://opensource.org/licenses/mit-license.php MIT License
* @package logicboxes
*/
class LogicboxesApi {
const SANDBOX_URL = "https://test.httpapi.com/api/";
const LIVE_URL = "https://httpapi.com/api/";
const RESPONSE_FORMAT = "json";
/**
* @var string The reseller ID to connect as
*/
private $reseller_id;
/**
* @var string The key to use when connecting
*/
private $key;
/**
* @var boolean Whether or not to process in sandbox mode (for testing)
*/
private $sandbox;
/**
* @var array An array representing the last request made
*/
private $last_request = array('url' => null, 'args' => null);
/**
* Sets the connection details
*
* @param string $reseller_id The reseller ID to connect as
* @param string $key The key to use when connecting
* @param boolean $sandbox Whether or not to process in sandbox mode (for testing)
*/
public function __construct($reseller_id, $key, $sandbox = false) {
$this->reseller_id = $reseller_id;
$this->key = $key;
$this->sandbox = $sandbox;
}
/**
* Submits a request to the API
*
* @param string $command The command to submit (e.g. domains/available)
* @param array $args An array of key/value pair arguments to submit to the given API command
* @param string $method The request method (GET, POST, PUT, DELETE, etc.)
* @return LogicboxesResponse The response object
*/
public function submit($command, array $args = array(), $method = "POST") {
$url = self::LIVE_URL;
if ($this->sandbox)
$url = self::SANDBOX_URL;
$url .= $command . "." . self::RESPONSE_FORMAT;
$args['auth-userid'] = $this->reseller_id;
$args['api-key'] = $this->key;
$this->last_request = array(
'url' => $url,
'args' => $args
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
if ($method == "GET") {
curl_setopt($ch, CURLOPT_URL, $url . "?" . $this->buildQuery($args));
}
else {
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->buildQuery($args));
}
$response = curl_exec($ch);
curl_close($ch);
return new LogicboxesResponse($response);
}
/**
* Returns the details of the last request made
*
* @return array An array containg:
* - url The URL of the last request
* - args The paramters passed to the URL
*/
public function lastRequest() {
return $this->last_request;
}
/**
* Loads a command class
*
* @param string $command The command class filename to load
*/
public function loadCommand($command) {
require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . "commands" . DIRECTORY_SEPARATOR . $command . ".php";
}
/**
* Implementation of http_build_query() that treats numerical arrays as duplicate key values
*
* @param array An array of data
* @return string A string of parameters encoded per RFC 3986
*/
private function buildQuery(array $args) {
$query = array();
foreach ($args as $key => $value) {
if (is_array($value)) {
foreach ($value as $subkey => $subvalue) {
if (is_numeric($subkey))
$query[] = rawurlencode($key) . "=" . rawurlencode($subvalue);
else
$query[] = rawurlencode($key . "[" . $subkey . "]") . "=" . rawurlencode($subvalue);
}
}
else
$query[] = rawurlencode($key) . "=" . rawurlencode($value);
}
return implode("&", $query);
}
}
?>