⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 response.php

📁 Bug tracker, and reporter.
💻 PHP
📖 第 1 页 / 共 2 页
字号:
     * Get the HTTP response status code     *     * @return int     */    public function getStatus()    {        return $this->code;    }    /**     * Return a message describing the HTTP response code     * (Eg. "OK", "Not Found", "Moved Permanently")     *     * @return string     */    public function getMessage()    {        return $this->message;    }    /**     * Get the response headers     *     * @return array     */    public function getHeaders()    {        return $this->headers;    }    /**     * Get a specific header as string, or null if it is not set     *     * @param string$header     * @return string|array|null     */    public function getHeader($header)    {        $header = ucwords(strtolower($header));        if (! is_string($header) || ! isset($this->headers[$header])) return null;        return $this->headers[$header];    }    /**     * Get all headers as string     *     * @param boolean $status_line Whether to return the first status line (IE "HTTP 200 OK")     * @param string $br Line breaks (eg. "\n", "\r\n", "<br />")     * @return string     */    public function getHeadersAsString($status_line = true, $br = "\n")    {        $str = '';        if ($status_line) {            $str = "HTTP/{$this->version} {$this->code} {$this->message}{$br}";        }        // Iterate over the headers and stringify them        foreach ($this->headers as $name => $value)        {            if (is_string($value))                $str .= "{$name}: {$value}{$br}";            elseif (is_array($value)) {                foreach ($value as $subval) {                    $str .= "{$name}: {$subval}{$br}";                }            }        }        return $str;    }    /**     * Get the entire response as string     *     * @param string $br Line breaks (eg. "\n", "\r\n", "<br />")     * @return string     */    public function asString($br = "\n")    {        return $this->getHeadersAsString(true, $br) . $br . $this->getBody();    }    /**     * A convenience function that returns a text representation of     * HTTP response codes. Returns 'Unknown' for unknown codes.     * Returns array of all codes, if $code is not specified.     *     * Conforms to HTTP/1.1 as defined in RFC 2616 (except for 'Unknown')     * See http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10 for reference     *     * @param int $code HTTP response code     * @param boolean $http11 Use HTTP version 1.1     * @return string     */    public static function responseCodeAsText($code = null, $http11 = true)    {        $messages = self::$messages;        if (! $http11) $messages[302] = 'Moved Temporarily';        if ($code === null) {            return $messages;        } elseif (isset($messages[$code])) {            return $messages[$code];        } else {            return 'Unknown';        }    }    /**     * Extract the response code from a response string     *     * @param string $response_str     * @return int     */    public static function extractCode($response_str)    {        preg_match("|^HTTP/[\d\.x]+ (\d+)|", $response_str, $m);        if (isset($m[1])) {            return (int) $m[1];        } else {            return false;        }    }    /**     * Extract the HTTP message from a response     *     * @param string $response_str     * @return string     */    public static function extractMessage($response_str)    {        preg_match("|^HTTP/[\d\.x]+ \d+ ([^\r\n]+)|", $response_str, $m);        if (isset($m[1])) {            return $m[1];        } else {            return false;        }    }    /**     * Extract the HTTP version from a response     *     * @param string $response_str     * @return string     */    public static function extractVersion($response_str)    {        preg_match("|^HTTP/([\d\.x]+) \d+|", $response_str, $m);        if (isset($m[1])) {            return $m[1];        } else {            return false;        }    }    /**     * Extract the headers from a response string     *     * @param string $response_str     * @return array     */    public static function extractHeaders($response_str)    {        $headers = array();                // First, split body and headers        $parts = preg_split('|(?:\r?\n){2}|m', $response_str, 2);        if (! $parts[0]) return $headers;                // Split headers part to lines        $lines = explode("\n", $parts[0]);        unset($parts);        $last_header = null;        foreach($lines as $line) {            $line = trim($line, "\r\n");            if ($line == "") break;            if (preg_match("|^([\w-]+):\s+(.+)|", $line, $m)) {                unset($last_header);                $h_name = strtolower($m[1]);                $h_value = $m[2];                if (isset($headers[$h_name])) {                    if (! is_array($headers[$h_name])) {                        $headers[$h_name] = array($headers[$h_name]);                    }                    $headers[$h_name][] = $h_value;                } else {                    $headers[$h_name] = $h_value;                }                $last_header = $h_name;            } elseif (preg_match("|^\s+(.+)$|", $line, $m) && $last_header !== null) {                if (is_array($headers[$last_header])) {                    end($headers[$last_header]);                    $last_header_key = key($headers[$last_header]);                    $headers[$last_header][$last_header_key] .= $m[1];                } else {                    $headers[$last_header] .= $m[1];                }            }        }        return $headers;    }    /**     * Extract the body from a response string     *     * @param string $response_str     * @return string     */    public static function extractBody($response_str)    {        $parts = preg_split('|(?:\r?\n){2}|m', $response_str, 2);        if (isset($parts[1])) {         	return $parts[1];        } else {        	return '';        }    }    /**     * Decode a "chunked" transfer-encoded body and return the decoded text     *     * @param string $body     * @return string     */    public static function decodeChunkedBody($body)    {        $decBody = '';                while (trim($body)) {            if (! preg_match("/^([\da-fA-F]+)[^\r\n]*\r\n/sm", $body, $m)) {                require_once 'Zend/Http/Exception.php';                throw new Zend_Http_Exception("Error parsing body - doesn't seem to be a chunked message");            }            $length = hexdec(trim($m[1]));            $cut = strlen($m[0]);            $decBody .= substr($body, $cut, $length);            $body = substr($body, $cut + $length + 2);        }        return $decBody;    }    /**     * Decode a gzip encoded message (when Content-encoding = gzip)     *     * Currently requires PHP with zlib support     *     * @param string $body     * @return string     */    public static function decodeGzip($body)    {        if (! function_exists('gzinflate')) {            require_once 'Zend/Http/Exception.php';            throw new Zend_Http_Exception('Unable to decode gzipped response ' .                 'body: perhaps the zlib extension is not loaded?');         }        return gzinflate(substr($body, 10));    }    /**     * Decode a zlib deflated message (when Content-encoding = deflate)     *     * Currently requires PHP with zlib support     *     * @param string $body     * @return string     */    public static function decodeDeflate($body)    {        if (! function_exists('gzuncompress')) {            require_once 'Zend/Http/Exception.php';            throw new Zend_Http_Exception('Unable to decode deflated response ' .                 'body: perhaps the zlib extension is not loaded?');         }    	return gzuncompress($body);    }    /**     * Create a new Zend_Http_Response object from a string     *     * @param string $response_str     * @return Zend_Http_Response     */    public static function fromString($response_str)    {        $code    = self::extractCode($response_str);        $headers = self::extractHeaders($response_str);        $body    = self::extractBody($response_str);        $version = self::extractVersion($response_str);        $message = self::extractMessage($response_str);        return new Zend_Http_Response($code, $headers, $body, $version, $message);    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -