HttpSocketLib.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. App::uses('HttpSocket', 'Network/Http');
  3. App::uses('CurlLib', 'Tools.Lib');
  4. /**
  5. * Wrapper for curl, php or file_get_contents
  6. *
  7. * @author Mark Scherer
  8. * @license MIT
  9. * @cakephp 2.0
  10. * 2011-10-14 ms
  11. */
  12. class HttpSocketLib {
  13. // First tries with curl, then cake, then php
  14. public $use = array('curl' => true, 'cake'=> true, 'php' => true);
  15. public $debug = null;
  16. public $timeout = 5;
  17. public $cacheUsed = null;
  18. public $error = array();
  19. public function __construct($use = array()) {
  20. if (is_array($use)) {
  21. foreach ($use as $key => $value) {
  22. if (array_key_exists($key, $this->use)) {
  23. $this->use[$key] = $value;
  24. }
  25. }
  26. } elseif (array_key_exists($use, $this->use)) {
  27. $this->use[$use] = true;
  28. if ($use == 'cake') {
  29. $this->use['curl'] = false;
  30. } elseif ($use == 'php') {
  31. $this->use['curl'] = $this->use['cake'] = false;
  32. }
  33. }
  34. }
  35. public function setError($error) {
  36. if (empty($error)) {
  37. return;
  38. }
  39. $this->error[] = $error;
  40. }
  41. public function error($asString = true, $separator = ', ') {
  42. return implode(', ', $this->error);
  43. }
  44. public function reset() {
  45. $this->error = array();
  46. $this->debug = null;
  47. }
  48. /**
  49. * fetches url with curl if available
  50. * fallbacks: cake and php
  51. * note: expects url with json encoded content
  52. * @access private
  53. **/
  54. public function fetch($url, $options = array()) {
  55. if (!is_array($options)) {
  56. $options = array('agent'=>$options);
  57. }
  58. $defaults = array(
  59. 'agent' => 'cakephp http socket lib',
  60. 'cache' => false,
  61. 'clearCache' => false,
  62. 'use' => $this->use,
  63. 'timeout' => $this->timeout,
  64. );
  65. $options = array_merge($defaults, $options);
  66. # cached?
  67. if ($options['cache']) {
  68. $cacheName = md5($url);
  69. $cacheConfig = $options['cache'] === true ? null: $options['cache'];
  70. $cacheConfig = !Cache::isInitialized($cacheConfig) ? null : $cacheConfig;
  71. if ($options['clearCache']) {
  72. Cache::delete('http_'.$cacheName, $cacheConfig);
  73. } elseif (($res = Cache::read('http_'.$cacheName, $cacheConfig)) !== false && $res !== null) {
  74. $this->cacheUsed = true;
  75. return $res;
  76. }
  77. }
  78. $res = $this->_fetch($url, $options);
  79. if ($options['cache']) {
  80. Cache::write('http_'.$cacheName, $res, $cacheConfig);
  81. }
  82. return $res;
  83. }
  84. public function _fetch($url, $options) {
  85. if ($options['use']['curl'] && function_exists('curl_init')) {
  86. $this->debug = 'curl';
  87. $Ch = new CurlLib();
  88. $Ch->setUserAgent($options['agent']);
  89. $data = $Ch->get($url);
  90. $response = $data[0];
  91. $status = $data[1]['http_code'];
  92. if ($status != '200') {
  93. $this->setError('Error '.$status);
  94. return false;
  95. }
  96. return $response;
  97. } elseif ($options['use']['cake']) {
  98. $this->debug = 'cake';
  99. $HttpSocket = new HttpSocket(array('timeout' => $options['timeout']));
  100. $response = $HttpSocket->get($url);
  101. if ($response->code != 200) { //TODO: status 200?
  102. return false;
  103. }
  104. return $response;
  105. } elseif ($options['use']['php']) {
  106. $this->debug = 'php';
  107. $response = file_get_contents($url, 'r');
  108. //TODO: status 200?
  109. if (empty($response)) {
  110. return false;
  111. }
  112. return $response;
  113. } else {
  114. throw new CakeException('no protocol given');
  115. }
  116. return null;
  117. }
  118. }