[ Index ] |
|
Code source de Dotclear 1.2.5 |
1 <?php 2 3 /* Version 0.9, 6th April 2003 - Simon Willison ( http://simon.incutio.com/ ) 4 Manual: http://scripts.incutio.com/httpclient/ 5 */ 6 7 class HttpClient 8 { 9 // Request vars 10 var $host; 11 var $port; 12 var $path; 13 var $method; 14 var $postdata = ''; 15 var $cookies = array(); 16 var $referer; 17 var $accept = 'text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,image/jpeg,image/gif,*/*'; 18 var $accept_encoding = 'gzip'; 19 var $accept_language = 'en-us'; 20 var $user_agent = 'Incutio HttpClient v0.9'; 21 // Options 22 var $timeout = 20; 23 var $use_gzip = false; 24 var $persist_cookies = true; // If true, received cookies are placed in the $this->cookies array ready for the next request 25 // Note: This currently ignores the cookie path (and time) completely. Time is not important, 26 // but path could possibly lead to security problems. 27 var $persist_referers = true; // For each request, sends path of last request as referer 28 var $debug = false; 29 var $handle_redirects = true; // Auaomtically redirect if Location or URI header is found 30 var $max_redirects = 5; 31 var $headers_only = false; // If true, stops receiving once headers have been read. 32 // Basic authorization variables 33 var $username; 34 var $password; 35 // Response vars 36 var $status; 37 var $headers = array(); 38 var $content = ''; 39 var $errormsg; 40 // Tracker variables 41 var $redirect_count = 0; 42 var $cookie_host = ''; 43 44 function HttpClient($host, $port=80) 45 { 46 $this->host = $host; 47 $this->port = $port; 48 } 49 50 function get($path, $data = false) 51 { 52 $this->path = $path; 53 $this->method = 'GET'; 54 55 if ($data) { 56 $this->path .= '?'.$this->buildQueryString($data); 57 } 58 59 return $this->doRequest(); 60 } 61 62 function post($path, $data) 63 { 64 $this->path = $path; 65 $this->method = 'POST'; 66 $this->postdata = $this->buildQueryString($data); 67 return $this->doRequest(); 68 } 69 70 function buildQueryString($data) { 71 $querystring = ''; 72 73 if (is_array($data)) { 74 // Change data in to postable data 75 foreach ($data as $key => $val) { 76 if (is_array($val)) { 77 foreach ($val as $val2) { 78 $querystring .= urlencode($key).'='.urlencode($val2).'&'; 79 } 80 } else { 81 $querystring .= urlencode($key).'='.urlencode($val).'&'; 82 } 83 } 84 $querystring = substr($querystring, 0, -1); // Eliminate unnecessary & 85 } else { 86 $querystring = $data; 87 } 88 89 return $querystring; 90 } 91 92 function doRequest() 93 { 94 // Performs the actual HTTP request, returning true or false depending on outcome 95 if (!$fp = @fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout)) { 96 // Set error message 97 switch($errno) { 98 case -3: 99 $this->errormsg = 'Socket creation failed (-3)'; 100 case -4: 101 $this->errormsg = 'DNS lookup failure (-4)'; 102 case -5: 103 $this->errormsg = 'Connection refused or timed out (-5)'; 104 default: 105 $this->errormsg = 'Connection failed ('.$errno.')'; 106 $this->errormsg .= ' '.$errstr; 107 $this->debug($this->errormsg); 108 } 109 return false; 110 } 111 112 socket_set_timeout($fp, $this->timeout); 113 $request = $this->buildRequest(); 114 $this->debug('Request', $request); 115 fwrite($fp, $request); 116 117 // Reset all the variables that should not persist between requests 118 $this->headers = array(); 119 $this->content = ''; 120 $this->errormsg = ''; 121 122 // Set a couple of flags 123 $inHeaders = true; 124 $atStart = true; 125 126 // Now start reading back the response 127 while (!feof($fp)) 128 { 129 $line = fgets($fp, 4096); 130 131 if ($atStart) 132 { 133 // Deal with first line of returned data 134 $atStart = false; 135 if (!preg_match('/HTTP\/(\\d\\.\\d)\\s*(\\d+)\\s*(.*)/', $line, $m)) { 136 $this->errormsg = "Status code line invalid: ".htmlentities($line); 137 $this->debug($this->errormsg); 138 return false; 139 } 140 $http_version = $m[1]; // not used 141 $this->status = $m[2]; 142 $status_string = $m[3]; // not used 143 $this->debug(trim($line)); 144 continue; 145 } 146 147 if ($inHeaders) { 148 if (trim($line) == '') { 149 $inHeaders = false; 150 $this->debug('Received Headers', $this->headers); 151 if ($this->headers_only) { 152 break; // Skip the rest of the input 153 } 154 continue; 155 } 156 157 if (!preg_match('/([^:]+):\\s*(.*)/', $line, $m)) { 158 // Skip to the next header 159 continue; 160 } 161 $key = strtolower(trim($m[1])); 162 $val = trim($m[2]); 163 // Deal with the possibility of multiple headers of same name 164 if (isset($this->headers[$key])) { 165 if (is_array($this->headers[$key])) { 166 $this->headers[$key][] = $val; 167 } else { 168 $this->headers[$key] = array($this->headers[$key], $val); 169 } 170 } else { 171 $this->headers[$key] = $val; 172 } 173 continue; 174 } 175 176 // We're not in the headers, so append the line to the contents 177 $this->content .= $line; 178 } 179 180 fclose($fp); 181 182 // If data is compressed, uncompress it 183 if (isset($this->headers['content-encoding']) && $this->headers['content-encoding'] == 'gzip') 184 { 185 $this->debug('Content is gzip encoded, unzipping it'); 186 $this->content = substr($this->content, 10); // See http://www.php.net/manual/en/function.gzencode.php 187 $this->content = gzinflate($this->content); 188 } 189 190 // If $persist_cookies, deal with any cookies 191 if ($this->persist_cookies && isset($this->headers['set-cookie']) && $this->host == $this->cookie_host) 192 { 193 $cookies = $this->headers['set-cookie']; 194 if (!is_array($cookies)) { 195 $cookies = array($cookies); 196 } 197 198 foreach ($cookies as $cookie) 199 { 200 if (preg_match('/([^=]+)=([^;]+);/', $cookie, $m)) { 201 $this->cookies[$m[1]] = $m[2]; 202 } 203 } 204 205 // Record domain of cookies for security reasons 206 $this->cookie_host = $this->host; 207 } 208 209 // If $persist_referers, set the referer ready for the next request 210 if ($this->persist_referers) { 211 $this->debug('Persisting referer: '.$this->getRequestURL()); 212 $this->referer = $this->getRequestURL(); 213 } 214 215 // Finally, if handle_redirects and a redirect is sent, do that 216 if ($this->handle_redirects) 217 { 218 if (++$this->redirect_count >= $this->max_redirects) 219 { 220 $this->errormsg = 'Number of redirects exceeded maximum ('.$this->max_redirects.')'; 221 $this->debug($this->errormsg); 222 $this->redirect_count = 0; 223 return false; 224 } 225 226 $location = isset($this->headers['location']) ? $this->headers['location'] : ''; 227 $uri = isset($this->headers['uri']) ? $this->headers['uri'] : ''; 228 if ($location || $uri) { 229 $url = parse_url($location.$uri); 230 // This will FAIL if redirect is to a different site 231 return $this->get($url['path']); 232 } 233 } 234 return true; 235 } 236 237 function buildRequest() 238 { 239 $headers = array(); 240 $headers[] = "{$this->method} {$this->path} HTTP/1.0"; // Using 1.1 leads to all manner of problems, such as "chunked" encoding 241 $headers[] = "Host: {$this->host}"; 242 $headers[] = "User-Agent: {$this->user_agent}"; 243 $headers[] = "Accept: {$this->accept}"; 244 245 if ($this->use_gzip) { 246 $headers[] = "Accept-encoding: {$this->accept_encoding}"; 247 } 248 $headers[] = "Accept-language: {$this->accept_language}"; 249 250 if ($this->referer) { 251 $headers[] = "Referer: {$this->referer}"; 252 } 253 254 // Cookies 255 if ($this->cookies) { 256 $cookie = 'Cookie: '; 257 foreach ($this->cookies as $key => $value) { 258 $cookie .= "$key=$value; "; 259 } 260 $headers[] = $cookie; 261 } 262 263 // Basic authentication 264 if ($this->username && $this->password) { 265 $headers[] = 'Authorization: BASIC '.base64_encode($this->username.':'.$this->password); 266 } 267 268 // If this is a POST, set the content type and length 269 if ($this->postdata) { 270 $headers[] = 'Content-Type: application/x-www-form-urlencoded'; 271 $headers[] = 'Content-Length: '.strlen($this->postdata); 272 } 273 274 $request = implode("\r\n", $headers)."\r\n\r\n".$this->postdata; 275 return $request; 276 } 277 278 function getStatus() 279 { 280 return $this->status; 281 } 282 283 function getContent() 284 { 285 return $this->content; 286 } 287 288 function getHeaders(){ 289 return $this->headers; 290 } 291 292 function getHeader($header) 293 { 294 $header = strtolower($header); 295 if (isset($this->headers[$header])) { 296 return $this->headers[$header]; 297 } else { 298 return false; 299 } 300 } 301 302 function getError() 303 { 304 return $this->errormsg; 305 } 306 307 function getCookies() 308 { 309 return $this->cookies; 310 } 311 312 function getRequestURL() 313 { 314 $url = 'http://'.$this->host; 315 if ($this->port != 80) { 316 $url .= ':'.$this->port; 317 } 318 $url .= $this->path; 319 return $url; 320 } 321 322 // Setter methods 323 function setUserAgent($string) 324 { 325 $this->user_agent = $string; 326 } 327 328 function setAuthorization($username, $password) 329 { 330 $this->username = $username; 331 $this->password = $password; 332 } 333 334 function setCookies($array) 335 { 336 $this->cookies = $array; 337 } 338 339 // Option setting methods 340 function useGzip($boolean) 341 { 342 $this->use_gzip = $boolean; 343 } 344 345 function setPersistCookies($boolean) 346 { 347 $this->persist_cookies = $boolean; 348 } 349 350 function setPersistReferers($boolean) 351 { 352 $this->persist_referers = $boolean; 353 } 354 355 function setHandleRedirects($boolean) 356 { 357 $this->handle_redirects = $boolean; 358 } 359 360 function setMaxRedirects($num) 361 { 362 $this->max_redirects = $num; 363 } 364 365 function setHeadersOnly($boolean) 366 { 367 $this->headers_only = $boolean; 368 } 369 370 function setDebug($boolean) 371 { 372 $this->debug = $boolean; 373 } 374 375 // "Quick" static methods 376 function quickGet($url) 377 { 378 $bits = parse_url($url); 379 $host = $bits['host']; 380 $port = isset($bits['port']) ? $bits['port'] : 80; 381 $path = isset($bits['path']) ? $bits['path'] : '/'; 382 383 if (isset($bits['query'])) { 384 $path .= '?'.$bits['query']; 385 } 386 $client = new HttpClient($host, $port); 387 388 if (!$client->get($path)) { 389 return false; 390 } else { 391 return $client->getContent(); 392 } 393 } 394 395 function quickPost($url, $data) 396 { 397 $bits = parse_url($url); 398 $host = $bits['host']; 399 $port = isset($bits['port']) ? $bits['port'] : 80; 400 $path = isset($bits['path']) ? $bits['path'] : '/'; 401 $client = new HttpClient($host, $port); 402 403 if (!$client->post($path, $data)) { 404 return false; 405 } else { 406 return $client->getContent(); 407 } 408 } 409 410 function debug($msg, $object = false) 411 { 412 if ($this->debug) { 413 print '<div style="border: 1px solid red; padding: 0.5em; margin: 0.5em;"><strong>HttpClient Debug:</strong> '.$msg; 414 if ($object) { 415 ob_start(); 416 print_r($object); 417 $content = htmlentities(ob_get_contents()); 418 ob_end_clean(); 419 print '<pre>'.$content.'</pre>'; 420 } 421 print '</div>'; 422 } 423 } 424 } 425 426 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Fri Feb 23 21:40:15 2007 | par Balluche grâce à PHPXref 0.7 |