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.

119 lines
3.9 KiB

  1. <?php
  2. namespace p3k;
  3. class HTTP {
  4. public $timeout = 4;
  5. public $max_redirects = 8;
  6. public function get($url) {
  7. $ch = curl_init($url);
  8. $this->_set_curlopts($ch, $url);
  9. $response = curl_exec($ch);
  10. $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
  11. return array(
  12. 'code' => curl_getinfo($ch, CURLINFO_HTTP_CODE),
  13. 'headers' => self::parse_headers(trim(substr($response, 0, $header_size))),
  14. 'body' => substr($response, $header_size),
  15. 'error' => self::error_string_from_code(curl_errno($ch)),
  16. 'error_description' => curl_error($ch),
  17. 'error_code' => curl_errno($ch),
  18. );
  19. }
  20. public function post($url, $body, $headers=array()) {
  21. $ch = curl_init($url);
  22. $this->_set_curlopts($ch, $url);
  23. $response = curl_exec($ch);
  24. $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
  25. return array(
  26. 'code' => curl_getinfo($ch, CURLINFO_HTTP_CODE),
  27. 'headers' => self::parse_headers(trim(substr($response, 0, $header_size))),
  28. 'body' => substr($response, $header_size),
  29. 'error' => self::error_string_from_code(curl_errno($ch)),
  30. 'error_description' => curl_error($ch),
  31. 'error_code' => curl_errno($ch),
  32. );
  33. }
  34. public function head($url) {
  35. $ch = curl_init($url);
  36. $this->_set_curlopts($ch, $url);
  37. $response = curl_exec($ch);
  38. return array(
  39. 'code' => curl_getinfo($ch, CURLINFO_HTTP_CODE),
  40. 'headers' => self::parse_headers(trim($response)),
  41. 'error' => self::error_string_from_code(curl_errno($ch)),
  42. 'error_description' => curl_error($ch),
  43. 'error_code' => curl_errno($ch),
  44. );
  45. }
  46. private function _set_curlopts($ch, $url) {
  47. $host = parse_url($url, PHP_URL_HOST);
  48. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  49. curl_setopt($ch, CURLOPT_HEADER, true);
  50. // Special-case appspot.com URLs to not follow redirects.
  51. // https://cloud.google.com/appengine/docs/php/urlfetch/
  52. if(substr($host, -12) == '.appspot.com') {
  53. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
  54. } else {
  55. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  56. curl_setopt($ch, CURLOPT_MAXREDIRS, $this->max_redirects);
  57. }
  58. curl_setopt($ch, CURLOPT_MAXREDIRS, $this->max_redirects);
  59. curl_setopt($ch, CURLOPT_TIMEOUT_MS, round($this->timeout * 1000));
  60. }
  61. public static function error_string_from_code($code) {
  62. switch($code) {
  63. case 0:
  64. return '';
  65. case CURLE_COULDNT_RESOLVE_HOST:
  66. return 'dns_error';
  67. case CURLE_COULDNT_CONNECT:
  68. return 'connect_error';
  69. case CURLE_OPERATION_TIMEDOUT:
  70. return 'timeout';
  71. case CURLE_SSL_CONNECT_ERROR:
  72. return 'ssl_error';
  73. case CURLE_SSL_CERTPROBLEM:
  74. return 'ssl_cert_error';
  75. case CURLE_SSL_CIPHER:
  76. return 'ssl_unsupported_cipher';
  77. case CURLE_SSL_CACERT:
  78. return 'ssl_cert_error';
  79. case CURLE_TOO_MANY_REDIRECTS:
  80. return 'too_many_redirects';
  81. default:
  82. return 'unknown';
  83. }
  84. }
  85. public static function parse_headers($headers) {
  86. $retVal = array();
  87. $fields = explode("\r\n", preg_replace('/\x0D\x0A[\x09\x20]+/', ' ', $headers));
  88. foreach($fields as $field) {
  89. if(preg_match('/([^:]+): (.+)/m', $field, $match)) {
  90. $match[1] = preg_replace_callback('/(?<=^|[\x09\x20\x2D])./', function($m) {
  91. return strtoupper($m[0]);
  92. }, strtolower(trim($match[1])));
  93. // If there's already a value set for the header name being returned, turn it into an array and add the new value
  94. $match[1] = preg_replace_callback('/(?<=^|[\x09\x20\x2D])./', function($m) {
  95. return strtoupper($m[0]);
  96. }, strtolower(trim($match[1])));
  97. if(isset($retVal[$match[1]])) {
  98. if(!is_array($retVal[$match[1]]))
  99. $retVal[$match[1]] = array($retVal[$match[1]]);
  100. $retVal[$match[1]][] = $match[2];
  101. } else {
  102. $retVal[$match[1]] = trim($match[2]);
  103. }
  104. }
  105. }
  106. return $retVal;
  107. }
  108. }