request.php
来自「视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.」· PHP 代码 · 共 1,144 行 · 第 1/3 页
PHP
1,144 行
<?php// +-----------------------------------------------------------------------+// | Copyright (c) 2002-2003, Richard Heyes |// | All rights reserved. |// | |// | Redistribution and use in source and binary forms, with or without |// | modification, are permitted provided that the following conditions |// | are met: |// | |// | o Redistributions of source code must retain the above copyright |// | notice, this list of conditions and the following disclaimer. |// | o Redistributions in binary form must reproduce the above copyright |// | notice, this list of conditions and the following disclaimer in the |// | documentation and/or other materials provided with the distribution.|// | o The names of the authors may not be used to endorse or promote |// | products derived from this software without specific prior written |// | permission. |// | |// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |// | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |// | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |// | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |// | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |// | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |// | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |// | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |// | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |// | |// +-----------------------------------------------------------------------+// | Author: Richard Heyes <richard@phpguru.org> |// +-----------------------------------------------------------------------+//// $Id: Request.php,v 1.41 2004/12/10 14:42:57 avb Exp $//// HTTP_Request Class//// Simple example, (Fetches yahoo.com and displays it)://// $a = &new HTTP_Request('http://www.yahoo.com/');// $a->sendRequest();// echo $a->getResponseBody();//require_once 'PEAR.php';require_once 'Net/Socket.php';require_once 'Net/URL.php';define('HTTP_REQUEST_METHOD_GET', 'GET', true);define('HTTP_REQUEST_METHOD_HEAD', 'HEAD', true);define('HTTP_REQUEST_METHOD_POST', 'POST', true);define('HTTP_REQUEST_METHOD_PUT', 'PUT', true);define('HTTP_REQUEST_METHOD_DELETE', 'DELETE', true);define('HTTP_REQUEST_METHOD_OPTIONS', 'OPTIONS', true);define('HTTP_REQUEST_METHOD_TRACE', 'TRACE', true);define('HTTP_REQUEST_HTTP_VER_1_0', '1.0', true);define('HTTP_REQUEST_HTTP_VER_1_1', '1.1', true);class HTTP_Request { /** * Instance of Net_URL * @var object Net_URL */ var $_url; /** * Type of request * @var string */ var $_method; /** * HTTP Version * @var string */ var $_http; /** * Request headers * @var array */ var $_requestHeaders; /** * Basic Auth Username * @var string */ var $_user; /** * Basic Auth Password * @var string */ var $_pass; /** * Socket object * @var object Net_Socket */ var $_sock; /** * Proxy server * @var string */ var $_proxy_host; /** * Proxy port * @var integer */ var $_proxy_port; /** * Proxy username * @var string */ var $_proxy_user; /** * Proxy password * @var string */ var $_proxy_pass; /** * Post data * @var mixed */ var $_postData; /** * Files to post * @var array */ var $_postFiles = array(); /** * Connection timeout. * @var float */ var $_timeout; /** * HTTP_Response object * @var object HTTP_Response */ var $_response; /** * Whether to allow redirects * @var boolean */ var $_allowRedirects; /** * Maximum redirects allowed * @var integer */ var $_maxRedirects; /** * Current number of redirects * @var integer */ var $_redirects; /** * Whether to append brackets [] to array variables * @var bool */ var $_useBrackets = true; /** * Attached listeners * @var array */ var $_listeners = array(); /** * Whether to save response body in response object property * @var bool */ var $_saveBody = true; /** * Timeout for reading from socket (array(seconds, microseconds)) * @var array */ var $_readTimeout = null; /** * Options to pass to Net_Socket::connect. See stream_context_create * @var array */ var $_socketOptions = null; /** * Constructor * * Sets up the object * @param string The url to fetch/access * @param array Associative array of parameters which can have the following keys: * <ul> * <li>method - Method to use, GET, POST etc (string)</li> * <li>http - HTTP Version to use, 1.0 or 1.1 (string)</li> * <li>user - Basic Auth username (string)</li> * <li>pass - Basic Auth password (string)</li> * <li>proxy_host - Proxy server host (string)</li> * <li>proxy_port - Proxy server port (integer)</li> * <li>proxy_user - Proxy auth username (string)</li> * <li>proxy_pass - Proxy auth password (string)</li> * <li>timeout - Connection timeout in seconds (float)</li> * <li>allowRedirects - Whether to follow redirects or not (bool)</li> * <li>maxRedirects - Max number of redirects to follow (integer)</li> * <li>useBrackets - Whether to append [] to array variable names (bool)</li> * <li>saveBody - Whether to save response body in response object property (bool)</li> * <li>readTimeout - Timeout for reading / writing data over the socket (array (seconds, microseconds))</li> * <li>socketOptions - Options to pass to Net_Socket object (array)</li> * </ul> * @access public */ function HTTP_Request($url = '', $params = array()) { $this->_sock = &new Net_Socket(); $this->_method = HTTP_REQUEST_METHOD_GET; $this->_http = HTTP_REQUEST_HTTP_VER_1_1; $this->_requestHeaders = array(); $this->_postData = null; $this->_user = null; $this->_pass = null; $this->_proxy_host = null; $this->_proxy_port = null; $this->_proxy_user = null; $this->_proxy_pass = null; $this->_allowRedirects = false; $this->_maxRedirects = 3; $this->_redirects = 0; $this->_timeout = null; $this->_response = null; foreach ($params as $key => $value) { $this->{'_' . $key} = $value; } if (!empty($url)) { $this->setURL($url); } // Default useragent $this->addHeader('User-Agent', 'PEAR HTTP_Request class ( http://pear.php.net/ )'); // Make sure keepalives dont knobble us $this->addHeader('Connection', 'close'); // Basic authentication if (!empty($this->_user)) { $this->_requestHeaders['Authorization'] = 'Basic ' . base64_encode($this->_user . ':' . $this->_pass); } // Use gzip encoding if possible // Avoid gzip encoding if using multibyte functions (see #1781) if (HTTP_REQUEST_HTTP_VER_1_1 == $this->_http && extension_loaded('zlib') && 0 == (2 & ini_get('mbstring.func_overload'))) { $this->addHeader('Accept-Encoding', 'gzip'); } } /** * Generates a Host header for HTTP/1.1 requests * * @access private * @return string */ function _generateHostHeader() { if ($this->_url->port != 80 AND strcasecmp($this->_url->protocol, 'http') == 0) { $host = $this->_url->host . ':' . $this->_url->port; } elseif ($this->_url->port != 443 AND strcasecmp($this->_url->protocol, 'https') == 0) { $host = $this->_url->host . ':' . $this->_url->port; } elseif ($this->_url->port == 443 AND strcasecmp($this->_url->protocol, 'https') == 0 AND strpos($this->_url->url, ':443') !== false) { $host = $this->_url->host . ':' . $this->_url->port; } else { $host = $this->_url->host; } return $host; } /** * Resets the object to its initial state (DEPRECATED). * Takes the same parameters as the constructor. * * @param string $url The url to be requested * @param array $params Associative array of parameters * (see constructor for details) * @access public * @deprecated deprecated since 1.2, call the constructor if this is necessary */ function reset($url, $params = array()) { $this->HTTP_Request($url, $params); } /** * Sets the URL to be requested * * @param string The url to be requested * @access public */ function setURL($url) { $this->_url = &new Net_URL($url, $this->_useBrackets); if (!empty($this->_url->user) || !empty($this->_url->pass)) { $this->setBasicAuth($this->_url->user, $this->_url->pass); } if (HTTP_REQUEST_HTTP_VER_1_1 == $this->_http) { $this->addHeader('Host', $this->_generateHostHeader()); } } /** * Sets a proxy to be used * * @param string Proxy host * @param int Proxy port * @param string Proxy username * @param string Proxy password * @access public */ function setProxy($host, $port = 8080, $user = null, $pass = null) { $this->_proxy_host = $host; $this->_proxy_port = $port; $this->_proxy_user = $user; $this->_proxy_pass = $pass; if (!empty($user)) { $this->addHeader('Proxy-Authorization', 'Basic ' . base64_encode($user . ':' . $pass)); } } /** * Sets basic authentication parameters * * @param string Username * @param string Password */ function setBasicAuth($user, $pass) { $this->_user = $user; $this->_pass = $pass; $this->addHeader('Authorization', 'Basic ' . base64_encode($user . ':' . $pass)); } /** * Sets the method to be used, GET, POST etc. * * @param string Method to use. Use the defined constants for this * @access public */ function setMethod($method) { $this->_method = $method; } /** * Sets the HTTP version to use, 1.0 or 1.1
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?