forked from EverexIO/JSON-RPC
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequest.php
More file actions
194 lines (176 loc) · 5.15 KB
/
Request.php
File metadata and controls
194 lines (176 loc) · 5.15 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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
/**
* @package AmiLabs/JSONRPC
*/
namespace AmiLabs\JSONRPC;
/**
* Request based on cURL library implementation.
*
* @package AmiLabs/JSONRPC
* @author deepeloper ({@see https://github.com/deepeloper})
*/
class Request
{
const METHOD_GET = 1;
const METHOD_POST = 2;
const METHOD_OTHER = -1;
/**
* Default options
*
* @var array
*/
protected $defaultOptions = array(
// cURL library options
CURLOPT_HEADER => FALSE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_FOLLOWLOCATION => TRUE,
CURLOPT_CONNECTTIMEOUT => 20,
CURLOPT_TIMEOUT => 30,
CURLOPT_USERAGENT =>
'PHP AmiLabs Framework / AmiLabs\\JSONRPC\\Request',
// Class cpecific options
'resetCookiesAtStart' => TRUE,
'resetCookiesOnSend' => FALSE,
'resetCookesAtEnd' => TRUE,
'cookieFile' => '',
);
/**
* Options
*
* @var array
*/
protected $options = array();
/**
* cURL handler
*
* @var resource
*/
protected $handler;
/**
* Array containing cURL request error,
*
* @var array
* @link http://php.net/manual/en/function.curl-errno.php
* @link http://php.net/manual/en/function.curl-error.php
*/
protected $error;
/**
* @param array $options Array of cURL and internal options,
* see self::$defaultOptions
* @param bool $reset Reset previous options
*/
public function __construct(array $options = array(), $reset = FALSE)
{
$this->setOptions($this->defaultOptions);
$this->setOptions($options, (bool)$reset);
$this->handler = curl_init();
$this->prepareCookieFile('resetCookiesAtStart');
}
public function __destruct()
{
curl_close($this->handler);
$this->prepareCookieFile('resetCookiesAtEnd');
}
/**
* Sets options.
*
* @param array $options Array of cURL and internal options,
* see {@see self::$defaultOptions}
* @param bool $reset Reset previous options
*/
public function setOptions(array $options = array(), $reset = FALSE)
{
$this->options =
$reset
? $options
: $options + $this->options;
}
/**
* Sends request.
*
* @param string $url
* @param string|array $data
* @param int $method self::GET | self::POST
* @return mixed curl_exec() result
* @limk http://php.net/manual/en/function.curl-exec.php
*/
public function send($url, $data = '', $method = self::METHOD_GET)
{
$this->prepareCookieFile('resetCookiesOnSend');
$options = array();
foreach (array_keys($this->options) as $key) {
if (is_int($key)) {
$options[$key] = $this->options[$key];
}
}
if (
isset($this->options['cookieFile']) &&
'' !== $this->options['cookieFile']
) {
$options = array(
CURLOPT_COOKIEFILE => $this->options['cookieFile'],
CURLOPT_COOKIEJAR => $this->options['cookieFile'],
);
}
curl_setopt_array($this->handler, $options);
if (is_array($data)) {
$data = http_build_query($data);
}
switch ($method) {
case self::METHOD_GET:
if ('' !== $data) {
$url .=
(FALSE === mb_strpos($url, '?') ? '?' : '&') .
$data;
}
curl_setopt($this->handler, CURLOPT_POST, FALSE);
break;
case self::METHOD_POST:
curl_setopt($this->handler, CURLOPT_POST, TRUE);
curl_setopt($this->handler, CURLOPT_POSTFIELDS, $data);
break;
}
if ('' !== $url) {
curl_setopt($this->handler, CURLOPT_URL, $url);
}
$result = curl_exec($this->handler);
$this->error = array(
'code' => curl_errno($this->handler),
'message' => curl_error($this->handler)
);
return $result;
}
/**
* Returns cURL request info.
*
* @parm int $option
* @return mixed
* @limk http://php.net/manual/en/function.curl-getinfo.php
*/
public function getInfo($option = 0)
{
return $option ? curl_getinfo($this->handler, $option) : curl_getinfo($this->handler);
}
/**
* Returns cURL error.
*
* @return array
* @limk http://php.net/manual/en/function.curl-errno.php
* @limk http://php.net/manual/en/function.curl-error.php
*/
public function getError()
{
return $this->error;
}
protected function prepareCookieFile($mode)
{
if (
isset($this->options['cookieFile']) &&
'' !== $this->options['cookieFile'] &&
file_exists($this->options['cookieFile']) &&
!empty($this->options[$mode])
) {
unlink($this->options['cookieFile']);
}
}
}