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.

135 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. );
  59. }
  60. private function _stream_context($method, $url, $body=false, $headers=[]) {
  61. $options = [
  62. 'method' => $method,
  63. 'timeout' => $this->timeout,
  64. 'ignore_errors' => true,
  65. ];
  66. if($body) {
  67. $options['content'] = $body;
  68. }
  69. if($headers) {
  70. $options['header'] = $headers;
  71. }
  72. // Special-case appspot.com URLs to not follow redirects.
  73. // https://cloud.google.com/appengine/docs/php/urlfetch/
  74. if(should_follow_redirects($url)) {
  75. $options['follow_location'] = 1;
  76. $options['max_redirects'] = $this->max_redirects;
  77. } else {
  78. $options['follow_location'] = 0;
  79. }
  80. return stream_context_create(['http' => $options]);
  81. }
  82. public static function parse_response_code($headers) {
  83. // When a response is a redirect, we want to find the last occurrence of the HTTP code
  84. $code = false;
  85. foreach($headers as $field) {
  86. if(preg_match('/HTTP\/\d\.\d (\d+)/', $field, $match)) {
  87. $code = $match[1];
  88. }
  89. }
  90. return $code;
  91. }
  92. public static function parse_headers($headers) {
  93. $retVal = array();
  94. foreach($headers as $field) {
  95. if(preg_match('/([^:]+): (.+)/m', $field, $match)) {
  96. $match[1] = preg_replace_callback('/(?<=^|[\x09\x20\x2D])./', function($m) {
  97. return strtoupper($m[0]);
  98. }, strtolower(trim($match[1])));
  99. // If there's already a value set for the header name being returned, turn it into an array and add the new value
  100. $match[1] = preg_replace_callback('/(?<=^|[\x09\x20\x2D])./', function($m) {
  101. return strtoupper($m[0]);
  102. }, strtolower(trim($match[1])));
  103. if(isset($retVal[$match[1]])) {
  104. if(!is_array($retVal[$match[1]]))
  105. $retVal[$match[1]] = array($retVal[$match[1]]);
  106. $retVal[$match[1]][] = $match[2];
  107. } else {
  108. $retVal[$match[1]] = trim($match[2]);
  109. }
  110. }
  111. }
  112. return $retVal;
  113. }
  114. }