截流自动化的商城平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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