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.

120 lines
4.0 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. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  52. curl_setopt($ch, CURLOPT_HEADER, true);
  53. // Special-case appspot.com URLs to not follow redirects.
  54. // https://cloud.google.com/appengine/docs/php/urlfetch/
  55. if(should_follow_redirects($url)) {
  56. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  57. curl_setopt($ch, CURLOPT_MAXREDIRS, $this->max_redirects);
  58. } else {
  59. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
  60. }
  61. curl_setopt($ch, CURLOPT_TIMEOUT_MS, round($this->timeout * 1000));
  62. }
  63. public static function error_string_from_code($code) {
  64. switch($code) {
  65. case 0:
  66. return '';
  67. case CURLE_COULDNT_RESOLVE_HOST:
  68. return 'dns_error';
  69. case CURLE_COULDNT_CONNECT:
  70. return 'connect_error';
  71. case CURLE_OPERATION_TIMEDOUT:
  72. return 'timeout';
  73. case CURLE_SSL_CONNECT_ERROR:
  74. return 'ssl_error';
  75. case CURLE_SSL_CERTPROBLEM:
  76. return 'ssl_cert_error';
  77. case CURLE_SSL_CIPHER:
  78. return 'ssl_unsupported_cipher';
  79. case CURLE_SSL_CACERT:
  80. return 'ssl_cert_error';
  81. case CURLE_TOO_MANY_REDIRECTS:
  82. return 'too_many_redirects';
  83. default:
  84. return 'unknown';
  85. }
  86. }
  87. public static function parse_headers($headers) {
  88. $retVal = array();
  89. $fields = explode("\r\n", preg_replace('/\x0D\x0A[\x09\x20]+/', ' ', $headers));
  90. foreach($fields as $field) {
  91. if(preg_match('/([^:]+): (.+)/m', $field, $match)) {
  92. $match[1] = preg_replace_callback('/(?<=^|[\x09\x20\x2D])./', function($m) {
  93. return strtoupper($m[0]);
  94. }, strtolower(trim($match[1])));
  95. // If there's already a value set for the header name being returned, turn it into an array and add the new value
  96. $match[1] = preg_replace_callback('/(?<=^|[\x09\x20\x2D])./', function($m) {
  97. return strtoupper($m[0]);
  98. }, strtolower(trim($match[1])));
  99. if(isset($retVal[$match[1]])) {
  100. if(!is_array($retVal[$match[1]]))
  101. $retVal[$match[1]] = array($retVal[$match[1]]);
  102. $retVal[$match[1]][] = $match[2];
  103. } else {
  104. $retVal[$match[1]] = trim($match[2]);
  105. }
  106. }
  107. }
  108. return $retVal;
  109. }
  110. }