http_socket_lib.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * wrapper for curl,
  4. */
  5. class HttpSocketLib {
  6. // First tries with curl, then cake, then php
  7. var $use = array('curl' => true, 'cake'=> true, 'php' => true);
  8. var $debug = null;
  9. function __construct($use = array()) {
  10. if (is_array($use)) {
  11. foreach ($use as $key => $value) {
  12. if (array_key_exists($key, $this->use)) {
  13. $this->use[$key] = $value;
  14. }
  15. }
  16. } elseif (array_key_exists($use, $this->use)) {
  17. $this->use[$use] = true;
  18. if ($use == 'cake') {
  19. $this->use['curl'] = false;
  20. } elseif ($use == 'php') {
  21. $this->use['curl'] = $this->use['cake'] = false;
  22. }
  23. }
  24. }
  25. function setError($error) {
  26. if (empty($error)) {
  27. return;
  28. }
  29. $this->error[] = $error;
  30. }
  31. function error($asString = true, $separator = ', ') {
  32. return implode(', ', $this->error);
  33. }
  34. /**
  35. * fetches url with curl if available
  36. * fallbacks: cake and php
  37. * note: expects url with json encoded content
  38. * @access private
  39. **/
  40. public function fetch($url, $agent = 'cakephp http socket lib') {
  41. if ($this->use['curl'] && function_exists('curl_init')) {
  42. $this->debug = 'curl';
  43. $ch = curl_init();
  44. curl_setopt($ch, CURLOPT_URL, $url);
  45. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  46. curl_setopt($ch, CURLOPT_USERAGENT, $agent);
  47. $response = curl_exec($ch);
  48. $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  49. curl_close ($ch);
  50. if ($status != '200') {
  51. $this->setError('Error '.$status);
  52. return false;
  53. }
  54. return $response;
  55. } elseif($this->use['cake'] && App::import('Core', 'HttpSocket')) {
  56. $this->debug = 'cake';
  57. $HttpSocket = new HttpSocket(array('timeout' => 5));
  58. $response = $HttpSocket->get($url);
  59. if (empty($response)) { //TODO: status 200?
  60. return false;
  61. }
  62. return $response;
  63. } elseif($this->use['php'] || true) {
  64. $this->debug = 'php';
  65. $response = file_get_contents($url, 'r');
  66. //TODO: status 200?
  67. if (empty($response)) {
  68. return false;
  69. }
  70. return $response;
  71. }
  72. }
  73. }
  74. /*
  75. Array
  76. (
  77. [name] => 74523 Deutschland
  78. [Status] => Array
  79. (
  80. [code] => 200
  81. [request] => geocode
  82. )
  83. [Placemark] => Array
  84. (
  85. [id] => p1
  86. [address] => 74523, Deutschland
  87. [AddressDetails] => Array
  88. (
  89. [Accuracy] => 5
  90. [xmlns] => urn:oasis:names:tc:ciq:xsdschema:xAL:2.0
  91. [Country] => Array
  92. (
  93. [CountryNameCode] => DE
  94. [CountryName] => Deutschland
  95. [AdministrativeArea] => Array
  96. (
  97. [AdministrativeAreaName] => Baden-Württemberg
  98. [SubAdministrativeArea] => Array
  99. (
  100. [SubAdministrativeAreaName] => Schwäbisch Hall
  101. [PostalCode] => Array
  102. (
  103. [PostalCodeNumber] => 74523
  104. )
  105. )
  106. )
  107. )
  108. )
  109. [ExtendedData] => Array
  110. (
  111. [LatLonBox] => Array
  112. (
  113. [north] => 49.1670260
  114. [south] => 49.0451477
  115. [east] => 9.8756350
  116. [west] => 9.6132550
  117. )
  118. )
  119. [Point] => Array
  120. (
  121. [coordinates] => 9.7544127,49.1257616,0
  122. )
  123. )
  124. )
  125. */
  126. ?>