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.

136 lines
4.0 KiB

  1. <?php
  2. namespace p3k;
  3. class HTTPStream {
  4. public $timeout = 4;
  5. public $max_redirects = 8;
  6. public static function exception_error_handler($severity, $message, $file, $line) {
  7. if (!(error_reporting() & $severity)) {
  8. // This error code is not included in error_reporting
  9. return;
  10. }
  11. throw new \ErrorException($message, 0, $severity, $file, $line);
  12. }
  13. public function get($url) {
  14. set_error_handler("p3k\HTTPStream::exception_error_handler");
  15. $context = $this->_stream_context('GET', $url);
  16. return $this->_fetch($url, $context);
  17. }
  18. public function post($url, $body, $headers=array()) {
  19. set_error_handler("p3k\HTTPStream::exception_error_handler");
  20. $context = $this->_stream_context('POST', $url, $body, $headers);
  21. return $this->_fetch($url, $context);
  22. }
  23. public function head($url) {
  24. set_error_handler("p3k\HTTPStream::exception_error_handler");
  25. $context = $this->_stream_context('HEAD', $url);
  26. return $this->_fetch($url, $context);
  27. }
  28. private function _fetch($url, $context) {
  29. $error = false;
  30. try {
  31. $body = file_get_contents($url, false, $context);
  32. } catch(\Exception $e) {
  33. $body = false;
  34. $http_response_header = [];
  35. $description = str_replace('file_get_contents(): ', '', $e->getMessage());
  36. $code = 'unknown';
  37. if(preg_match('/getaddrinfo failed/', $description)) {
  38. $code = 'dns_error';
  39. $description = str_replace('php_network_getaddresses: ', '', $description);
  40. }
  41. if(preg_match('/timed out|request failed/', $description)) {
  42. $code = 'timeout';
  43. }
  44. if(preg_match('/certificate/', $description)) {
  45. $code = 'ssl_error';
  46. }
  47. $error = [
  48. 'description' => $description,
  49. 'code' => $code
  50. ];
  51. }
  52. return array(
  53. 'code' => self::parse_response_code($http_response_header),
  54. 'headers' => self::parse_headers($http_response_header),
  55. 'body' => $body,
  56. 'error' => $error ? $error['code'] : false,
  57. 'error_description' => $error ? $error['description'] : false,
  58. 'url' => $url,
  59. );
  60. }
  61. private function _stream_context($method, $url, $body=false, $headers=[]) {
  62. $options = [
  63. 'method' => $method,
  64. 'timeout' => $this->timeout,
  65. 'ignore_errors' => true,
  66. ];
  67. if($body) {
  68. $options['content'] = $body;
  69. }
  70. if($headers) {
  71. $options['header'] = $headers;
  72. }
  73. // Special-case appspot.com URLs to not follow redirects.
  74. // https://cloud.google.com/appengine/docs/php/urlfetch/
  75. if(should_follow_redirects($url)) {
  76. $options['follow_location'] = 1;
  77. $options['max_redirects'] = $this->max_redirects;
  78. } else {
  79. $options['follow_location'] = 0;
  80. }
  81. return stream_context_create(['http' => $options]);
  82. }
  83. public static function parse_response_code($headers) {
  84. // When a response is a redirect, we want to find the last occurrence of the HTTP code
  85. $code = false;
  86. foreach($headers as $field) {
  87. if(preg_match('/HTTP\/\d\.\d (\d+)/', $field, $match)) {
  88. $code = $match[1];
  89. }
  90. }
  91. return $code;
  92. }
  93. public static function parse_headers($headers) {
  94. $retVal = array();
  95. foreach($headers as $field) {
  96. if(preg_match('/([^:]+): (.+)/m', $field, $match)) {
  97. $match[1] = preg_replace_callback('/(?<=^|[\x09\x20\x2D])./', function($m) {
  98. return strtoupper($m[0]);
  99. }, strtolower(trim($match[1])));
  100. // If there's already a value set for the header name being returned, turn it into an array and add the new value
  101. $match[1] = preg_replace_callback('/(?<=^|[\x09\x20\x2D])./', function($m) {
  102. return strtoupper($m[0]);
  103. }, strtolower(trim($match[1])));
  104. if(isset($retVal[$match[1]])) {
  105. if(!is_array($retVal[$match[1]]))
  106. $retVal[$match[1]] = array($retVal[$match[1]]);
  107. $retVal[$match[1]][] = $match[2];
  108. } else {
  109. $retVal[$match[1]] = trim($match[2]);
  110. }
  111. }
  112. }
  113. return $retVal;
  114. }
  115. }