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.

107 lines
3.3 KiB

  1. <?php
  2. namespace p3k\HTTP;
  3. class Curl implements Transport {
  4. protected $_timeout = 4;
  5. protected $_max_redirects = 8;
  6. public function set_max_redirects($max) {
  7. $this->_max_redirects = $max;
  8. }
  9. public function set_timeout($timeout) {
  10. $this->_timeout = $timeout;
  11. }
  12. public function get($url, $headers=[]) {
  13. $ch = curl_init($url);
  14. $this->_set_curlopts($ch, $url);
  15. if($headers)
  16. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  17. $response = curl_exec($ch);
  18. $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
  19. $header_str = trim(substr($response, 0, $header_size));
  20. return array(
  21. 'code' => curl_getinfo($ch, CURLINFO_HTTP_CODE),
  22. 'header' => $header_str,
  23. 'body' => substr($response, $header_size),
  24. 'error' => self::error_string_from_code(curl_errno($ch)),
  25. 'error_description' => curl_error($ch),
  26. 'url' => curl_getinfo($ch, CURLINFO_EFFECTIVE_URL),
  27. 'debug' => $response
  28. );
  29. }
  30. public function post($url, $body, $headers=[]) {
  31. $ch = curl_init($url);
  32. $this->_set_curlopts($ch, $url);
  33. curl_setopt($ch, CURLOPT_POST, true);
  34. curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
  35. if($headers)
  36. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  37. $response = curl_exec($ch);
  38. $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
  39. $header_str = trim(substr($response, 0, $header_size));
  40. return array(
  41. 'code' => curl_getinfo($ch, CURLINFO_HTTP_CODE),
  42. 'header' => $header_str,
  43. 'body' => substr($response, $header_size),
  44. 'error' => self::error_string_from_code(curl_errno($ch)),
  45. 'error_description' => curl_error($ch),
  46. 'url' => curl_getinfo($ch, CURLINFO_EFFECTIVE_URL),
  47. 'debug' => $response
  48. );
  49. }
  50. public function head($url, $headers=[]) {
  51. $ch = curl_init($url);
  52. $this->_set_curlopts($ch, $url);
  53. if($headers)
  54. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  55. curl_setopt($ch, CURLOPT_NOBODY, true);
  56. $response = curl_exec($ch);
  57. return array(
  58. 'code' => curl_getinfo($ch, CURLINFO_HTTP_CODE),
  59. 'header' => trim($response),
  60. 'error' => self::error_string_from_code(curl_errno($ch)),
  61. 'error_description' => curl_error($ch),
  62. 'url' => curl_getinfo($ch, CURLINFO_EFFECTIVE_URL),
  63. 'debug' => $response
  64. );
  65. }
  66. private function _set_curlopts($ch, $url) {
  67. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  68. curl_setopt($ch, CURLOPT_HEADER, true);
  69. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  70. curl_setopt($ch, CURLOPT_MAXREDIRS, $this->_max_redirects);
  71. curl_setopt($ch, CURLOPT_TIMEOUT_MS, round($this->_timeout * 1000));
  72. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, 2000);
  73. }
  74. public static function error_string_from_code($code) {
  75. switch($code) {
  76. case 0:
  77. return '';
  78. case CURLE_COULDNT_RESOLVE_HOST:
  79. return 'dns_error';
  80. case CURLE_COULDNT_CONNECT:
  81. return 'connect_error';
  82. case CURLE_OPERATION_TIMEDOUT:
  83. return 'timeout';
  84. case CURLE_SSL_CONNECT_ERROR:
  85. return 'ssl_error';
  86. case CURLE_SSL_CERTPROBLEM:
  87. return 'ssl_cert_error';
  88. case CURLE_SSL_CIPHER:
  89. return 'ssl_unsupported_cipher';
  90. case CURLE_SSL_CACERT:
  91. return 'ssl_cert_error';
  92. case CURLE_TOO_MANY_REDIRECTS:
  93. return 'too_many_redirects';
  94. default:
  95. return 'unknown';
  96. }
  97. }
  98. }