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

📄 php_http_client_generic.php

📁 Joomla!是一套获得过多个奖项的内容管理系统(Content Management System, CMS)。Joomla!采用PHP+MySQL数据库开发
💻 PHP
📖 第 1 页 / 共 2 页
字号:
<?php/*** PHP HTTP Tools is a library for working with the http protocol* php_http_client_generic represents a basic http client* @package php-http-tools* @version 0.3* @copyright (C) 2004 John Heinstein. All rights reserved* @license http://www.gnu.org/copyleft/lesser.html LGPL License* @author John Heinstein <johnkarl@nbnet.nb.ca>* @link http://www.engageinteractive.com/php_http_tools/ PHP HTTP Tools Home Page* PHP HTTP Tools are Free Software**/if (!defined('PHP_HTTP_TOOLS_INCLUDE_PATH')) {	define('PHP_HTTP_TOOLS_INCLUDE_PATH', (dirname(__FILE__) . "/"));}/** end-of-line character sequence as defined in HTTP spec */define ('CRLF', "\r\n");/** carriage return character */define ('CR', "\r");/** line feed character */define ('LF', "\n");//http read states for client/** beginning read state */define('HTTP_READ_STATE_BEGIN', 1);/** state when reading headers */define('HTTP_READ_STATE_HEADERS', 2);/** state when reading body of message */define('HTTP_READ_STATE_BODY', 3);require_once(PHP_HTTP_TOOLS_INCLUDE_PATH . 'php_http_exceptions.php');/*** An HTTP Request class** @package php-http-tools* @author John Heinstein <johnkarl@nbnet.nb.ca>*/class php_http_request {	/** @var object A reference to the headers object */	var $headers = null;	/** @var string The requested method, e.g. GET, POST, HEAD */	var $requestMethod = 'POST';	/** @var string The requested path */	var $requestPath = '';	/** @var string The requested protocol */	var $protocol = 'HTTP';	/** @var string The version of the requested protocol */	var $protocolVersion= '1.1';	/**	* Returns the headers object	* @return object The headers object	*/	function &getHeaders() {		return $this->headers;	} //getHeaders	/**	* Sets the 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) {		$this->headers->setHeader($name, $value, $allowMultipleHeaders);	} //setHeader	/**	* Default method for setting headers; meant to be overridden in subclasses	*/	function setHeaders() {		//you will want to override this method		$this->setHeader('User-Agent', 'PHP-HTTP-Client(Generic)/0.1');		$this->setHeader('Connection', 'Close');	} //setHeaders	/**	* Sets the request method, e.g., GET	* @param string The name of the request method	* @return boolean True if the version number is valid	*/	function setRequestMethod($method) {		$method = strtoupper($method);		switch ($method) {			case 'POST':			case 'GET':			case 'HEAD':			case 'PUT':				$this->requestMethod = $method;				return true;				break;		}		return false;	} //setRequestMethod	/**	* Sets the request path, e.g., http://www.engageinteractive.com/domit/test.xml	* @param string The request path	*/	function setRequestPath($path) {		$this->requestPath = $path;	} //setRequestPath	/**	* Sets the version number of the protocol	* @param string The version number	* @return boolean True if the version number is valid	*/	function setProtocolVersion($version) {		if (($version == '1.0') || ($version == '1.1')) {			$this->protocolVersion = $version;			return true;		}		return false;	} //setProtocolVersion	/**	* Specifies a user name and password for basic authentication	* @param string The user name	* @param string The password	*/	function setAuthorization($user, $password) {		$encodedChallengeResponse = 'Basic ' . base64_encode($this->user . ':' . $this->password);		$this->setHeader('Authorization', $encodedChallengeResponse);	} //setAuthorization} //php_http_requestclass php_http_client_generic extends php_http_request {	/** @var object A reference to the connection object */	var $connection;	/** @var string True if response headers are to be generated as an object */	var $responseHeadersAsObject = false;	/** @var object The http response */	var $response = null;	/** @var string A list of event names that can be fired by the client */	var $events = array('onRequest' => null, 'onRead' => null,						'onResponse' => null, 'onResponseHeaders' => null,						'onResponseBody' => null);	/**	* HTTP Client constructor	* @param string The client connection host name, with or without its protocol prefix	* @param string The client 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_client_generic($host = '', $path = '/', $port = 80, $timeout = 0) {		$this->connection = new php_http_connection($host, $path, $port, $timeout);		$this->headers = new php_http_headers();		$this->requestPath = $path;		$this->response = new php_http_response();		$this->setHeaders();	} //php_http_client_generic	/**	* Specifies that the response headers array should be generated	* @param boolean True if the response headers array should be built	*/	function generateResponseHeadersAsObject($responseHeadersAsObject) {		$this->responseHeadersAsObject = $responseHeadersAsObject;		if ($responseHeadersAsObject) {			$this->response->headers = new php_http_headers();		}	} //generateResponseHeadersAsObject	/**	* Fires an http event that has been registered	* @param string The name of the event, e.g., onRead	* @param string The data to be passed to the event	*/	function fireEvent($target, $data) {		if ($this->events[$target] != null) {			call_user_func($this->events[$target], $data);		}	} //fireEvent	/**	* Sets which http events are to be fired	* @param string The http event option to be set	* @param string True if the event is to be fired	* @param object A reference to a custom handler for the http event data	*/	function setHTTPEvent($option, $truthVal, $customHandler = null) {		if ($customHandler != null) {			$handler =& $customHandler;		}		else {			$handler = array(&$this, 'defaultHTTPEventHandler');		}		switch($option) {			case 'onRequest':			case 'onRead':			case 'onResponse':			case 'onResponseHeaders':			case 'onResponseBody':				$truthVal ? ($this->events[$option] =& $handler) : ($this->events[$option] = null);				break;		}	} //setHTTPEvent	/**	* Evaluates whether the specified http event option is active	* @param string The http event option to evaluate	* @return boolean True if the specified option is active	*/	function getHTTPEvent($option) {		switch($option) {			case 'onRequest':			case 'onRead':			case 'onResponse':			case 'onResponseHeaders':			case 'onResponseBody':				return ($this->events[$option] != null);				break;		}	} //getHTTPEvent	/**	* The default http event handler; fired if no custom handler has been registered	* @param string The event data	*/	function defaultHTTPEventHandler($data) {		$this->printHTML($data);	} //defaultHTTPEventHandler	/**	* Prints the data to the browser as preformatted, htmlentified output	* @param string The data to be printed	*/	function printHTML($html) {		print('<pre>' . htmlentities($html)  . '</pre>');	} //printHTML	/**	* Establishes a client connection	*/	function connect() {		if (!$this->headers->headerExists('Host')) {			$this->setHeader('Host', $this->connection->host);		}		return $this->connection->connect();	} //connect	/**	* Disconnects the current client connection if one exists	*/	function disconnect() {		return $this->connection->disconnect();	} //disconnect	/**	* Evaluated whether the current client is connected	* @return boolean True if a connection exists	*/	function isConnected() {		return $this->connection->isOpen();	} //isConnected	/**	* Performs an HTTP GET	* @param string The target url	* @return object An HTTP response object	*/	function &get($url) {		$this->setRequestMethod('GET');		$this->setRequestPath($url);		$this->get_custom($url);		$this->connect();		$result = $this->send('');		return $result;	} //get	/**	* Handler for customizing the HTTP GET call	* @param string The target url	*/	function get_custom($url) {		//do nothing; meant to be overridden	} //get_custom	/**	* Performs an HTTP POST	* @param string The posted data	* @return object An HTTP response object	*/	function &post($data) {		$this->setRequestMethod('POST');		$this->setHeader('Content-Type', 'text/html');		$this->post_custom($data);		$this->connect();		return $this->send($data);	} //post	/**	* Handler for customizing the HTTP POST call	* @param string The post data	*/	function post_custom($data) {		//do nothing; meant to be overridden	} //post_custom	/**	* Performs an HTTP HEAD	* @param string The target url	* @return object An HTTP response object	*/	function &head($url) {		$this->setRequestMethod('HEAD');		$this->head_custom($url);		$this->connect();		return $this->send('');	} //head	/**	* Handler for customizing the HTTP HEAD call	* @param string The target url	*/	function head_custom($url) {		//do nothing; meant to be overridden	} //head_custom	/**	* Sends data through the client connection	* @param string The message to be sent	* @return string The http response	*/	function send($message) {		$conn =& $this->connection;		if ($conn->isOpen()) {			//build header info			$request = $this->requestMethod  . ' '  . $this->requestPath . ' '  . $this->protocol .							'/' . $this->protocolVersion . CRLF;			$request .= $this->headers->toString() . CRLF;			$request .= $message;			//init variables			$response = $headers = $body = '';			$readState = HTTP_READ_STATE_BEGIN;			$this->fireEvent('onRequest', $request);			//send request			$connResource =& $conn->connection;			fputs ($connResource, $request);			//read response			while (!feof($connResource)) {				$data = fgets($connResource, 4096);				$this->fireEvent('onRead', $data);				switch ($readState) {					case HTTP_READ_STATE_BEGIN:						$this->response->statusLine = $data;						$readState = HTTP_READ_STATE_HEADERS;						break;					case HTTP_READ_STATE_HEADERS:						if (trim($data) == '') { //end of headers is signalled by a blank line							$readState = HTTP_READ_STATE_BODY;						}						else {

⌨️ 快捷键说明

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