Overview

Packages

  • Http

Classes

  • Horde_Http
  • Horde_Http_Client
  • Horde_Http_Exception
  • Horde_Http_Request_Base
  • Horde_Http_Request_Curl
  • Horde_Http_Request_Factory
  • Horde_Http_Request_Fopen
  • Horde_Http_Request_Mock
  • Horde_Http_Request_Peclhttp
  • Horde_Http_Response_Base
  • Horde_Http_Response_Curl
  • Horde_Http_Response_Fopen
  • Horde_Http_Response_Mock
  • Horde_Http_Response_Peclhttp
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * Copyright 2007-2012 Horde LLC (http://www.horde.org/)
  4:  *
  5:  * @author   Chuck Hagenbuch <chuck@horde.org>
  6:  * @license  http://www.horde.org/licenses/bsd BSD
  7:  * @category Horde
  8:  * @package  Http
  9:  */
 10: 
 11: /**
 12:  * @author   Chuck Hagenbuch <chuck@horde.org>
 13:  * @license  http://www.horde.org/licenses/bsd BSD
 14:  * @category Horde
 15:  * @package  Http
 16:  */
 17: class Horde_Http_Request_Peclhttp extends Horde_Http_Request_Base
 18: {
 19:     /**
 20:      * Map of HTTP authentication schemes from Horde_Http constants to HTTP_AUTH constants.
 21:      * @var array
 22:      */
 23:     protected $_httpAuthSchemes = array(
 24:         Horde_Http::AUTH_ANY => HTTP_AUTH_ANY,
 25:         Horde_Http::AUTH_BASIC => HTTP_AUTH_BASIC,
 26:         Horde_Http::AUTH_DIGEST => HTTP_AUTH_DIGEST,
 27:         Horde_Http::AUTH_GSSNEGOTIATE => HTTP_AUTH_GSSNEG,
 28:         Horde_Http::AUTH_NTLM => HTTP_AUTH_NTLM,
 29:     );
 30: 
 31:     /**
 32:      * Constructor
 33:      *
 34:      * @throws Horde_Http_Exception
 35:      */
 36:     public function __construct($args = array())
 37:     {
 38:         if (!class_exists('HttpRequest', false)) {
 39:             throw new Horde_Http_Exception('The pecl_http extension is not installed. See http://php.net/http.install');
 40:         }
 41: 
 42:         parent::__construct($args);
 43:     }
 44: 
 45:     /**
 46:      * Send this HTTP request
 47:      *
 48:      * @throws Horde_Http_Exception
 49:      * @return Horde_Http_Response_Base
 50:      */
 51:     public function send()
 52:     {
 53:         if (!defined('HTTP_METH_' . $this->method)) {
 54:             throw new Horde_Http_Exception('Method ' . $this->method . ' not supported.');
 55:         }
 56: 
 57:         $httpRequest = new HttpRequest($this->uri, constant('HTTP_METH_' . $this->method));
 58: 
 59:         $data = $this->data;
 60:         if (is_array($data)) {
 61:             $httpRequest->setPostFields($data);
 62:         } else {
 63:             if ($this->method == 'PUT') {
 64:                 $httpRequest->setPutData($data);
 65:             } else {
 66:                 $httpRequest->setBody($data);
 67:             }
 68:         }
 69: 
 70:         $httpOptions = array('timeout' => $this->timeout);
 71: 
 72:         // Proxy settings
 73:         if ($this->proxyServer) {
 74:             $httpOptions['proxyhost'] = $this->proxyServer;
 75:             if ($this->proxyPort) {
 76:                 $httpOptions['proxyport'] = $this->proxyPort;
 77:             }
 78:             if ($this->proxyUsername && $this->proxyPassword) {
 79:                 $httpOptions['proxyauth'] = $this->proxyUsername . ':' . $this->proxyPassword;
 80:                 $httpOptions['proxyauthtype'] = $this->_httpAuthScheme($this->proxyAuthenticationScheme);
 81:             }
 82:             if ($this->proxyType == Horde_Http::PROXY_SOCKS4) {
 83:                 $httpOptions['proxytype'] = HTTP_PROXY_SOCKS4;
 84:             } else if ($this->proxyType == Horde_Http::PROXY_SOCKS5) {
 85:                 $httpOptions['proxytype'] = HTTP_PROXY_SOCKS5;
 86:             } else if ($this->proxyType != Horde_Http::PROXY_HTTP) {
 87:                 throw new Horde_Http_Exception(sprintf('Proxy type %s not supported by this request type!', $this->proxyType));
 88:             }
 89:         }
 90: 
 91:         // Authentication settings
 92:         if ($this->username) {
 93:             $httpOptions['httpauth'] = $this->username . ':' . $this->password;
 94:             $httpOptions['httpauthtype'] = $this->_httpAuthScheme($this->authenticationScheme);
 95:         }
 96: 
 97:         // Headers
 98:         $httpOptions['headers'] = $this->headers;
 99: 
100:         // Redirects
101:         $httpOptions['redirect'] = (int)$this->redirects;
102: 
103:         // Set options
104:         $httpRequest->setOptions($httpOptions);
105: 
106:         try {
107:             $httpResponse = $httpRequest->send();
108:         } catch (HttpException $e) {
109:             throw new Horde_Http_Exception($e->getMessage(), $e->getCode(), $e);
110:         }
111: 
112:         return new Horde_Http_Response_Peclhttp($this->uri, $httpResponse);
113:     }
114: 
115:     /**
116:      * Translate a Horde_Http::AUTH_* constant to HTTP_AUTH_*
117:      *
118:      * @param const
119:      * @throws Horde_Http_Exception
120:      * @return const
121:      */
122:     protected function _httpAuthScheme($httpAuthScheme)
123:     {
124:         if (!isset($this->_httpAuthSchemes[$httpAuthScheme])) {
125:             throw new Horde_Http_Exception('Unsupported authentication scheme (' . $httpAuthScheme . ')');
126:         }
127:         return $this->_httpAuthSchemes[$httpAuthScheme];
128:     }
129: }
130: 
API documentation generated by ApiGen