📄 php_http_client_generic.php
字号:
if ($this->responseHeadersAsObject) { $this->response->setUnformattedHeader($data); } else { $this->response->headers .= $data; } } break; case HTTP_READ_STATE_BODY: $this->response->message .= $data; break; } } $this->normalizeResponseIfChunked(); $headerString = is_object($this->response->headers) ? $this->response->headers->toString() : $this->response->headers; $this->fireEvent('onResponseHeaders', $headerString); $this->fireEvent('onResponseBody', $this->response->message); $this->fireEvent('onResponse', $this->response->headers . $this->response->message); return $this->response; } else { HTTPExceptions::raiseException(HTTP_SOCKET_CONNECTION_ERR, ('HTTP Transport Error - Unable to establish connection to host ' . $conn->host)); } } //send /** * Determines if response data is transfer encoding chunked, then decodes */ function normalizeResponseIfChunked() { if (($this->protocolVersion = '1.1') && (!$this->response->isResponseChunkDecoded)) { if ($this->responseHeadersAsObject) { if ($this->response->headers->headerExists('Transfer-Encoding') && ($this->response->headers->getHeader('Transfer-Encoding') == 'chunked')) { $this->response->message = $this->decodeChunkedData($this->response->getResponse()); $this->response->isResponseChunkDecoded = true; } } else { if ((strpos($this->response->headers, 'Transfer-Encoding') !== false) && (strpos($this->response->headers, 'chunked') !== false)){ $this->response->message = $this->decodeChunkedData($this->response->getResponse()); $this->response->isResponseChunkDecoded = true; } } } } //normalizeResponseIfChunked /** * Decodes data if transfer encoding chunked * @param string The encoded data * @return string The decoded data */ function decodeChunkedData($data) { $chunkStart = $chunkEnd = strpos($data, CRLF) + 2; $chunkLengthInHex = substr($data, 0, $chunkEnd); $chunkLength = hexdec(trim($chunkLengthInHex)); $decodedData = ''; while ($chunkLength > 0) { $chunkEnd = strpos($data, CRLF, ($chunkStart + $chunkLength)); if (!$chunkEnd) { //if the trailing CRLF is missing, return all the remaining data $decodedData .= substr($data, $chunkStart); break; } $decodedData .= substr($data, $chunkStart, ($chunkEnd - $chunkStart)); $chunkStart = $chunkEnd + 2; $chunkEnd = strpos($data, CRLF, $chunkStart) + 2; if (!$chunkEnd) break; $chunkLengthInHex = substr($data, $chunkStart, ($chunkEnd - $chunkStart)); $chunkLength = hexdec(trim($chunkLengthInHex)); $chunkStart = $chunkEnd; } return $decodedData; } //decodeChunkedData} //php_http_client_generic/*** An HTTP Connection class** @package php-http-tools* @author John Heinstein <johnkarl@nbnet.nb.ca>*/class php_http_connection { /** @var object A reference to the current connection */ var $connection = null; /** @var string The host of the connection */ var $host; /** @var string The path of the connection */ var $path; /** @var int The port of the connection */ var $port; /** @var int The timeout value for the connection */ var $timeout; /** @var int The error number of the connection */ var $errorNumber = 0; /** @var string The error string of the connection */ var $errorString = ''; /** * HTTP Connection constructor * @param string The connection host name, with or without its protocol prefix * @param string The connection path, not including the host name * @param int The port to establish the client connection on * @param int The timeout value for the client connection */ function php_http_connection($host = '', $path = '/', $port = 80, $timeout = 0) { $this->host = $this->formatHost($host); $this->path = $this->formatPath($path); $this->port = $port; $this->timeout = $timeout; } //php_http_connection /** * Formats a host string by stripping off the http:// prefix * @param string The host name * @return string The formatted host name */ function formatHost($hostString) { $hasProtocol = (substr(strtoupper($hostString), 0, 7) == 'HTTP://'); if ($hasProtocol) { $hostString = substr($hostString, 7); } return $hostString; } //formatHost /** * Formats a path string * @param string The path * @return string The formatted path */ function formatPath($pathString) { if (($pathString == '') || ($pathString == null)) { $pathString = '/'; } return $pathString; } //formatPath /** * Establishes a socket connection * @return boolean True if the connection was successful */ function connect() { if ($this->timeout == 0) { $this->connection = @fsockopen($this->host, $this->port, $errorNumber, $errorString); } else { $this->connection = @fsockopen($this->host, $this->port, $errorNumber, $errorString, $this->timeout); } $this->errorNumber = $errorNumber; $this->errorString = $errorString; return is_resource($this->connection); } //connect /** * Determines whether the connection is still open * @return boolean True if the connection is still open */ function isOpen() { return (is_resource($this->connection) && (!feof($this->connection))); } //isOpen /** * Disconnects the current connection * @return boolean True if the connection has been disconnected */ function disconnect() { fclose($this->connection); $this->connection = null; return true; } //disconnect} //php_http_connection/*** An HTTP Headers class** @package php-http-tools* @author John Heinstein <johnkarl@nbnet.nb.ca>*/class php_http_headers { /** @var object An array of headers */ var $headers; /** * HTTP Headers constructor */ function php_http_headers() { $this->headers = array(); } //php_http_headers /** * Returns the specified header value * @param string The header name * @return mixed The header value, or an array of header values */ function &getHeader($name) { if ($this->headerExists($name)) { return $this->headers[$name]; } return false; } //getHeader /** * Sets the named header to the specified value * @param string The header name * @param string The header value * @param boolean True if multiple headers with the same name are allowed */ function setHeader($name, $value, $allowMultipleHeaders = false) { if ($allowMultipleHeaders) { if (isset($this->headers[$name])) { if (is_array($this->headers[$name])) { $this->headers[$name][count($this->headers)] = $value; } else { $tempVal = $this->headers[$name]; $this->headers[$name] = array($tempVal, $value); } } else { $this->headers[$name] = array(); $this->headers[$name][0] = $value; } } else { $this->headers[$name] = $value; } } //setHeader /** * Determines whether the specified header exists * @param string The header name * @return boolean True if the specified header exists */ function headerExists($name) { return isset($this->headers[$name]); } //headerExists /** * Removes the specified header * @param string The header name * @return boolean True if the specified header has been removed */ function removeHeader($name) { if ($this->headerExists($name)) { unset($this->headers[$name]); return true; } return false; } //removeHeader /** * Returns a reference to the headers array * @return array The headers array */ function getHeaders() { return $this->headers; } //getHeaders /** * Returns a list of existing headers names * @return array A list of existing header names */ function getHeaderList() { return array_keys($this->headers); } //getHeaderList /** * Returns a string representation of the headers array * @return string A string representation of the headers array */ function toString() { $retString = ''; foreach ($this->headers as $key => $value) { if (is_array($value)) { foreach ($value as $key2 => $value2) { $retString .= $key . ': ' . $value2 . CRLF; } } else { $retString .= $key . ': ' . $value . CRLF; } } return $retString; } //toString} //php_http_headers/*** An HTTP Response class** @package php-http-tools* @author John Heinstein <johnkarl@nbnet.nb.ca>*/class php_http_response { /** @var string Response number */ var $statusLine = ''; /** @var mixed Response headers, either as object or string */ var $headers = ''; /** @var string Response message */ var $message = ''; /** @var boolean True if the chunked transfer-encoding of the response has been decoded */ var $isResponseChunkDecoded = false; /** * Returns a reference to the headers array * @return array The headers array */ function getResponse() { return $this->message; } //getResponse /** * Returns the response status line * @return string The response status line */ function getStatusLine() { return $this->statusLine; } //getStatusLine /** * Returns the response status code * @return int The response status code */ function getStatusCode() { $statusArray = split(' ', $this->statusLine); if (count($statusArray > 1)) { return intval($statusArray[1], 10); } return -1; } //getStatusCode /** * Returns a reference to the headers array * @return array The headers array */ function &getHeaders() { return $this->headers; } //getHeaders /** * Converts a header string into a key value pair and sets header */ function setUnformattedHeader($headerString) { $colonIndex = strpos($headerString, ':'); if ($colonIndex !== false) { $key = trim(substr($headerString, 0, $colonIndex)); $value = trim(substr($headerString, ($colonIndex + 1))); $this->headers->setHeader($key, $value, true); } } //setUnformattedHeader} //php_http_response?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -