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.

122 lines
4.1 KiB

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