Requests_Session

Session handler for persistent requests and default parameters.

Defined (1)

The class is defined in the following location(s).

/wp-includes/Requests/Session.php  
  1. class Requests_Session { 
  2. /** 
  3. * Base URL for requests 
  4. * URLs will be made absolute using this as the base 
  5. * @var string|null 
  6. */ 
  7. public $url = null; 
  8.  
  9. /** 
  10. * Base headers for requests 
  11. * @var array 
  12. */ 
  13. public $headers = array(); 
  14.  
  15. /** 
  16. * Base data for requests 
  17. * If both the base data and the per-request data are arrays, the data will 
  18. * be merged before sending the request. 
  19. * @var array 
  20. */ 
  21. public $data = array(); 
  22.  
  23. /** 
  24. * Base options for requests 
  25. * The base options are merged with the per-request data for each request. 
  26. * The only default option is a shared cookie jar between requests. 
  27. * Values here can also be set directly via properties on the Session 
  28. * object, e.g. `$session->useragent = 'X';` 
  29. * @var array 
  30. */ 
  31. public $options = array(); 
  32.  
  33. /** 
  34. * Create a new session 
  35. * @param string|null $url Base URL for requests 
  36. * @param array $headers Default headers for requests 
  37. * @param array $data Default data for requests 
  38. * @param array $options Default options for requests 
  39. */ 
  40. public function __construct($url = null, $headers = array(), $data = array(), $options = array()) { 
  41. $this->url = $url; 
  42. $this->headers = $headers; 
  43. $this->data = $data; 
  44. $this->options = $options; 
  45.  
  46. if (empty($this->options['cookies'])) { 
  47. $this->options['cookies'] = new Requests_Cookie_Jar(); 
  48.  
  49. /** 
  50. * Get a property's value 
  51. * @param string $key Property key 
  52. * @return mixed|null Property value, null if none found 
  53. */ 
  54. public function __get($key) { 
  55. if (isset($this->options[$key])) { 
  56. return $this->options[$key]; 
  57.  
  58. return null; 
  59.  
  60. /** 
  61. * Set a property's value 
  62. * @param string $key Property key 
  63. * @param mixed $value Property value 
  64. */ 
  65. public function __set($key, $value) { 
  66. $this->options[$key] = $value; 
  67.  
  68. /** 
  69. * Remove a property's value 
  70. * @param string $key Property key 
  71. */ 
  72. public function __isset($key) { 
  73. return isset($this->options[$key]); 
  74.  
  75. /** 
  76. * Remove a property's value 
  77. * @param string $key Property key 
  78. */ 
  79. public function __unset($key) { 
  80. if (isset($this->options[$key])) { 
  81. unset($this->options[$key]); 
  82.  
  83. /**#@+ 
  84. * @see request() 
  85. * @param string $url 
  86. * @param array $headers 
  87. * @param array $options 
  88. * @return Requests_Response 
  89. */ 
  90. /** 
  91. * Send a GET request 
  92. */ 
  93. public function get($url, $headers = array(), $options = array()) { 
  94. return $this->request($url, $headers, null, Requests::GET, $options); 
  95.  
  96. /** 
  97. * Send a HEAD request 
  98. */ 
  99. public function head($url, $headers = array(), $options = array()) { 
  100. return $this->request($url, $headers, null, Requests::HEAD, $options); 
  101.  
  102. /** 
  103. * Send a DELETE request 
  104. */ 
  105. public function delete($url, $headers = array(), $options = array()) { 
  106. return $this->request($url, $headers, null, Requests::DELETE, $options); 
  107. /**#@-*/ 
  108.  
  109. /**#@+ 
  110. * @see request() 
  111. * @param string $url 
  112. * @param array $headers 
  113. * @param array $data 
  114. * @param array $options 
  115. * @return Requests_Response 
  116. */ 
  117. /** 
  118. * Send a POST request 
  119. */ 
  120. public function post($url, $headers = array(), $data = array(), $options = array()) { 
  121. return $this->request($url, $headers, $data, Requests::POST, $options); 
  122.  
  123. /** 
  124. * Send a PUT request 
  125. */ 
  126. public function put($url, $headers = array(), $data = array(), $options = array()) { 
  127. return $this->request($url, $headers, $data, Requests::PUT, $options); 
  128.  
  129. /** 
  130. * Send a PATCH request 
  131. * Note: Unlike {@see post} and {@see put}, `$headers` is required, as the 
  132. * specification recommends that should send an ETag 
  133. * @link https://tools.ietf.org/html/rfc5789 
  134. */ 
  135. public function patch($url, $headers, $data = array(), $options = array()) { 
  136. return $this->request($url, $headers, $data, Requests::PATCH, $options); 
  137. /**#@-*/ 
  138.  
  139. /** 
  140. * Main interface for HTTP requests 
  141. * This method initiates a request and sends it via a transport before 
  142. * parsing. 
  143. * @see Requests::request() 
  144. * @throws Requests_Exception On invalid URLs (`nonhttp`) 
  145. * @param string $url URL to request 
  146. * @param array $headers Extra headers to send with the request 
  147. * @param array|null $data Data to send either as a query string for GET/HEAD requests, or in the body for POST requests 
  148. * @param string $type HTTP request type (use Requests constants) 
  149. * @param array $options Options for the request (see {@see Requests::request}) 
  150. * @return Requests_Response 
  151. */ 
  152. public function request($url, $headers = array(), $data = array(), $type = Requests::GET, $options = array()) { 
  153. $request = $this->merge_request(compact('url', 'headers', 'data', 'options')); 
  154.  
  155. return Requests::request($request['url'], $request['headers'], $request['data'], $type, $request['options']); 
  156.  
  157. /** 
  158. * Send multiple HTTP requests simultaneously 
  159. * @see Requests::request_multiple() 
  160. * @param array $requests Requests data (see {@see Requests::request_multiple}) 
  161. * @param array $options Global and default options (see {@see Requests::request}) 
  162. * @return array Responses (either Requests_Response or a Requests_Exception object) 
  163. */ 
  164. public function request_multiple($requests, $options = array()) { 
  165. foreach ($requests as $key => $request) { 
  166. $requests[$key] = $this->merge_request($request, false); 
  167.  
  168. $options = array_merge($this->options, $options); 
  169.  
  170. // Disallow forcing the type, as that's a per request setting 
  171. unset($options['type']); 
  172.  
  173. return Requests::request_multiple($requests, $options); 
  174.  
  175. /** 
  176. * Merge a request's data with the default data 
  177. * @param array $request Request data (same form as {@see request_multiple}) 
  178. * @param boolean $merge_options Should we merge options as well? 
  179. * @return array Request data 
  180. */ 
  181. protected function merge_request($request, $merge_options = true) { 
  182. if ($this->url !== null) { 
  183. $request['url'] = Requests_IRI::absolutize($this->url, $request['url']); 
  184. $request['url'] = $request['url']->uri; 
  185.  
  186. if (empty($request['headers'])) { 
  187. $request['headers'] = array(); 
  188. $request['headers'] = array_merge($this->headers, $request['headers']); 
  189.  
  190. if (empty($request['data'])) { 
  191. if (is_array($this->data)) { 
  192. $request['data'] = $this->data; 
  193. elseif (is_array($request['data']) && is_array($this->data)) { 
  194. $request['data'] = array_merge($this->data, $request['data']); 
  195.  
  196. if ($merge_options !== false) { 
  197. $request['options'] = array_merge($this->options, $request['options']); 
  198.  
  199. // Disallow forcing the type, as that's a per request setting 
  200. unset($request['options']['type']); 
  201.  
  202. return $request;