3 * Session handler for persistent requests and default parameters
6 * @subpackage Session Handler
10 * Session handler for persistent requests and default parameters
12 * Allows various options to be set as default values, and merges both the
13 * options and URL properties together. A base URL can be set for all requests,
14 * with all subrequests resolved from this. Base options can be set (including
15 * a shared cookie jar), then overridden for individual requests.
18 * @subpackage Session Handler
20 class Requests_Session {
22 * Base URL for requests
24 * URLs will be made absolute using this as the base
30 * Base headers for requests
33 public $headers = array();
36 * Base data for requests
38 * If both the base data and the per-request data are arrays, the data will
39 * be merged before sending the request.
43 public $data = array();
46 * Base options for requests
48 * The base options are merged with the per-request data for each request.
49 * The only default option is a shared cookie jar between requests.
51 * Values here can also be set directly via properties on the Session
52 * object, e.g. `$session->useragent = 'X';`
56 public $options = array();
59 * Create a new session
61 * @param string|null $url Base URL for requests
62 * @param array $headers Default headers for requests
63 * @param array $data Default data for requests
64 * @param array $options Default options for requests
66 public function __construct($url = null, $headers = array(), $data = array(), $options = array()) {
68 $this->headers = $headers;
70 $this->options = $options;
72 if (empty($this->options['cookies'])) {
73 $this->options['cookies'] = new Requests_Cookie_Jar();
78 * Get a property's value
80 * @param string $key Property key
81 * @return mixed|null Property value, null if none found
83 public function __get($key) {
84 if (isset($this->options[$key])) {
85 return $this->options[$key];
92 * Set a property's value
94 * @param string $key Property key
95 * @param mixed $value Property value
97 public function __set($key, $value) {
98 $this->options[$key] = $value;
102 * Remove a property's value
104 * @param string $key Property key
106 public function __isset($key) {
107 return isset($this->options[$key]);
111 * Remove a property's value
113 * @param string $key Property key
115 public function __unset($key) {
116 if (isset($this->options[$key])) {
117 unset($this->options[$key]);
124 * @param array $headers
125 * @param array $options
126 * @return Requests_Response
131 public function get($url, $headers = array(), $options = array()) {
132 return $this->request($url, $headers, null, Requests::GET, $options);
136 * Send a HEAD request
138 public function head($url, $headers = array(), $options = array()) {
139 return $this->request($url, $headers, null, Requests::HEAD, $options);
143 * Send a DELETE request
145 public function delete($url, $headers = array(), $options = array()) {
146 return $this->request($url, $headers, null, Requests::DELETE, $options);
153 * @param array $headers
155 * @param array $options
156 * @return Requests_Response
159 * Send a POST request
161 public function post($url, $headers = array(), $data = array(), $options = array()) {
162 return $this->request($url, $headers, $data, Requests::POST, $options);
168 public function put($url, $headers = array(), $data = array(), $options = array()) {
169 return $this->request($url, $headers, $data, Requests::PUT, $options);
173 * Send a PATCH request
175 * Note: Unlike {@see post} and {@see put}, `$headers` is required, as the
176 * specification recommends that should send an ETag
178 * @link https://tools.ietf.org/html/rfc5789
180 public function patch($url, $headers, $data = array(), $options = array()) {
181 return $this->request($url, $headers, $data, Requests::PATCH, $options);
186 * Main interface for HTTP requests
188 * This method initiates a request and sends it via a transport before
191 * @see Requests::request()
193 * @throws Requests_Exception On invalid URLs (`nonhttp`)
195 * @param string $url URL to request
196 * @param array $headers Extra headers to send with the request
197 * @param array|null $data Data to send either as a query string for GET/HEAD requests, or in the body for POST requests
198 * @param string $type HTTP request type (use Requests constants)
199 * @param array $options Options for the request (see {@see Requests::request})
200 * @return Requests_Response
202 public function request($url, $headers = array(), $data = array(), $type = Requests::GET, $options = array()) {
203 $request = $this->merge_request(compact('url', 'headers', 'data', 'options'));
205 return Requests::request($request['url'], $request['headers'], $request['data'], $type, $request['options']);
209 * Send multiple HTTP requests simultaneously
211 * @see Requests::request_multiple()
213 * @param array $requests Requests data (see {@see Requests::request_multiple})
214 * @param array $options Global and default options (see {@see Requests::request})
215 * @return array Responses (either Requests_Response or a Requests_Exception object)
217 public function request_multiple($requests, $options = array()) {
218 foreach ($requests as $key => $request) {
219 $requests[$key] = $this->merge_request($request, false);
222 $options = array_merge($this->options, $options);
224 // Disallow forcing the type, as that's a per request setting
225 unset($options['type']);
227 return Requests::request_multiple($requests, $options);
231 * Merge a request's data with the default data
233 * @param array $request Request data (same form as {@see request_multiple})
234 * @param boolean $merge_options Should we merge options as well?
235 * @return array Request data
237 protected function merge_request($request, $merge_options = true) {
238 if ($this->url !== null) {
239 $request['url'] = Requests_IRI::absolutize($this->url, $request['url']);
240 $request['url'] = $request['url']->uri;
243 if (empty($request['headers'])) {
244 $request['headers'] = array();
246 $request['headers'] = array_merge($this->headers, $request['headers']);
248 if (empty($request['data'])) {
249 if (is_array($this->data)) {
250 $request['data'] = $this->data;
253 elseif (is_array($request['data']) && is_array($this->data)) {
254 $request['data'] = array_merge($this->data, $request['data']);
257 if ($merge_options !== false) {
258 $request['options'] = array_merge($this->options, $request['options']);
260 // Disallow forcing the type, as that's a per request setting
261 unset($request['options']['type']);