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.

165 lines
5.2 KiB

8 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. ### Response
  32. The get/post/head functions will return an array with the following properties:
  33. * `code` - integer, the HTTP response code that was returned
  34. * `headers` - array, the HTTP headers returned
  35. * `rels` - array, the parsed HTTP rel values from any `Link` headers
  36. * `body` - string, the body of the HTTP response, or false/omit for a HEAD request
  37. * `error` - string, an error string. see below for the enumerated list.
  38. * `error_description` - string,
  39. * `url` - string, the final URL retrieved after following any redirects
  40. * `debug` - string, the full HTTP response
  41. #### `headers`
  42. 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.
  43. ```
  44. [headers] => Array
  45. (
  46. [Server] => nginx/1.12.0
  47. [Content-Type] => text/html; charset=UTF-8
  48. [Transfer-Encoding] => chunked
  49. [Connection] => keep-alive
  50. [Cache-Control] => no-cache
  51. [Link] => Array
  52. (
  53. [0] => <https://switchboard.p3k.io/>; rel="hub"
  54. [1] => <https://aaronparecki.com/auth>; rel="authorization_endpoint"
  55. [2] => <https://aaronparecki.com/micropub>; rel="micropub"
  56. [3] => <https://aaronparecki.com/auth/token>; rel="token_endpoint"
  57. [4] => <https://aaronparecki.com/>; rel="self"
  58. )
  59. [Date] => Fri, 28 Apr 2017 18:40:42 GMT
  60. [Strict-Transport-Security] => max-age=2592000
  61. [X-No-Cache] => 0
  62. [X-Cache] => EXPIRED
  63. )
  64. ```
  65. #### `rels`
  66. 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.
  67. ```
  68. [rels] => Array
  69. (
  70. [hub] => Array
  71. (
  72. [0] => https://switchboard.p3k.io/
  73. )
  74. [authorization_endpoint] => Array
  75. (
  76. [0] => https://aaronparecki.com/auth
  77. )
  78. [micropub] => Array
  79. (
  80. [0] => https://aaronparecki.com/micropub
  81. )
  82. [token_endpoint] => Array
  83. (
  84. [0] => https://aaronparecki.com/auth/token
  85. )
  86. [self] => Array
  87. (
  88. [0] => https://aaronparecki.com/
  89. )
  90. )
  91. ```
  92. ### Options
  93. There are a few options you can set when making HTTP requests, to configure how the request will behave.
  94. * `$http->set_user_agent('String')` - A shortcut for setting the user agent header.
  95. * `$http->set_max_redirects(2)` - The maximum number of redirects to follow. Defaults to 8.
  96. * `$http->set_timeout(10)` - The timeout in seconds for waiting for a response. Defaults to 4.
  97. #### User Agent
  98. You can set the user agent when you first instantiate the HTTP object:
  99. ```php
  100. $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');
  101. $http->get('http://example.com/');
  102. ```
  103. Alternately, you can change it before each request:
  104. ```php
  105. $http = new p3k\HTTP();
  106. $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');
  107. $http->get('http://example.com/');
  108. ```
  109. ## Transports
  110. By default, the library will use the PHP curl functions to make the HTTP request.
  111. 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.
  112. Define a new class that implements the `p3k\HTTP\Transport` interface. That interface documents the return values expected as well.
  113. Then you can set the transport after you create the main HTTP object.
  114. ```php
  115. $http = new p3k\HTTP();
  116. $http->set_transport(new p3k\HTTP\Stream()); // or your custom class here
  117. ```
  118. 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.
  119. ## License
  120. Copyright 2017 by Aaron Parecki
  121. Available under the MIT license.