3 /* Version 0.9, 6th April 2003 - Simon Willison ( http://simon.incutio.com/ )
4 Manual: http://scripts.incutio.com/httpclient/
14 var $cookies = array();
16 var $accept = 'text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,image/jpeg,image/gif,*/*';
17 var $accept_encoding = 'gzip';
18 var $accept_language = 'en-us';
19 var $user_agent = 'Incutio HttpClient v0.9';
23 var $persist_cookies = true; // If true, received cookies are placed in the $this->cookies array ready for the next request
24 // Note: This currently ignores the cookie path (and time) completely. Time is not important,
25 // but path could possibly lead to security problems.
26 var $persist_referers = true; // For each request, sends path of last request as referer
28 var $handle_redirects = true; // Auaomtically redirect if Location or URI header is found
29 var $max_redirects = 5;
30 var $headers_only = false; // If true, stops receiving once headers have been read.
31 // Basic authorization variables
36 var $headers = array();
40 var $redirect_count = 0;
41 var $cookie_host = '';
42 function HttpClient($host, $port=80) {
46 function get($path, $data = false) {
48 $this->method = 'GET';
50 $this->path .= '?'.$this->buildQueryString($data);
52 return $this->doRequest();
54 function post($path, $data) {
56 $this->method = 'POST';
57 $this->postdata = $this->buildQueryString($data);
58 return $this->doRequest();
60 function buildQueryString($data) {
62 if (is_array($data)) {
63 // Change data in to postable data
64 foreach ($data as $key => $val) {
66 foreach ($val as $val2) {
67 $querystring .= urlencode($key).'='.urlencode($val2).'&';
70 $querystring .= urlencode($key).'='.urlencode($val).'&';
73 $querystring = substr($querystring, 0, -1); // Eliminate unnecessary &
79 function doRequest() {
80 // Performs the actual HTTP request, returning true or false depending on outcome
81 if (!$fp = @fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout)) {
85 $this->errormsg = 'Socket creation failed (-3)';
87 $this->errormsg = 'DNS lookup failure (-4)';
89 $this->errormsg = 'Connection refused or timed out (-5)';
91 $this->errormsg = 'Connection failed ('.$errno.')';
92 $this->errormsg .= ' '.$errstr;
93 $this->debug($this->errormsg);
97 socket_set_timeout($fp, $this->timeout);
98 $request = $this->buildRequest();
99 $this->debug('Request', $request);
100 fwrite($fp, $request);
101 // Reset all the variables that should not persist between requests
102 $this->headers = array();
104 $this->errormsg = '';
105 // Set a couple of flags
108 // Now start reading back the response
110 $line = fgets($fp, 4096);
112 // Deal with first line of returned data
114 if (!preg_match('/HTTP\/(\\d\\.\\d)\\s*(\\d+)\\s*(.*)/', $line, $m)) {
115 $this->errormsg = "Status code line invalid: ".htmlentities($line);
116 $this->debug($this->errormsg);
119 $http_version = $m[1]; // not used
120 $this->status = $m[2];
121 $status_string = $m[3]; // not used
122 $this->debug(trim($line));
126 if (trim($line) == '') {
128 $this->debug('Received Headers', $this->headers);
129 if ($this->headers_only) {
130 break; // Skip the rest of the input
134 if (!preg_match('/([^:]+):\\s*(.*)/', $line, $m)) {
135 // Skip to the next header
138 $key = strtolower(trim($m[1]));
140 // Deal with the possibility of multiple headers of same name
141 if (isset($this->headers[$key])) {
142 if (is_array($this->headers[$key])) {
143 $this->headers[$key][] = $val;
145 $this->headers[$key] = array($this->headers[$key], $val);
148 $this->headers[$key] = $val;
152 // We're not in the headers, so append the line to the contents
153 $this->content .= $line;
156 // If data is compressed, uncompress it
157 if (isset($this->headers['content-encoding']) && $this->headers['content-encoding'] == 'gzip') {
158 $this->debug('Content is gzip encoded, unzipping it');
159 $this->content = substr($this->content, 10); // See http://www.php.net/manual/en/function.gzencode.php
160 $this->content = gzinflate($this->content);
162 // If $persist_cookies, deal with any cookies
163 if ($this->persist_cookies && isset($this->headers['set-cookie']) && $this->host == $this->cookie_host) {
164 $cookies = $this->headers['set-cookie'];
165 if (!is_array($cookies)) {
166 $cookies = array($cookies);
168 foreach ($cookies as $cookie) {
169 if (preg_match('/([^=]+)=([^;]+);/', $cookie, $m)) {
170 $this->cookies[$m[1]] = $m[2];
173 // Record domain of cookies for security reasons
174 $this->cookie_host = $this->host;
176 // If $persist_referers, set the referer ready for the next request
177 if ($this->persist_referers) {
178 $this->debug('Persisting referer: '.$this->getRequestURL());
179 $this->referer = $this->getRequestURL();
181 // Finally, if handle_redirects and a redirect is sent, do that
182 if ($this->handle_redirects) {
183 if (++$this->redirect_count >= $this->max_redirects) {
184 $this->errormsg = 'Number of redirects exceeded maximum ('.$this->max_redirects.')';
185 $this->debug($this->errormsg);
186 $this->redirect_count = 0;
189 $location = isset($this->headers['location']) ? $this->headers['location'] : '';
190 $uri = isset($this->headers['uri']) ? $this->headers['uri'] : '';
191 if ($location || $uri) {
192 $url = parse_url($location.$uri);
193 // This will FAIL if redirect is to a different site
194 return $this->get($url['path']);
199 function buildRequest() {
201 $headers[] = "{$this->method} {$this->path} HTTP/1.0"; // Using 1.1 leads to all manner of problems, such as "chunked" encoding
202 $headers[] = "Host: {$this->host}";
203 $headers[] = "User-Agent: {$this->user_agent}";
204 $headers[] = "Accept: {$this->accept}";
205 if ($this->use_gzip) {
206 $headers[] = "Accept-encoding: {$this->accept_encoding}";
208 $headers[] = "Accept-language: {$this->accept_language}";
209 if ($this->referer) {
210 $headers[] = "Referer: {$this->referer}";
213 if ($this->cookies) {
214 $cookie = 'Cookie: ';
215 foreach ($this->cookies as $key => $value) {
216 $cookie .= "$key=$value; ";
218 $headers[] = $cookie;
220 // Basic authentication
221 if ($this->username && $this->password) {
222 $headers[] = 'Authorization: BASIC '.base64_encode($this->username.':'.$this->password);
224 // If this is a POST, set the content type and length
225 if ($this->postdata) {
226 $headers[] = 'Content-Type: application/x-www-form-urlencoded';
227 $headers[] = 'Content-Length: '.strlen($this->postdata);
229 $request = implode("\r\n", $headers)."\r\n\r\n".$this->postdata;
232 function getStatus() {
233 return $this->status;
235 function getContent() {
236 return $this->content;
238 function getHeaders() {
239 return $this->headers;
241 function getHeader($header) {
242 $header = strtolower($header);
243 if (isset($this->headers[$header])) {
244 return $this->headers[$header];
249 function getError() {
250 return $this->errormsg;
252 function getCookies() {
253 return $this->cookies;
255 function getRequestURL() {
256 $url = 'http://'.$this->host;
257 if ($this->port != 80) {
258 $url .= ':'.$this->port;
264 function setUserAgent($string) {
265 $this->user_agent = $string;
267 function setAuthorization($username, $password) {
268 $this->username = $username;
269 $this->password = $password;
271 function setCookies($array) {
272 $this->cookies = $array;
274 // Option setting methods
275 function useGzip($boolean) {
276 $this->use_gzip = $boolean;
278 function setPersistCookies($boolean) {
279 $this->persist_cookies = $boolean;
281 function setPersistReferers($boolean) {
282 $this->persist_referers = $boolean;
284 function setHandleRedirects($boolean) {
285 $this->handle_redirects = $boolean;
287 function setMaxRedirects($num) {
288 $this->max_redirects = $num;
290 function setHeadersOnly($boolean) {
291 $this->headers_only = $boolean;
293 function setDebug($boolean) {
294 $this->debug = $boolean;
296 // "Quick" static methods
297 function quickGet($url) {
298 $bits = parse_url($url);
299 $host = $bits['host'];
300 $port = isset($bits['port']) ? $bits['port'] : 80;
301 $path = isset($bits['path']) ? $bits['path'] : '/';
302 if (isset($bits['query'])) {
303 $path .= '?'.$bits['query'];
305 $client = new HttpClient($host, $port);
306 if (!$client->get($path)) {
309 return $client->getContent();
312 function quickPost($url, $data) {
313 $bits = parse_url($url);
314 $host = $bits['host'];
315 $port = isset($bits['port']) ? $bits['port'] : 80;
316 $path = isset($bits['path']) ? $bits['path'] : '/';
317 $client = new HttpClient($host, $port);
318 if (!$client->post($path, $data)) {
321 return $client->getContent();
324 function debug($msg, $object = false) {
326 print '<div style="border: 1px solid red; padding: 0.5em; margin: 0.5em;"><strong>HttpClient Debug:</strong> '.$msg;
330 $content = htmlentities(ob_get_contents());
332 print '<pre>'.$content.'</pre>';