-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCurlTransporter.php
executable file
·171 lines (137 loc) · 4.84 KB
/
CurlTransporter.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
<?php
/**
** Handles HTTP transport with cURL
**
** @copyright Copyright 2010 Pascal Hurni
** @license http://www.opensource.org/licenses/mit-license.php Licensed under the MIT License
** @author Pascal Hurni <phi@ruby-reactive.org> http://github.com/phurni
**
*/
class CurlTransporter {
/// HTTP Basic Authentication username
var $username = null;
/// HTTP Basic Authentication password
var $password = null;
/// An error message if an error occurred.
/// Contains the message returned by curl_error()
var $error_message = false;
/// The error number if an error occurred.
/// -1 is cURL is missing otherwise the code returned by curl_errno()
var $error_code = false;
/// The response code returned from the server.
var $http_code = false;
/// The raw response headers sent from the server.
var $response_headers = '';
/// The response body sent from the server.
var $response_body = '';
// the cURL instance
protected $curl = null;
function __construct () {
}
/**
** Issue a GET request
**
** @param string $url The target URL.
** @param assoc $options Options map. Currently no options.
** @return string the response body
**/
function get($url, $options = array()) {
if (! $this->_setup($url, $options))
return false;
return $this->_execute();
}
/**
** Issue a POST request
**
** @param string $url The target URL.
** @param string $data The body data.
** @param assoc $options Options map. You may pass a specific content-type with 'format' => 'your_mime_type'
** @return string the response body
**/
function post($url, $data, $options = array()) {
if (! $this->_setup($url, $options))
return false;
curl_setopt ($this->curl, CURLOPT_POST, 1);
curl_setopt ($this->curl, CURLOPT_POSTFIELDS, $data);
if ($options['format'])
curl_setopt ($this->curl, CURLOPT_HTTPHEADER, array ("Content-Type: " . $options['format']));
return $this->_execute();
}
/**
** Issue a PUT request
**
** @param string $url The target URL.
** @param string $data The body data.
** @param assoc $options Options map. You may pass a specific content-type with 'format' => 'your_mime_type'
** @return string the response body
**/
function put($url, $data, $options = array()) {
if (! $this->_setup($url, $options))
return false;
curl_setopt ($this->curl, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt ($this->curl, CURLOPT_POSTFIELDS, $data);
if ($options['format'])
curl_setopt ($this->curl, CURLOPT_HTTPHEADER, array ("Content-Type: " . $options['format']));
return $this->_execute();
}
/**
** Issue a DELETE request
**
** @param string $url The target URL.
** @param assoc $options Options map. Currently no options.
** @return string the response body (has no meaning)
**/
function delete($url, $options = array()) {
if (! $this->_setup($url, $options))
return false;
curl_setopt ($this->curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
return $this->_execute();
}
protected function _setup($url, $options) {
if (!extension_loaded ('curl')) {
$this->error_message = 'cURL extension missing.';
$this->error_code = -1;
return false;
}
$this->curl = curl_init ();
curl_setopt ($this->curl, CURLOPT_URL, $url);
curl_setopt ($this->curl, CURLOPT_MAXREDIRS, 3);
curl_setopt ($this->curl, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt ($this->curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($this->curl, CURLOPT_VERBOSE, 0);
curl_setopt ($this->curl, CURLOPT_HEADER, 1);
curl_setopt ($this->curl, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt ($this->curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt ($this->curl, CURLOPT_SSL_VERIFYHOST, 0);
if (isset($options['headers']))
curl_setopt ($this->curl, CURLOPT_HTTPHEADER, $options['headers']);
if (isset($options['forward_cookies']))
curl_setopt ($this->curl, CURLOPT_COOKIE, $_SERVER['HTTP_COOKIE']);
/* HTTP Basic Authentication */
if ($this->username && $this->password) {
curl_setopt ($this->curl, CURLOPT_USERPWD, $this->username . ":" . $this->password);
}
return true;
}
protected function _execute() {
$this->error_code = 0;
$res = curl_exec ($this->curl);
$this->http_code = curl_getinfo ($this->curl, CURLINFO_HTTP_CODE);
if (! $res) {
$this->error_code = curl_errno ($this->curl);
$this->error_message = curl_error ($this->curl);
curl_close ($this->curl);
return false;
}
curl_close ($this->curl);
list ($headers, $res) = explode ("\r\n\r\n", $res, 2);
$this->response_headers = $headers;
$this->response_body = $res;
return $res;
}
function set_auth($username, $password) {
$this->username = $username;
$this->password = $password;
}
}
?>