Auth
4 years ago
Cookie
2 years ago
Exception
4 years ago
Proxy
4 years ago
Response
4 years ago
Transport
2 years ago
Utility
2 years ago
Auth.php
4 years ago
Cookie.php
4 years ago
Exception.php
4 years ago
Hooker.php
4 years ago
Hooks.php
2 years ago
IDNAEncoder.php
4 years ago
IPv6.php
4 years ago
IRI.php
2 years ago
Proxy.php
4 years ago
Response.php
4 years ago
SSL.php
4 years ago
Session.php
2 years ago
Transport.php
4 years ago
Session.php
272 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Session handler for persistent requests and default parameters |
| 4 | * |
| 5 | * @package Requests |
| 6 | * @subpackage Session Handler |
| 7 | */ |
| 8 | |
| 9 | /** |
| 10 | * Session handler for persistent requests and default parameters |
| 11 | * |
| 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. |
| 16 | * |
| 17 | * @package Requests |
| 18 | * @subpackage Session Handler |
| 19 | */ |
| 20 | class Requests_Session { |
| 21 | |
| 22 | public function __wakeup(){throw new \LogicException( __CLASS__ . " should never be unserialized" );} |
| 23 | |
| 24 | /** |
| 25 | * Base URL for requests |
| 26 | * |
| 27 | * URLs will be made absolute using this as the base |
| 28 | * |
| 29 | * @var string|null |
| 30 | */ |
| 31 | public $url = null; |
| 32 | |
| 33 | /** |
| 34 | * Base headers for requests |
| 35 | * |
| 36 | * @var array |
| 37 | */ |
| 38 | public $headers = array(); |
| 39 | |
| 40 | /** |
| 41 | * Base data for requests |
| 42 | * |
| 43 | * If both the base data and the per-request data are arrays, the data will |
| 44 | * be merged before sending the request. |
| 45 | * |
| 46 | * @var array |
| 47 | */ |
| 48 | public $data = array(); |
| 49 | |
| 50 | /** |
| 51 | * Base options for requests |
| 52 | * |
| 53 | * The base options are merged with the per-request data for each request. |
| 54 | * The only default option is a shared cookie jar between requests. |
| 55 | * |
| 56 | * Values here can also be set directly via properties on the Session |
| 57 | * object, e.g. `$session->useragent = 'X';` |
| 58 | * |
| 59 | * @var array |
| 60 | */ |
| 61 | public $options = array(); |
| 62 | |
| 63 | /** |
| 64 | * Create a new session |
| 65 | * |
| 66 | * @param string|null $url Base URL for requests |
| 67 | * @param array $headers Default headers for requests |
| 68 | * @param array $data Default data for requests |
| 69 | * @param array $options Default options for requests |
| 70 | */ |
| 71 | public function __construct($url = null, $headers = array(), $data = array(), $options = array()) { |
| 72 | $this->url = $url; |
| 73 | $this->headers = $headers; |
| 74 | $this->data = $data; |
| 75 | $this->options = $options; |
| 76 | |
| 77 | if (empty($this->options['cookies'])) { |
| 78 | $this->options['cookies'] = new Requests_Cookie_Jar(); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Get a property's value |
| 84 | * |
| 85 | * @param string $key Property key |
| 86 | * @return mixed|null Property value, null if none found |
| 87 | */ |
| 88 | public function __get($key) { |
| 89 | if (isset($this->options[$key])) { |
| 90 | return $this->options[$key]; |
| 91 | } |
| 92 | |
| 93 | return null; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Set a property's value |
| 98 | * |
| 99 | * @param string $key Property key |
| 100 | * @param mixed $value Property value |
| 101 | */ |
| 102 | public function __set($key, $value) { |
| 103 | $this->options[$key] = $value; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Remove a property's value |
| 108 | * |
| 109 | * @param string $key Property key |
| 110 | */ |
| 111 | public function __isset($key) { |
| 112 | return isset($this->options[$key]); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Remove a property's value |
| 117 | * |
| 118 | * @param string $key Property key |
| 119 | */ |
| 120 | public function __unset($key) { |
| 121 | if (isset($this->options[$key])) { |
| 122 | unset($this->options[$key]); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | /**#@+ |
| 127 | * @see request() |
| 128 | * @param string $url |
| 129 | * @param array $headers |
| 130 | * @param array $options |
| 131 | * @return Requests_Response |
| 132 | */ |
| 133 | /** |
| 134 | * Send a GET request |
| 135 | */ |
| 136 | public function get($url, $headers = array(), $options = array()) { |
| 137 | return $this->request($url, $headers, null, Requests::GET, $options); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Send a HEAD request |
| 142 | */ |
| 143 | public function head($url, $headers = array(), $options = array()) { |
| 144 | return $this->request($url, $headers, null, Requests::HEAD, $options); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Send a DELETE request |
| 149 | */ |
| 150 | public function delete($url, $headers = array(), $options = array()) { |
| 151 | return $this->request($url, $headers, null, Requests::DELETE, $options); |
| 152 | } |
| 153 | /**#@-*/ |
| 154 | |
| 155 | /**#@+ |
| 156 | * @see request() |
| 157 | * @param string $url |
| 158 | * @param array $headers |
| 159 | * @param array $data |
| 160 | * @param array $options |
| 161 | * @return Requests_Response |
| 162 | */ |
| 163 | /** |
| 164 | * Send a POST request |
| 165 | */ |
| 166 | public function post($url, $headers = array(), $data = array(), $options = array()) { |
| 167 | return $this->request($url, $headers, $data, Requests::POST, $options); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Send a PUT request |
| 172 | */ |
| 173 | public function put($url, $headers = array(), $data = array(), $options = array()) { |
| 174 | return $this->request($url, $headers, $data, Requests::PUT, $options); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Send a PATCH request |
| 179 | * |
| 180 | * Note: Unlike {@see post} and {@see put}, `$headers` is required, as the |
| 181 | * specification recommends that should send an ETag |
| 182 | * |
| 183 | * @link https://tools.ietf.org/html/rfc5789 |
| 184 | */ |
| 185 | public function patch($url, $headers, $data = array(), $options = array()) { |
| 186 | return $this->request($url, $headers, $data, Requests::PATCH, $options); |
| 187 | } |
| 188 | /**#@-*/ |
| 189 | |
| 190 | /** |
| 191 | * Main interface for HTTP requests |
| 192 | * |
| 193 | * This method initiates a request and sends it via a transport before |
| 194 | * parsing. |
| 195 | * |
| 196 | * @see Requests::request() |
| 197 | * |
| 198 | * @throws Requests_Exception On invalid URLs (`nonhttp`) |
| 199 | * |
| 200 | * @param string $url URL to request |
| 201 | * @param array $headers Extra headers to send with the request |
| 202 | * @param array|null $data Data to send either as a query string for GET/HEAD requests, or in the body for POST requests |
| 203 | * @param string $type HTTP request type (use Requests constants) |
| 204 | * @param array $options Options for the request (see {@see Requests::request}) |
| 205 | * @return Requests_Response |
| 206 | */ |
| 207 | public function request($url, $headers = array(), $data = array(), $type = Requests::GET, $options = array()) { |
| 208 | $request = $this->merge_request(compact('url', 'headers', 'data', 'options')); |
| 209 | |
| 210 | return Requests::request($request['url'], $request['headers'], $request['data'], $type, $request['options']); |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Send multiple HTTP requests simultaneously |
| 215 | * |
| 216 | * @see Requests::request_multiple() |
| 217 | * |
| 218 | * @param array $requests Requests data (see {@see Requests::request_multiple}) |
| 219 | * @param array $options Global and default options (see {@see Requests::request}) |
| 220 | * @return array Responses (either Requests_Response or a Requests_Exception object) |
| 221 | */ |
| 222 | public function request_multiple($requests, $options = array()) { |
| 223 | foreach ($requests as $key => $request) { |
| 224 | $requests[$key] = $this->merge_request($request, false); |
| 225 | } |
| 226 | |
| 227 | $options = array_merge($this->options, $options); |
| 228 | |
| 229 | // Disallow forcing the type, as that's a per request setting |
| 230 | unset($options['type']); |
| 231 | |
| 232 | return Requests::request_multiple($requests, $options); |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Merge a request's data with the default data |
| 237 | * |
| 238 | * @param array $request Request data (same form as {@see request_multiple}) |
| 239 | * @param boolean $merge_options Should we merge options as well? |
| 240 | * @return array Request data |
| 241 | */ |
| 242 | protected function merge_request($request, $merge_options = true) { |
| 243 | if ($this->url !== null) { |
| 244 | $request['url'] = Requests_IRI::absolutize($this->url, $request['url']); |
| 245 | $request['url'] = $request['url']->uri; |
| 246 | } |
| 247 | |
| 248 | if (empty($request['headers'])) { |
| 249 | $request['headers'] = array(); |
| 250 | } |
| 251 | $request['headers'] = array_merge($this->headers, $request['headers']); |
| 252 | |
| 253 | if (empty($request['data'])) { |
| 254 | if (is_array($this->data)) { |
| 255 | $request['data'] = $this->data; |
| 256 | } |
| 257 | } |
| 258 | elseif (is_array($request['data']) && is_array($this->data)) { |
| 259 | $request['data'] = array_merge($this->data, $request['data']); |
| 260 | } |
| 261 | |
| 262 | if ($merge_options !== false) { |
| 263 | $request['options'] = array_merge($this->options, $request['options']); |
| 264 | |
| 265 | // Disallow forcing the type, as that's a per request setting |
| 266 | unset($request['options']['type']); |
| 267 | } |
| 268 | |
| 269 | return $request; |
| 270 | } |
| 271 | } |
| 272 |