| 1 |
<?php |
| 2 |
class usces_httpRequest { |
| 3 |
|
| 4 |
var $host; |
| 5 |
var $path; |
| 6 |
var $data; |
| 7 |
var $method; |
| 8 |
var $port; |
| 9 |
var $rawhost; |
| 10 |
|
| 11 |
var $header; |
| 12 |
var $content; |
| 13 |
var $parsedHeader; |
| 14 |
|
| 15 |
function usces_httpRequest($host, $path, $method = 'POST', $ssl = false, $port = 0) { |
| 16 |
$this->host = $host; |
| 17 |
$this->rawhost = $ssl ? ("ssl://".$host) : $host; |
| 18 |
$this->path = $path; |
| 19 |
$this->method = strtoupper($method); |
| 20 |
if ($port) { |
| 21 |
$this->port = $port; |
| 22 |
} else { |
| 23 |
if (!$ssl) $this->port = 80; else $this->port = 443; |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
function connect( $data = ''){ |
| 28 |
$fp = fsockopen($this->rawhost, $this->port); |
| 29 |
if (!$fp) return false; |
| 30 |
|
| 31 |
fputs($fp, "$this->method $this->path HTTP/1.1\r\n"); |
| 32 |
fputs($fp, "Host: $this->host\r\n"); |
| 33 |
//fputs($fp, "Content-type: $contenttype\r\n"); |
| 34 |
fputs($fp, "Content-length: ".strlen($data)."\r\n"); |
| 35 |
fputs($fp, "Connection: close\r\n"); |
| 36 |
fputs($fp, "\r\n"); |
| 37 |
fputs($fp, $data); |
| 38 |
|
| 39 |
$responseHeader = ''; |
| 40 |
$responseContent = ''; |
| 41 |
|
| 42 |
do{ |
| 43 |
$responseHeader.= fread($fp, 1); |
| 44 |
}while (!preg_match('/\\r\\n\\r\\n$/', $responseHeader)); |
| 45 |
|
| 46 |
if (!strstr($responseHeader, "Transfer-Encoding: chunked")){ |
| 47 |
while (!feof($fp)){ |
| 48 |
$responseContent.= fgets($fp, 128); |
| 49 |
} |
| 50 |
}else{ |
| 51 |
while ($chunk_length = hexdec(fgets($fp))){ |
| 52 |
$responseContentChunk = ''; |
| 53 |
|
| 54 |
$read_length = 0; |
| 55 |
|
| 56 |
while ($read_length < $chunk_length){ |
| 57 |
$responseContentChunk .= fread($fp, $chunk_length - $read_length); |
| 58 |
$read_length = strlen($responseContentChunk); |
| 59 |
} |
| 60 |
|
| 61 |
$responseContent.= $responseContentChunk; |
| 62 |
|
| 63 |
fgets($fp); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
$this->header = chop($responseHeader); |
| 68 |
$this->content = $responseContent; |
| 69 |
$this->parsedHeader = $this->headerParse(); |
| 70 |
|
| 71 |
$code = intval(trim(substr($this->parsedHeader[0], 9))); |
| 72 |
|
| 73 |
return $code; |
| 74 |
} |
| 75 |
|
| 76 |
function headerParse(){ |
| 77 |
$h = $this->header; |
| 78 |
$a=explode("\r\n", $h); |
| 79 |
$out = array(); |
| 80 |
foreach ($a as $v){ |
| 81 |
$k = strpos($v, ':'); |
| 82 |
if ($k) { |
| 83 |
$key = trim(substr($v,0,$k)); |
| 84 |
$value = trim(substr($v,$k+1)); |
| 85 |
if (!$key) continue; |
| 86 |
$out[$key] = $value; |
| 87 |
}else{ |
| 88 |
if ($v) $out[] = $v; |
| 89 |
} |
| 90 |
} |
| 91 |
return $out; |
| 92 |
} |
| 93 |
|
| 94 |
function getContent() { |
| 95 |
return $this->content; |
| 96 |
} |
| 97 |
|
| 98 |
function getHeader() { |
| 99 |
return $this->parsedHeader; |
| 100 |
} |
| 101 |
} |
| 102 |
?> |
| 103 |
|