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_Curl extends Horde_Http_Request_Base
 18: {
 19:     /**
 20:      * Map of HTTP authentication schemes from Horde_Http constants to
 21:      * HTTP_AUTH constants.
 22:      *
 23:      * @var array
 24:      */
 25:     protected $_httpAuthSchemes = array(
 26:         Horde_Http::AUTH_ANY => CURLAUTH_ANY,
 27:         Horde_Http::AUTH_BASIC => CURLAUTH_BASIC,
 28:         Horde_Http::AUTH_DIGEST => CURLAUTH_DIGEST,
 29:         Horde_Http::AUTH_GSSNEGOTIATE => CURLAUTH_GSSNEGOTIATE,
 30:         Horde_Http::AUTH_NTLM => CURLAUTH_NTLM,
 31:     );
 32: 
 33:     /**
 34:      * Constructor
 35:      *
 36:      * @throws Horde_Http_Exception
 37:      */
 38:     public function __construct($args = array())
 39:     {
 40:         if (!extension_loaded('curl')) {
 41:             throw new Horde_Http_Exception('The curl extension is not installed. See http://php.net/curl.installation');
 42:         }
 43: 
 44:         parent::__construct($args);
 45:     }
 46: 
 47:     /**
 48:      * Send this HTTP request
 49:      *
 50:      * @throws Horde_Http_Exception
 51:      * @return Horde_Http_Response_Base
 52:      */
 53:     public function send()
 54:     {
 55:         $curl = curl_init();
 56:         curl_setopt($curl, CURLOPT_URL, $this->uri);
 57:         curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
 58:         curl_setopt($curl, CURLOPT_HEADER, true);
 59:         curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $this->method);
 60:         curl_setopt($curl, CURLOPT_TIMEOUT, $this->timeout);
 61: 
 62:         // User-Agent
 63:         curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)');
 64: 
 65:         // Redirects
 66:         if ($this->redirects) {
 67:             curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
 68:             curl_setopt($curl, CURLOPT_MAXREDIRS, $this->redirects);
 69:         } else {
 70:             curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false);
 71:             curl_setopt($curl, CURLOPT_MAXREDIRS, 0);
 72:         }
 73: 
 74:         $data = $this->data;
 75:         if (is_array($data)) {
 76:             // If we don't set POSTFIELDS to a string, and the first value
 77:             // begins with @, it will be treated as a filename, and the proper
 78:             // POST data isn't passed.
 79:             $data = http_build_query($data);
 80:         }
 81:         if ($data) {
 82:             curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
 83:         }
 84: 
 85:         // Proxy settings
 86:         if ($this->proxyServer) {
 87:             curl_setopt($curl, CURLOPT_PROXY, $this->proxyServer);
 88:             if ($this->proxyPort) {
 89:                 curl_setopt($curl, CURLOPT_PROXYPORT, $this->proxyPort);
 90:             }
 91:             if ($this->proxyUsername && $this->proxyPassword) {
 92:                 curl_setopt($curl, CURLOPT_PROXYUSERPWD, $this->proxyUsername . ':' . $this->proxyPassword);
 93:                 curl_setopt($curl, CURLOPT_PROXYAUTH, $this->_httpAuthScheme($this->proxyAuthenticationScheme));
 94:             }
 95:             if ($this->proxyType == Horde_Http::PROXY_SOCKS5) {
 96:                 curl_setopt($curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
 97:             } else if ($this->proxyType != Horde_Http::PROXY_HTTP) {
 98:                 throw new Horde_Http_Exception(sprintf('Proxy type %s not supported by this request type!', $this->proxyType));
 99:             }
100:         }
101: 
102:         // Authentication settings
103:         if ($this->username) {
104:             curl_setopt($curl, CURLOPT_USERPWD, $this->username . ':' . $this->password);
105:             curl_setopt($curl, CURLOPT_HTTPAUTH, $this->_httpAuthScheme($this->authenticationScheme));
106:         }
107: 
108:         // Concatenate the headers
109:         $hdr = array();
110:         foreach ($this->headers as $header => $value) {
111:             $hdr[] = $header . ': ' . $value;
112:         }
113:         curl_setopt($curl, CURLOPT_HTTPHEADER, $hdr);
114: 
115:         $result = curl_exec($curl);
116:         if ($result === false) {
117:             throw new Horde_Http_Exception(curl_error($curl), curl_errno($curl));
118:         }
119:         $info = curl_getinfo($curl);
120:         return new Horde_Http_Response_Curl($this->uri, $result, $info);
121:     }
122: 
123:     /**
124:      * Translate a Horde_Http::AUTH_* constant to CURLAUTH_*
125:      *
126:      * @param const
127:      * @throws Horde_Http_Exception
128:      * @return const
129:      */
130:     protected function _httpAuthScheme($httpAuthScheme)
131:     {
132:         if (!isset($this->_httpAuthSchemes[$httpAuthScheme])) {
133:             throw new Horde_Http_Exception('Unsupported authentication scheme (' . $httpAuthScheme . ')');
134:         }
135:         return $this->_httpAuthSchemes[$httpAuthScheme];
136:     }
137: }
138: 
API documentation generated by ApiGen