-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPH7Client.php
executable file
·259 lines (216 loc) · 7.12 KB
/
PH7Client.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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<?php
/**
* @title pH7Client
* @desc pH7Client simulates a Web browser. It automates the task of retrieving web page content and posting forms, for example.
*
* @author Pierre-Henry Soria <hello@ph7cms.com>
* @copyright (c) 2015-2017, Pierre-Henry Soria. All Rights Reserved.
* @license GNU General Public License; See PH7.LICENSE.txt and PH7.COPYRIGHT.txt in the root directory.
* @package PH7 / External / HTTP / Client
*/
namespace PH7\External\Http\Client;
use InvalidArgumentException;
/**
* First off, we check the requirements of the class.
*/
if (version_compare(PHP_VERSION, '5.4.0', '<')) {
exit('Your PHP version is ' . PHP_VERSION . '. PH7Client.php requires PHP 5.4 or newer.');
}
if (!function_exists('curl_init')) {
exit('PH7Client.php requires cURL PHP library. Please install it before running the class.');
}
class PH7Client
{
const PLAIN_TYPE = 1;
const ARR_TYPE = 2;
const OBJ_TYPE = 3;
const USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:11.0) Gecko/20100101 Firefox/11.0';
const POST_METHOD = 'POST';
const GET_METHOD = 'GET';
const PUT_METHOD = 'PUT';
const DELETE_METHOD = 'DELETE';
/** @var resource */
private $rCurl;
/** @var string */
private $sRemoteDomain;
/** @var null|string */
private $sUrl;
/** @var null|string */
private $sType;
/** @var null|string */
private $sSslPath;
/** @var null|string */
private $sResponse;
/** @var string */
private $sCookieFileName = 'cookie_log.txt';
/** @var bool */
private $bHeader = true;
/** @var null|array */
private $aParams;
/** @var array */
private $aAllowTypes;
/**
* Assign values to the attributes.
*
* @param string $sRemoteDomain The URL of where you want to execute the actions.
* @param string $sSslPath If it uses SSL certificate, you have to specify the certificate directory here. Ex: "/path/certificate.pem". Default: NULL
*/
public function __construct($sRemoteDomain, $sSslPath = null)
{
$this->rCurl = curl_init();
$this->sRemoteDomain = (substr($sRemoteDomain, -1) !== '/' ? $sRemoteDomain . '/' : $sRemoteDomain); // The domain has to finished by a Slash "/"
$this->sSslPath = $sSslPath;
$this->aAllowTypes = [self::GET_METHOD, self::POST_METHOD, self::PUT_METHOD, self::DELETE_METHOD];
}
/**
* @param string $sUrl
* @param array $aParms
*
* @return self
*/
public function get($sUrl, array $aParms)
{
$this->set($sUrl, $aParms, self::GET_METHOD);
return $this;
}
/**
* @param string $sUrl
* @param array $aParms
*
* @return self
*/
public function post($sUrl, array $aParms)
{
$this->set($sUrl, $aParms, self::POST_METHOD);
return $this;
}
/**
* @param string $sUrl
* @param array $aParms
*
* @return self
*/
public function put($sUrl, array $aParms)
{
$this->set($sUrl, $aParms, self::PUT_METHOD);
return $this;
}
/**
* @param string $sUrl
* @param array $aParms
*
* @return self
*/
public function delete($sUrl, array $aParms)
{
$this->set($sUrl, $aParms, self::DELETE_METHOD);
return $this;
}
/**
* @param bool $bHeader If TRUE, it passes headers to the data stream. Default: TRUE
*
* @return self
*/
public function setHeader($bHeader = true)
{
$this->bHeader = $bHeader;
return $this;
}
/**
* Get the response.
*
* @param integer $sType The type of response. Can be 'PH7Client::OBJ_TYPE', 'PH7Client::ARR_TYPE', or 'PH7Client::PLAIN_TYPE'
*
* @return string|array|object The response into Plain, Array or Object format.
*
* @throws InvalidArgumentException If the type (specified in $sType parameter) is invalid.
*/
public function getResponse($sType = self::PLAIN_TYPE)
{
switch ($sType) {
case static::OBJ_TYPE:
return json_decode($this->sResponse);
break;
case static::ARR_TYPE:
return json_decode($this->sResponse, true);
break;
case static::PLAIN_TYPE:
return $this->sResponse;
break;
default:
throw new InvalidArgumentException('Invalide Response Type. The type can only be "PH7Client::OBJ_TYPE", "PH7Client::ARR_TYPE", or "PH7Client::PLAIN_TYPE"');
}
}
public function getCookieFile()
{
return $this->sCookieFileName;
}
/**
* Change the location of the cookie file (where the cookies are stored).
*
* @param string $sFileName Path to the file.
*
* @return self
*/
public function setCookieFile($sFileName)
{
$this->sCookieFileName = $sFileName;
return $this;
}
/**
* Sent data to the remote site.
*
* @return self
*
* @throws InvalidArgumentException If the type (specified in $sType parameter) is invalid.
*/
public function send()
{
if (!in_array($this->sType, $this->aAllowTypes)) {
throw new InvalidArgumentException('The Request Type can be only "GET", "POST", "PUT" or "DELETE!"');
}
$sPostString = http_build_query($this->aParams, '', '&');
curl_setopt($this->rCurl, CURLOPT_URL, $this->sRemoteDomain . $this->sUrl);
curl_setopt($this->rCurl, CURLOPT_HEADER, $this->bHeader);
curl_setopt($this->rCurl, CURLOPT_POSTFIELDS, $sPostString);
curl_setopt($this->rCurl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->rCurl, CURLINFO_HEADER_OUT, true);
curl_setopt($this->rCurl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($this->rCurl, CURLOPT_CUSTOMREQUEST, $this->sType);
curl_setopt($this->rCurl, CURLOPT_COOKIESESSION, true);
curl_setopt($this->rCurl, CURLOPT_COOKIEJAR, $this->getCookieFile());
curl_setopt($this->rCurl, CURLOPT_COOKIEFILE, $this->getCookieFile());
curl_setopt($this->rCurl, CURLOPT_USERAGENT, static::USER_AGENT);
if (!empty($this->sSslPath)) {
curl_setopt($this->rCurl, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($this->rCurl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($this->rCurl, CURLOPT_CAINFO, $this->sSslPath);
}
// Set the Response into an attribute
$this->sResponse = curl_exec($this->rCurl);
return $this;
}
/**
* Assign values.
*
* @param string $sUrl The target URL to send the data.
* @param array $aParms The request parameters to send.
* @param string $sType The type of request. Choose only between: 'GET', 'POST', 'PUT' and 'DELETE'.
*
* @return self
*/
private function set($sUrl, array $aParms, $sType)
{
$this->sUrl = $sUrl;
$this->aParams = $aParms;
$this->sType = $sType;
return $this;
}
/**
* Close cURL connection.
*/
public function __destruct()
{
curl_close($this->rCurl);
}
}