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_Client
 18: {
 19:     /**
 20:      * The current HTTP request
 21:      * @var Horde_Http_Request_Base
 22:      */
 23:     protected $_request;
 24: 
 25:     /**
 26:      * The previous HTTP request
 27:      * @var Horde_Http_Request_Base
 28:      */
 29:     protected $_lastRequest;
 30: 
 31:     /**
 32:      * The most recent HTTP response
 33:      * @var Horde_Http_Response_Base
 34:      */
 35:     protected $_lastResponse;
 36: 
 37:     /**
 38:      * Use POST instead of PUT and DELETE, sending X-HTTP-Method-Override with
 39:      * the intended method name instead.
 40:      *
 41:      * @var boolean
 42:      */
 43:     protected $_httpMethodOverride = false;
 44: 
 45:     /**
 46:      * Horde_Http_Client constructor.
 47:      *
 48:      * @param array $args Any Http_Client settings to initialize in the
 49:      *                    constructor. Available settings are:
 50:      *                    - client.httpMethodOverride
 51:      *                    - request
 52:      *                    - request.uri
 53:      *                    - request.headers
 54:      *                    - request.method
 55:      *                    - request.data
 56:      *                    - request.username
 57:      *                    - request.password
 58:      *                    - request.authenticationScheme
 59:      *                    - request.proxyServer
 60:      *                    - request.proxyPort
 61:      *                    - request.proxyType
 62:      *                    - request.proxyUsername
 63:      *                    - request.proxyPassword
 64:      *                    - request.proxyAuthenticationScheme
 65:      *                    - request.timeout
 66:      */
 67:     public function __construct($args = array())
 68:     {
 69:         // Set or create request object
 70:         if (isset($args['request'])) {
 71:             $this->_request = $args['request'];
 72:             unset($args['request']);
 73:         } else {
 74:             $requestFactory = new Horde_Http_Request_Factory();
 75:             $this->_request = $requestFactory->create();
 76:         }
 77: 
 78:         foreach ($args as $key => $val) {
 79:             $this->$key = $val;
 80:         }
 81:     }
 82: 
 83:     /**
 84:      * Send a GET request
 85:      *
 86:      * @throws Horde_Http_Exception
 87:      * @return Horde_Http_Response_Base
 88:      */
 89:     public function get($uri = null, $headers = array())
 90:     {
 91:         return $this->request('GET', $uri, null, $headers);
 92:     }
 93: 
 94:     /**
 95:      * Send a POST request
 96:      *
 97:      * @throws Horde_Http_Exception
 98:      * @return Horde_Http_Response_Base
 99:      */
100:     public function post($uri = null, $data = null, $headers = array())
101:     {
102:         return $this->request('POST', $uri, $data, $headers);
103:     }
104: 
105:     /**
106:      * Send a PUT request
107:      *
108:      * @throws Horde_Http_Exception
109:      * @return Horde_Http_Response_Base
110:      */
111:     public function put($uri = null, $data = null, $headers = array())
112:     {
113:         if ($this->_httpMethodOverride) {
114:             $headers = array_merge(array('X-HTTP-Method-Override' => 'PUT'), $headers);
115:             return $this->post($uri, $data, $headers);
116:         }
117: 
118:         return $this->request('PUT', $uri, $data, $headers);
119:     }
120: 
121:     /**
122:      * Send a DELETE request
123:      *
124:      * @throws Horde_Http_Exception
125:      * @return Horde_Http_Response_Base
126:      */
127:     public function delete($uri = null, $headers = array())
128:     {
129:         if ($this->_httpMethodOverride) {
130:             $headers = array_merge(array('X-HTTP-Method-Override' => 'DELETE'), $headers);
131:             return $this->post($uri, null, $headers);
132:         }
133: 
134:         return $this->request('DELETE', $uri, null, $headers);
135:     }
136: 
137:     /**
138:      * Send a HEAD request
139:      * @TODO
140:      *
141:      * @throws Horde_Http_Exception
142:      * @return  ? Probably just the status
143:      */
144:     public function head($uri = null, $headers = array())
145:     {
146:         return $this->request('HEAD', $uri, null, $headers);
147:     }
148: 
149:     /**
150:      * Send an HTTP request
151:      *
152:      * @param string $method  HTTP request method (GET, PUT, etc.)
153:      * @param string $uri     URI to request, if different from $this->uri
154:      * @param mixed $data     Request data. Can be an array of form data that
155:      *                        will be encoded automatically, or a raw string.
156:      * @param array $headers  Any headers specific to this request. They will
157:      *                        be combined with $this->_headers, and override
158:      *                        headers of the same name for this request only.
159:      *
160:      * @throws Horde_Http_Exception
161:      * @return Horde_Http_Response_Base
162:      */
163:     public function request($method, $uri = null, $data = null, $headers = array())
164:     {
165:         if ($method !== null) {
166:             $this->request->method = $method;
167:         }
168:         if ($uri !== null) {
169:             $this->request->uri = $uri;
170:         }
171:         if ($data !== null) {
172:             $this->request->data = $data;
173:         }
174:         if (count($headers)) {
175:             $this->request->setHeaders($headers);
176:         }
177: 
178:         $this->_lastRequest = $this->_request;
179:         $this->_lastResponse = $this->_request->send();
180:         return $this->_lastResponse;
181:     }
182: 
183:     /**
184:      * Get a client parameter
185:      *
186:      * @param string $name  The parameter to get.
187:      * @return mixed        Parameter value.
188:      */
189:     public function __get($name)
190:     {
191:         return isset($this->{'_' . $name}) ? $this->{'_' . $name} : null;
192:     }
193: 
194:     /**
195:      * Set a client parameter
196:      *
197:      * @param string $name   The parameter to set.
198:      * @param mixed  $value  Parameter value.
199:      */
200:     public function __set($name, $value)
201:     {
202:         if ((strpos($name, '.') === false)) {
203:             if (isset($this->{'_' . $name})) {
204:                 $this->{'_' . $name} = $value;
205:                 return true;
206:             } else {
207:                 throw new Horde_Http_Exception('unknown parameter: "' . $name . '"');
208:             }
209:         }
210: 
211:         list($object, $objectkey) = explode('.', $name, 2);
212:         if ($object == 'request') {
213:             $this->$object->$objectkey = $value;
214:             return true;
215:         } elseif ($object == 'client') {
216:             $objectKey = '_' . $objectKey;
217:             $this->$objectKey = $value;
218:             return true;
219:         }
220: 
221:         throw new Horde_Http_Exception('unknown parameter: "' . $name . '"');
222:     }
223: }
224: 
API documentation generated by ApiGen