HttpSocketLib.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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.x
  19. */
  20. class HttpSocketLib {
  21. // First tries with curl, then cake, then php
  22. public $use = array('curl' => true, 'cake' => true, 'php' => true);
  23. public $debug = null;
  24. public $timeout = 5;
  25. public $cacheUsed = null;
  26. public $error = array();
  27. public $allowRedirects = array(301);
  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. $allowedCodes = array_merge($this->allowRedirects, array(200, 201, 202, 203, 204, 205, 206));
  109. if ($options['use']['curl'] && function_exists('curl_init')) {
  110. $this->debug = 'curl';
  111. $Ch = new CurlLib();
  112. $Ch->setUserAgent($options['agent']);
  113. $data = $Ch->get($url);
  114. $response = $data[0];
  115. $statusCode = $data[1]['http_code'];
  116. if (!in_array($statusCode, $allowedCodes)) {
  117. $this->setError('Error ' . $statusCode);
  118. return false;
  119. }
  120. $response = $this->_assertEncoding($response);
  121. return $response;
  122. }
  123. if ($options['use']['cake']) {
  124. $this->debug = 'cake';
  125. $HttpSocket = new HttpSocket(array('timeout' => $options['timeout']));
  126. $response = $HttpSocket->get($url);
  127. if (!in_array($response->code, $allowedCodes)) {
  128. return false;
  129. }
  130. $response = $this->_assertEncoding($response);
  131. return $response;
  132. }
  133. if ($options['use']['php']) {
  134. $this->debug = 'php';
  135. $opts = array(
  136. 'http' => array(
  137. 'method' => 'GET',
  138. 'header' => array('Connection: close'),
  139. 'timeout' => $options['timeout']
  140. )
  141. );
  142. if (isset($options['http'])) {
  143. $opts['http'] = array_merge($opts['http'], $options['http']);
  144. }
  145. if (is_array($opts['http']['header'])) {
  146. $opts['http']['header'] = implode(PHP_EOL, $opts['http']['header']);
  147. }
  148. $context = stream_context_create($opts);
  149. $response = file_get_contents($url, false, $context);
  150. if (!isset($httpResponseHeader)) {
  151. return false;
  152. }
  153. preg_match('/^HTTP.*\s([0-9]{3})/', $httpResponseHeader[0], $matches);
  154. $statusCode = (int)$matches[1];
  155. if (!in_array($statusCode, $allowedCodes)) {
  156. return false;
  157. }
  158. $response = $this->_assertEncoding($response);
  159. return $response;
  160. }
  161. throw new CakeException('no protocol given');
  162. }
  163. /**
  164. * It seems all three methods have encoding issues if not run through this method
  165. *
  166. * @param string $response
  167. * @param string Correctly encoded response
  168. */
  169. protected function _assertEncoding($response) {
  170. if (!WINDOWS) {
  171. return $response;
  172. }
  173. $x = mb_detect_encoding($response, 'auto', true);
  174. if ($x !== 'UTF-8') {
  175. $response = iconv(null, "utf-8", $response);
  176. }
  177. return $response;
  178. }
  179. }