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.

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