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
|
<?php
/**
* PCurl is a REST client libary for PHP.
*
* See http://github.com/purplecode/php.curl for details.
*
* This code is licensed for use, modification, and distribution
* under the terms of the MIT License (see http://en.wikipedia.org/wiki/MIT_License)
*/
namespace PurpleCode\PCurl;
require_once 'PCurlException.php';
use PurpleCode\PCurl\PCurlException;
class PCurl {
private $options;
private $host;
private $headers;
public function __construct($host) {
if (!function_exists('curl_init')) {
throw new PCurlException('CURL module not available! See http://php.net/manual/en/book.curl.php');
}
$this->host = $host;
$this->headers = array();
$this->options = array();
$this->setOption(CURLOPT_TIMEOUT, 30);
// forced to use SSL3.0
$this->setOption(CURLOPT_SSLVERSION, 3);
// verify SSL certificates
$this->setOption(CURLOPT_SSL_VERIFYPEER, true);
// should curl_exec return response, not print it on stdout
$this->setOption(CURLOPT_RETURNTRANSFER, true);
// should not include headers in response
$this->setOption(CURLOPT_HEADER, 0);
}
/**
* @return string
*/
public function call($method, $url, $payload = '') {
if ($method == "POST") {
$this->setOption(CURLOPT_POST, true);
$this->setOption(CURLOPT_POSTFIELDS, $payload);
} else if ($method == "PUT") {
$this->setOption(CURLOPT_CUSTOMREQUEST, 'PUT');
$this->setOption(CURLOPT_POSTFIELDS, $payload);
}
return $this->exec($url);
}
/**
* @return string
*/
public function get($url) {
return $this->call('GET', $url);
}
/**
* @return string
*/
public function post($url, $data) {
return $this->call('POST', $url, $data);
}
/**
* @return string
*/
public function put($url, $data) {
return $this->call('PUT', $url, $data);
}
/**
* @return string
*/
public function delete($url) {
$this->setOption(CURLOPT_CUSTOMREQUEST, 'DELETE');
return $this->exec($url);
}
/**
* @return string
*/
private function exec($url) {
$this->setOption(CURLOPT_URL, $this->host . $url);
$this->setOption(CURLOPT_HTTPHEADER, $this->headers);
$curl = curl_init();
foreach ($this->options as $key => $value) {
curl_setopt($curl, $key, $value);
}
$response = curl_exec($curl);
if (!$response) {
$error = curl_error($curl);
curl_close($curl);
throw new PCurlException($error);
}
curl_close($curl);
return $response;
}
/**
* @return PCurl
*/
public function url($url) {
$this->url = $url;
return $this;
}
/**
* @return PCurl
*/
public function auth($user, $password) {
$this->setOption(CURLOPT_USERPWD, $user . ":" . $password);
return $this;
}
/**
* @return PCurl
*/
public function proxy($host, $port, $user = null, $password = null) {
$this->setOption(CURLOPT_PROXYTYPE, 'HTTP');
$this->setOption(CURLOPT_PROXY, $host);
$this->setOption(CURLOPT_PROXYPORT, $port);
if ($user && $password) {
$this->setOption(CURLOPT_PROXYUSERPWD, $user . ":" . $password);
}
return $this;
}
/**
* @return PCurl
*/
public function headers(array $headers) {
$this->headers = $headers;
return $this;
}
/**
* @return PCurl
*/
public function header($header) {
$this->headers[] = $header;
return $this;
}
/**
* @return PCurl
*/
public function contentType($contentType) {
$this->header('Content-Type: ' . $contentType);
return $this;
}
/**
* @return PCurl
*/
public function contentTypeJson() {
$this->contentType('application/json');
return $this;
}
/**
* @return PCurl
*/
public function ignoreSSLCertificate($bool = true) {
$this->setOption(CURLOPT_SSL_VERIFYPEER, !$bool);
return $this;
}
/**
* @return PCurl
*/
public function useSSLCertificate($certificatePath) {
$this->setOption(CURLOPT_SSL_VERIFYHOST, 2);
$this->setOption(CURLOPT_CAINFO, $certificatePath);
return $this;
}
/**
* @return PCurl
*/
public function setOption($optionKey, $optionValue) {
$this->options[$optionKey] = $optionValue;
return $this;
}
/**
* @return PCurl
*/
public function getOption($optionKey) {
return $this->options[$optionKey];
}
}
|