模块

abstract HTTP

Contains the most low-level helpers methods in BootPHP:

  • Environment initialization
  • Locating files within the cascading filesystem
  • Auto-loading && transparent extension of classes
  • Variable and path debugging
package
BootPHP
category
HTTP
author
Tinsh
copyright
© 2005-2016 Kilofox Studio

该类在 SYSPATH/classes/http.php 第 18 行声明。

属性

public static The $protocol

default protocol to use if it cannot be detected

string(8) "HTTP/1.1"

方法

public static parse_header_string( string $header_string ) (在 HTTP 中定义)

Parses a HTTP header string into an associative array

参数

  • string $header_string required - Header string to parse

返回值

  • HTTP_Header

源代码

public static function parse_header_string($header_string)
{
	// If the PECL HTTP extension is loaded
	if (extension_loaded('http'))
	{
		// Use the fast method to parse header string
		return new HTTP_Header(http_parse_headers($header_string));
	}
	// Otherwise we use the slower PHP parsing
	$headers = array();
	// Match all HTTP headers
	if (preg_match_all('/(\w[^\s:]*):[ ]*([^\r\n]*(?:\r\n[ \t][^\r\n]*)*)/', $header_string, $matches))
	{
		// Parse each matched header
		foreach ($matches[0] as $key => $value)
		{
			// If the header has not already been set
			if (!isset($headers[$matches[1][$key]]))
			{
				// Apply the header directly
				$headers[$matches[1][$key]] = $matches[2][$key];
			}
			// Otherwise there is an existing entry
			else
			{
				// If the entry is an array
				if (is_array($headers[$matches[1][$key]]))
				{
					// Apply the new entry to the array
					$headers[$matches[1][$key]][] = $matches[2][$key];
				}
				// Otherwise create a new array with the entries
				else
				{
					$headers[$matches[1][$key]] = array(
						$headers[$matches[1][$key]],
						$matches[2][$key],
					);
				}
			}
		}
	}
	// Return the headers
	return new HTTP_Header($headers);
}

public static request_headers( ) (在 HTTP 中定义)

Parses the the HTTP request headers && returns an array containing key value pairs. This method is slow, but provides an accurate representation of the HTTP request.

// Get http headers into the request
$request->headers = HTTP::request_headers();

返回值

  • HTTP_Header

源代码

public static function request_headers()
{
	// If running on apache server
	if (function_exists('apache_request_headers'))
	{
		// Return the much faster method
		return new HTTP_Header(apache_request_headers());
	}
	// If the PECL HTTP tools are installed
	elseif (extension_loaded('http'))
	{
		// Return the much faster method
		return new HTTP_Header(http_get_request_headers());
	}
	// Setup the output
	$headers = array();
	// Parse the content type
	if (!empty($_SERVER['CONTENT_TYPE']))
	{
		$headers['content-type'] = $_SERVER['CONTENT_TYPE'];
	}
	// Parse the content length
	if (!empty($_SERVER['CONTENT_LENGTH']))
	{
		$headers['content-length'] = $_SERVER['CONTENT_LENGTH'];
	}
	foreach ($_SERVER as $key => $value)
	{
		// If there is no HTTP header here, skip
		if (strpos($key, 'HTTP_') !== 0)
		{
			continue;
		}
		// This is a dirty hack to ensure HTTP_X_FOO_BAR becomes x-foo-bar
		$headers[str_replace(array('HTTP_', '_'), array('', '-'), $key)] = $value;
	}
	return new HTTP_Header($headers);
}

public static www_form_urlencode( [ array $params = array(0) ] ) (在 HTTP 中定义)

Processes an array of key value pairs && encodes the values to meet RFC 3986

参数

  • array $params = array(0) - Params

返回值

  • string

源代码

public static function www_form_urlencode(array $params = array())
{
	if (!$params)
		return;
	$encoded = array();
	foreach ($params as $key => $value)
	{
		$encoded[] = $key . '=' . rawurlencode($value);
	}
	return implode('&', $encoded);
}