HttpSocketLib.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. App::uses('HttpSocket', 'Network/Http');
  3. App::uses('CurlLib', 'Tools.Lib');
  4. /**
  5. * Wrapper for curl, php or file_get_contents with some fixes or improvements:
  6. * - All 2xx OK status codes will return the proper result
  7. * - Response will be properly utf8 encoded on WINDOWS, as well
  8. * - Timeout is reduced to 5 by default and can easily be adjusted
  9. * - file_get_contents wrapper is fixed for HTTP1.1 to default to "Connection: close"
  10. * to avoid leaving the connection open
  11. * - Caching possibilities included
  12. * - Auto-Fallback if curl is not available
  13. *
  14. * TODO: throw exceptions instead of error stuff here
  15. *
  16. * @author Mark Scherer
  17. * @license MIT
  18. * @cakephp 2.0
  19. * 2011-10-14 ms
  20. */
  21. class HttpSocketLib {
  22. // First tries with curl, then cake, then php
  23. public $use = array('curl' => true, 'cake'=> true, 'php' => true);
  24. public $debug = null;
  25. public $timeout = 5;
  26. public $cacheUsed = null;
  27. public $error = array();
  28. public function __construct($use = array()) {
  29. if (is_array($use)) {
  30. foreach ($use as $key => $value) {
  31. if (array_key_exists($key, $this->use)) {
  32. $this->use[$key] = $value;
  33. }
  34. }
  35. } elseif (array_key_exists($use, $this->use)) {
  36. $this->use[$use] = true;
  37. if ($use === 'cake') {
  38. $this->use['curl'] = false;
  39. } elseif ($use === 'php') {
  40. $this->use['curl'] = $this->use['cake'] = false;
  41. }
  42. }
  43. }
  44. /**
  45. * @param string $error
  46. */
  47. public function setError($error) {
  48. if (empty($error)) {
  49. return;
  50. }
  51. $this->error[] = $error;
  52. }
  53. /**
  54. * @return string
  55. */
  56. public function error($asString = true, $separator = ', ') {
  57. return implode(', ', $this->error);
  58. }
  59. public function reset() {
  60. $this->error = array();
  61. $this->debug = null;
  62. }
  63. /**
  64. * fetches url with curl if available
  65. * fallbacks: cake and php
  66. * note: expects url with json encoded content
  67. *
  68. * @param string $url
  69. * @param array $options
  70. * @return string Response or false on failure
  71. */
  72. public function fetch($url, $options = array()) {
  73. if (!is_array($options)) {
  74. $options = array('agent'=>$options);
  75. }
  76. $defaults = array(
  77. 'agent' => 'cakephp http socket lib',
  78. 'cache' => false,
  79. 'clearCache' => false,
  80. 'use' => $this->use,
  81. 'timeout' => $this->timeout,
  82. );
  83. $options = array_merge($defaults, $options);
  84. # cached?
  85. if ($options['cache']) {
  86. $cacheName = md5($url);
  87. $cacheConfig = $options['cache'] === true ? null: $options['cache'];
  88. $cacheConfig = !Cache::isInitialized($cacheConfig) ? null : $cacheConfig;
  89. if ($options['clearCache']) {
  90. Cache::delete('http_'.$cacheName, $cacheConfig);
  91. } elseif (($res = Cache::read('http_'.$cacheName, $cacheConfig)) !== false && $res !== null) {
  92. $this->cacheUsed = true;
  93. return $res;
  94. }
  95. }
  96. $res = $this->_fetch($url, $options);
  97. if ($options['cache']) {
  98. Cache::write('http_'.$cacheName, $res, $cacheConfig);
  99. }
  100. return $res;
  101. }
  102. /**
  103. * @param string $url
  104. * @param array $options
  105. * @return string Response or false on failure
  106. */
  107. public function _fetch($url, $options) {
  108. if ($options['use']['curl'] && function_exists('curl_init')) {
  109. $this->debug = 'curl';
  110. $Ch = new CurlLib();
  111. $Ch->setUserAgent($options['agent']);
  112. $data = $Ch->get($url);
  113. $response = $data[0];
  114. $statusCode = $data[1]['http_code'];
  115. if (!in_array($statusCode, array(200, 201, 202, 203, 204, 205, 206))) {
  116. $this->setError('Error '.$statusCode);
  117. return false;
  118. }
  119. $response = $this->_assertEncoding($response);
  120. return $response;
  121. } elseif ($options['use']['cake']) {
  122. $this->debug = 'cake';
  123. $HttpSocket = new HttpSocket(array('timeout' => $options['timeout']));
  124. $response = $HttpSocket->get($url);
  125. if (!in_array($response->code, array(200, 201, 202, 203, 204, 205, 206))) {
  126. return false;
  127. }
  128. $response = $this->_assertEncoding($response);
  129. return $response;
  130. } elseif ($options['use']['php']) {
  131. $this->debug = 'php';
  132. $opts = array(
  133. 'http' => array(
  134. 'method' => 'GET',
  135. 'header' => array('Connection: close'),
  136. 'timeout' => $options['timeout']
  137. )
  138. );
  139. if (isset($options['http'])) {
  140. $opts['http'] = array_merge($opts['http'], $options['http']);
  141. }
  142. if (is_array($opts['http']['header'])) {
  143. $opts['http']['header'] = implode(PHP_EOL, $opts['http']['header']);
  144. }
  145. $context = stream_context_create($opts);
  146. $response = file_get_contents($url, false, $context);
  147. preg_match('/^HTTP.*\s([0-9]{3})/', $http_response_header[0], $matches);
  148. $statusCode = (int)$matches[1];
  149. if (!in_array($statusCode, array(200, 201, 202, 203, 204, 205, 206))) {
  150. return false;
  151. }
  152. $response = $this->_assertEncoding($response);
  153. return $response;
  154. } else {
  155. throw new CakeException('no protocol given');
  156. }
  157. return null;
  158. }
  159. /**
  160. * It seems all three methods have encoding issues if not run through this method
  161. *
  162. * @param string $response
  163. * @param string Correctly encoded response
  164. */
  165. protected function _assertEncoding($response) {
  166. if (!WINDOWS) {
  167. return $response;
  168. }
  169. $x = mb_detect_encoding($response, 'auto', true);
  170. if ($x !== 'UTF-8') {
  171. $response = iconv(null, "utf-8", $response);
  172. }
  173. return $response;
  174. }
  175. }