📄 client.php
字号:
<?php/** * Zend Framework * * LICENSE * * This source file is subject to the new BSD license that is bundled * with this package in the file LICENSE.txt. * It is also available through the world-wide-web at this URL: * http://framework.zend.com/license/new-bsd * If you did not receive a copy of the license and are unable to * obtain it through the world-wide-web, please send an email * to license@zend.com so we can send you a copy immediately. * * @category Zend * @package Zend_Http * @subpackage Client * @version $Id: Client.php 8064 2008-02-16 10:58:39Z thomas $ * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */require_once 'Zend/Loader.php';require_once 'Zend/Uri.php';require_once 'Zend/Http/Client/Adapter/Interface.php';require_once 'Zend/Http/Response.php';/** * Zend_Http_Client is an implemetation of an HTTP client in PHP. The client * supports basic features like sending different HTTP requests and handling * redirections, as well as more advanced features like proxy settings, HTTP * authentication and cookie persistance (using a Zend_Http_CookieJar object) * * @todo Implement proxy settings * @category Zend * @package Zend_Http * @subpackage Client * @throws Zend_Http_Client_Exception * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */class Zend_Http_Client{ /** * HTTP request methods */ const GET = 'GET'; const POST = 'POST'; const PUT = 'PUT'; const HEAD = 'HEAD'; const DELETE = 'DELETE'; const TRACE = 'TRACE'; const OPTIONS = 'OPTIONS'; const CONNECT = 'CONNECT'; /** * Supported HTTP Authentication methods */ const AUTH_BASIC = 'basic'; //const AUTH_DIGEST = 'digest'; <-- not implemented yet /** * HTTP protocol versions */ const HTTP_1 = '1.1'; const HTTP_0 = '1.0'; /** * POST data encoding methods */ const ENC_URLENCODED = 'application/x-www-form-urlencoded'; const ENC_FORMDATA = 'multipart/form-data'; /** * Configuration array, set using the constructor or using ::setConfig() * * @var unknown_type */ protected $config = array( 'maxredirects' => 5, 'strictredirects' => false, 'useragent' => 'Zend_Http_Client', 'timeout' => 10, 'adapter' => 'Zend_Http_Client_Adapter_Socket', 'httpversion' => self::HTTP_1, 'keepalive' => false, 'storeresponse' => true, 'strict' => true ); /** * The adapter used to preform the actual connection to the server * * @var Zend_Http_Client_Adapter_Interface */ protected $adapter = null; /** * Request URI * * @var Zend_Uri_Http */ protected $uri; /** * Associative array of request headers * * @var array */ protected $headers = array(); /** * HTTP request method * * @var string */ protected $method = self::GET; /** * Associative array of GET parameters * * @var array */ protected $paramsGet = array(); /** * Assiciative array of POST parameters * * @var array */ protected $paramsPost = array(); /** * Request body content type (for POST requests) * * @var string */ protected $enctype = null; /** * The raw post data to send. Could be set by setRawData($data, $enctype). * * @var string */ protected $raw_post_data = null; /** * HTTP Authentication settings * * Expected to be an associative array with this structure: * $this->auth = array('user' => 'username', 'password' => 'password', 'type' => 'basic') * Where 'type' should be one of the supported authentication types (see the AUTH_* * constants), for example 'basic' or 'digest'. * * If null, no authentication will be used. * * @var array|null */ protected $auth; /** * File upload arrays (used in POST requests) * * An associative array, where each element is of the format: * 'name' => array('filename.txt', 'text/plain', 'This is the actual file contents') * * @var array */ protected $files = array(); /** * The client's cookie jar * * @var Zend_Http_CookieJar */ protected $cookiejar = null; /** * The last HTTP request sent by the client, as string * * @var string */ protected $last_request = null; /** * The last HTTP response received by the client * * @var Zend_Http_Response */ protected $last_response = null; /** * Redirection counter * * @var int */ protected $redirectCounter = 0; /** * Contructor method. Will create a new HTTP client. Accepts the target * URL and optionally and array of headers. * * @param Zend_Uri_Http|string $uri * @param array $headers Optional request headers to set */ public function __construct($uri = null, $config = null) { if ($uri !== null) $this->setUri($uri); if ($config !== null) $this->setConfig($config); } /** * Set the URI for the next request * * @param Zend_Uri_Http|string $uri * @return Zend_Http_Client * @throws Zend_Http_Client_Exception */ public function setUri($uri) { if (is_string($uri)) { $uri = Zend_Uri::factory($uri); } if (!$uri instanceof Zend_Uri_Http) { require_once 'Zend/Http/Client/Exception.php'; throw new Zend_Http_Client_Exception('Passed parameter is not a valid HTTP URI.'); } // We have no ports, set the defaults if (! $uri->getPort()) { $uri->setPort(($uri->getScheme() == 'https' ? 443 : 80)); } $this->uri = $uri; return $this; } /** * Get the URI for the next request * * @param boolean $as_string If true, will return the URI as a string * @return Zend_Uri_Http|string */ public function getUri($as_string = false) { if ($as_string && $this->uri instanceof Zend_Uri_Http) { return $this->uri->__toString(); } else { return $this->uri; } } /** * Set configuration parameters for this HTTP client * * @param array $config * @return Zend_Http_Client */ public function setConfig($config = array()) { if (! is_array($config)) { require_once 'Zend/Http/Client/Exception.php'; throw new Zend_Http_Client_Exception('Expected array parameter, given ' . gettype($config)); } foreach ($config as $k => $v) $this->config[strtolower($k)] = $v; return $this; } /** * Set the next request's method * * Validated the passed method and sets it. If we have files set for * POST requests, and the new method is not POST, the files are silently * dropped. * * @param string $method * @return Zend_Http_Client */ public function setMethod($method = self::GET) { if (! preg_match('/^[A-Za-z_]+$/', $method)) { require_once 'Zend/Http/Client/Exception.php'; throw new Zend_Http_Client_Exception("'{$method}' is not a valid HTTP request method."); } if ($method == self::POST && $this->enctype === null) $this->setEncType(self::ENC_URLENCODED); $this->method = $method; return $this; } /** * Set one or more request headers * * This function can be used in several ways to set the client's request * headers: * 1. By providing two parameters: $name as the header to set (eg. 'Host') * and $value as it's value (eg. 'www.example.com'). * 2. By providing a single header string as the only parameter * eg. 'Host: www.example.com' * 3. By providing an array of headers as the first parameter * eg. array('host' => 'www.example.com', 'x-foo: bar'). In This case * the function will call itself recursively for each array item. * * @param string|array $name Header name, full header string ('Header: value') * or an array of headers * @param mixed $value Header value or null * @return Zend_Http_Client */ public function setHeaders($name, $value = null) { // If we got an array, go recusive! if (is_array($name)) { foreach ($name as $k => $v) { if (is_string($k)) { $this->setHeaders($k, $v); } else { $this->setHeaders($v, null); } } } else { // Check if $name needs to be split if ($value === null && (strpos($name, ':') > 0)) list($name, $value) = explode(':', $name, 2); // Make sure the name is valid if we are in strict mode if ($this->config['strict'] && (! preg_match('/^[a-zA-Z0-9-]+$/', $name))) { require_once 'Zend/Http/Client/Exception.php'; throw new Zend_Http_Client_Exception("{$name} is not a valid HTTP header name"); } $normalized_name = strtolower($name); // If $value is null or false, unset the header if ($value === null || $value === false) { unset($this->headers[$normalized_name]); // Else, set the header } else { // Header names are storred lowercase internally. if (is_string($value)) $value = trim($value); $this->headers[$normalized_name] = array($name, $value); } } return $this; } /** * Get the value of a specific header * * Note that if the header has more than one value, an array * will be returned. * * @param unknown_type $key
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -