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.

201 lines
6.5 KiB

9 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. # p3k-http
  2. A simple HTTP client, used by https://p3k.io projects.
  3. ## Usage
  4. ### GET
  5. ```php
  6. $http = new p3k\HTTP();
  7. $headers = [
  8. 'Accept: text/html, */*'
  9. ];
  10. $response = $http->get('http://example.com/', $headers);
  11. ```
  12. ### POST
  13. ```php
  14. $http = new p3k\HTTP();
  15. $headers = [
  16. 'Accept: application/json',
  17. 'Content-type: application/json'
  18. ];
  19. $response = $http->post('http://example.com/', json_encode([
  20. 'foo' => 'bar'
  21. ], $headers);
  22. ```
  23. ### HEAD
  24. ```php
  25. $http = new p3k\HTTP();
  26. $headers = [
  27. 'Accept: text/html, */*'
  28. ];
  29. $response = $http->head('http://example.com/', $headers);
  30. ```
  31. ### Safe mode
  32. When the URLs you fetch come from other people (a user's website, a
  33. discovered endpoint, a link in a post), turn on safe mode so they cannot
  34. point your server at itself or at your private network:
  35. ```php
  36. $http = new p3k\HTTP();
  37. $http->set_safe_mode(true);
  38. ```
  39. In safe mode:
  40. * only `http` and `https` URLs are fetched (no `file`, `gopher`, `dict`, …)
  41. * the host must resolve only to public addresses; loopback, private, link-local
  42. and other reserved ranges are refused, as are hosts written as unusual IP
  43. forms like `2130706433` or `0x7f.1`
  44. * curl connects to exactly the addresses that were checked, so a DNS answer
  45. that changes between the check and the request makes no difference
  46. * redirects are followed one at a time and every hop is checked the same way;
  47. `Authorization`, `Proxy-Authorization` and `Cookie` headers are dropped when a
  48. redirect goes to a different origin
  49. A refused request makes no connection and returns `code` 0 with `error` set to
  50. `blocked_url` (or `dns_error` when the host does not resolve).
  51. To reach a private server on purpose, such as a development site on your LAN,
  52. allow it by hostname, address or CIDR range:
  53. ```php
  54. $http->set_safe_mode(true, ['dev.example.com', '10.11.11.0/24']);
  55. ```
  56. Custom transports get the same checks. A transport that implements
  57. `p3k\HTTP\Pinnable` is also held to the checked addresses, as the curl transport is.
  58. ### Response
  59. The get/post/head functions will return an array with the following properties:
  60. * `code` - integer, the HTTP response code that was returned
  61. * `headers` - array, the HTTP headers returned
  62. * `rels` - array, the parsed HTTP rel values from any `Link` headers
  63. * `body` - string, the body of the HTTP response, or false/omit for a HEAD request
  64. * `error` - string, an error string. see below for the enumerated list.
  65. * `error_description` - string,
  66. * `url` - string, the final URL retrieved after following any redirects
  67. * `debug` - string, the full HTTP response
  68. #### `headers`
  69. The `headers` key will be an array of all the header values returned. The values will be either a string or array depending on whether there were multiple values returned for a given header name.
  70. ```
  71. [headers] => Array
  72. (
  73. [Server] => nginx/1.12.0
  74. [Content-Type] => text/html; charset=UTF-8
  75. [Transfer-Encoding] => chunked
  76. [Connection] => keep-alive
  77. [Cache-Control] => no-cache
  78. [Link] => Array
  79. (
  80. [0] => <https://switchboard.p3k.io/>; rel="hub"
  81. [1] => <https://aaronparecki.com/auth>; rel="authorization_endpoint"
  82. [2] => <https://aaronparecki.com/micropub>; rel="micropub"
  83. [3] => <https://aaronparecki.com/auth/token>; rel="token_endpoint"
  84. [4] => <https://aaronparecki.com/>; rel="self"
  85. )
  86. [Date] => Fri, 28 Apr 2017 18:40:42 GMT
  87. [Strict-Transport-Security] => max-age=2592000
  88. [X-No-Cache] => 0
  89. [X-Cache] => EXPIRED
  90. )
  91. ```
  92. #### `rels`
  93. The `rels` key will be the parsed version of any HTTP `Link` headers that contain a rel value. All values will be arrays even if there is only one value.
  94. ```
  95. [rels] => Array
  96. (
  97. [hub] => Array
  98. (
  99. [0] => https://switchboard.p3k.io/
  100. )
  101. [authorization_endpoint] => Array
  102. (
  103. [0] => https://aaronparecki.com/auth
  104. )
  105. [micropub] => Array
  106. (
  107. [0] => https://aaronparecki.com/micropub
  108. )
  109. [token_endpoint] => Array
  110. (
  111. [0] => https://aaronparecki.com/auth/token
  112. )
  113. [self] => Array
  114. (
  115. [0] => https://aaronparecki.com/
  116. )
  117. )
  118. ```
  119. ### Options
  120. There are a few options you can set when making HTTP requests, to configure how the request will behave.
  121. * `$http->set_user_agent('String')` - A shortcut for setting the user agent header.
  122. * `$http->set_max_redirects(2)` - The maximum number of redirects to follow. Defaults to 8.
  123. * `$http->set_timeout(10)` - The timeout in seconds for waiting for a response. Defaults to 4.
  124. #### User Agent
  125. You can set the user agent when you first instantiate the HTTP object:
  126. ```php
  127. $http = new p3k\HTTP('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36 p3k-http/0.1.0');
  128. $http->get('http://example.com/');
  129. ```
  130. Alternately, you can change it before each request:
  131. ```php
  132. $http = new p3k\HTTP();
  133. $http->set_user_agent('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36 p3k-http/0.1.0');
  134. $http->get('http://example.com/');
  135. ```
  136. ## Transports
  137. By default, the library will use the PHP curl functions to make the HTTP request.
  138. You can optionally define your own transport to use to make HTTP requests instead. This allows you to use an existing HTTP request mechanism you may have rather than using curl or the PHP stream functions.
  139. Define a new class that implements the `p3k\HTTP\Transport` interface. That interface documents the return values expected as well.
  140. Then you can set the transport after you create the main HTTP object.
  141. ```php
  142. $http = new p3k\HTTP();
  143. $http->set_transport(new p3k\HTTP\Stream()); // or your custom class here
  144. ```
  145. The library ships with two alternative transport mechanisms, `p3k\HTTP\Stream` and `p3k\HTTP\Test`. The Stream transport uses `file_get_contents` with all the necessary config options to make it work. This is useful when running in Google App Engine. The Test transport will read HTTP responses from files, so that you can write tests that simulate making HTTP calls. See https://github.com/aaronpk/XRay/tree/master/tests for an example of using this transport.
  146. ## License
  147. Copyright 2017 by Aaron Parecki
  148. Available under the MIT license.