<?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,v 1.1 2008-12-19 07:13:33 chenliang Exp $
* @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
- 1
- 2
前往页