PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/application/model/system/NetworkFetch.php

http://github.com/integry/livecart
PHP | 67 lines | 54 code | 13 blank | 0 comment | 4 complexity | fcd82b7595277a2b50353484f6b6a00a MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php
  2. class NetworkFetch
  3. {
  4. private $url;
  5. private $tmpFile;
  6. public function __construct($url)
  7. {
  8. $this->url = $url;
  9. $this->tmpFile = ClassLoader::getRealPath('cache.') . uniqid();
  10. }
  11. public function fetch()
  12. {
  13. if (extension_loaded('curl'))
  14. {
  15. return $this->fetchWithCurl();
  16. }
  17. else if (ini_get('allow_url_fopen'))
  18. {
  19. return $this->fetchWithCopy();
  20. }
  21. }
  22. public function fetchWithCurl()
  23. {
  24. $ch = curl_init();
  25. curl_setopt($ch, CURLOPT_URL, $this->url);
  26. curl_setopt($ch, CURLOPT_REFERER, $this->url);
  27. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  28. curl_setopt($ch, CURLOPT_HEADER, false);
  29. curl_setopt($ch, CURLOPT_TIMEOUT, 2);
  30. curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.8.1.11) Gecko/20071204 Ubuntu/7.10 (gutsy) Firefox/2.0.0.11');
  31. $stream = fopen($this->tmpFile, 'w');
  32. curl_setopt($ch, CURLOPT_FILE, $stream);
  33. $res = curl_exec($ch);
  34. curl_close($ch);
  35. fclose($stream);
  36. return $res;
  37. }
  38. public function fetchWithCopy()
  39. {
  40. return @copy($this->url, $this->tmpFile);
  41. }
  42. public function getTmpFile()
  43. {
  44. return $this->tmpFile;
  45. }
  46. public function __destruct()
  47. {
  48. if (file_exists($this->tmpFile))
  49. {
  50. unlink($this->tmpFile);
  51. }
  52. }
  53. }
  54. ?>