/library/ThreeScale_bak123/curl/curl.php

https://github.com/rashmigandhe/HHMobile · PHP · 144 lines · 105 code · 23 blank · 16 comment · 4 complexity · 86cfa6ad892d45b778f0cde3b9185d52 MD5 · raw file

  1. <?php
  2. # Curl, CurlResponse
  3. #
  4. # Author Sean Huber - shuber@huberry.com
  5. # Date May 2008
  6. #
  7. # A basic CURL wrapper for PHP
  8. #
  9. # See the README for documentation/examples or http://php.net/curl for more information about the libcurl extension for PHP
  10. class Curl
  11. {
  12. public $cookie_file;
  13. public $headers = array();
  14. public $options = array();
  15. public $referer = '';
  16. public $user_agent = '';
  17. protected $error = '';
  18. protected $handle;
  19. public function __construct()
  20. {
  21. $this->cookie_file = realpath('.').'/curl_cookie.txt';
  22. $this->user_agent = isset($_SERVER['HTTP_USER_AGENT']) ?
  23. $_SERVER['HTTP_USER_AGENT'] :
  24. 'Curl/PHP ' . PHP_VERSION . ' (http://github.com/shuber/curl/)';
  25. }
  26. public function delete($url, $vars = array())
  27. {
  28. return $this->request('DELETE', $url, $vars);
  29. }
  30. public function error()
  31. {
  32. return $this->error;
  33. }
  34. public function get($url, $vars = array())
  35. {
  36. if (!empty($vars)) {
  37. $url .= (stripos($url, '?') !== false) ? '&' : '?';
  38. $url .= http_build_query($vars, '', '&');
  39. }
  40. return $this->request('GET', $url);
  41. }
  42. public function post($url, $vars = array())
  43. {
  44. return $this->request('POST', $url, $vars);
  45. }
  46. public function put($url, $vars = array())
  47. {
  48. return $this->request('PUT', $url, $vars);
  49. }
  50. protected function request($method, $url, $vars = array())
  51. {
  52. $this->handle = curl_init();
  53. # Set some default CURL options
  54. curl_setopt($this->handle, CURLOPT_COOKIEFILE, $this->cookie_file);
  55. curl_setopt($this->handle, CURLOPT_COOKIEJAR, $this->cookie_file);
  56. curl_setopt($this->handle, CURLOPT_HEADER, true);
  57. curl_setopt($this->handle, CURLOPT_POSTFIELDS, (is_array($vars) ? http_build_query($vars, '', '&') : $vars));
  58. curl_setopt($this->handle, CURLOPT_REFERER, $this->referer);
  59. curl_setopt($this->handle, CURLOPT_RETURNTRANSFER, true);
  60. curl_setopt($this->handle, CURLOPT_URL, $url);
  61. curl_setopt($this->handle, CURLOPT_USERAGENT, $this->user_agent);
  62. # Format custom headers for this request and set CURL option
  63. $headers = array();
  64. foreach ($this->headers as $key => $value) {
  65. $headers[] = $key.': '.$value;
  66. }
  67. curl_setopt($this->handle, CURLOPT_HTTPHEADER, $headers);
  68. # Determine the request method and set the correct CURL option
  69. switch ($method) {
  70. case 'GET':
  71. curl_setopt($this->handle, CURLOPT_HTTPGET, true);
  72. break;
  73. case 'POST':
  74. curl_setopt($this->handle, CURLOPT_POST, true);
  75. break;
  76. default:
  77. curl_setopt($this->handle, CURLOPT_CUSTOMREQUEST, $method);
  78. }
  79. # Set any custom CURL options
  80. foreach ($this->options as $option => $value) {
  81. curl_setopt($this->handle, constant('CURLOPT_'.str_replace('CURLOPT_', '', strtoupper($option))), $value);
  82. }
  83. $response = curl_exec($this->handle);
  84. if ($response) {
  85. $response = new CurlResponse($response);
  86. } else {
  87. $this->error = curl_errno($this->handle).' - '.curl_error($this->handle);
  88. }
  89. curl_close($this->handle);
  90. return $response;
  91. }
  92. }
  93. class CurlResponse
  94. {
  95. public $body = '';
  96. public $headers = array();
  97. public function __construct($response)
  98. {
  99. # Extract headers from response
  100. $pattern = '#HTTP/\d\.\d.*?$.*?\r\n\r\n#ims';
  101. preg_match_all($pattern, $response, $matches);
  102. $headers = explode("\r\n", str_replace("\r\n\r\n", '', array_pop($matches[0])));
  103. # Extract the version and status from the first header
  104. $version_and_status = array_shift($headers);
  105. preg_match('#HTTP/(\d\.\d)\s(\d\d\d)\s(.*)#', $version_and_status, $matches);
  106. $this->headers['Http-Version'] = $matches[1];
  107. $this->headers['Status-Code'] = $matches[2];
  108. $this->headers['Status'] = $matches[2].' '.$matches[3];
  109. # Convert headers into an associative array
  110. foreach ($headers as $header) {
  111. preg_match('#(.*?)\:\s(.*)#', $header, $matches);
  112. $this->headers[$matches[1]] = $matches[2];
  113. }
  114. # Remove the headers from the response body
  115. $this->body = preg_replace($pattern, '', $response);
  116. }
  117. public function __toString()
  118. {
  119. return $this->body;
  120. }
  121. }