Posted By

ghprod on 12/17/11


Tagged


Versions (?)

cURL Wrapper


 / Published in: PHP
 

cURL Wrapper

  1. <?php
  2.  
  3. class curl
  4. {
  5.  
  6. public $options = array(
  7. CURLOPT_RETURNTRANSFER => true, // return the web page
  8. CURLOPT_HEADER => false, // don't return the headers
  9. CURLOPT_FOLLOWLOCATION => true, // follow redirects
  10. CURLOPT_USERAGENT => "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; Media Center PC 4.0)", // set a normal looking useragent
  11. CURLOPT_AUTOREFERER => true, // set referer on redirect
  12. CURLOPT_CONNECTTIMEOUT => 120, // timeout
  13. CURLOPT_TIMEOUT => 120, // timeout
  14. CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
  15. );
  16.  
  17. public function __construct()
  18. {
  19. }
  20.  
  21. public function getPage($url)
  22. {
  23.  
  24. $p = curl_init($url);
  25. curl_setopt_array($p, $this->options );
  26.  
  27. $content = curl_exec($p);
  28.  
  29. if(!$content)
  30. {
  31. throw new \Exception(curl_error($p) . ' ERROR CODE ' . curl_errno($p));
  32. }
  33.  
  34. $header = curl_getinfo($p);
  35. curl_close($p);
  36.  
  37. $header['content'] = $content;
  38.  
  39. return $header;
  40. }
  41.  
  42. public function downloadFile($url, $filePath)
  43. {
  44. $fp = fopen($filePath, 'w');
  45.  
  46. $this->options[CURLOPT_URL] = $url;
  47. $this->options[CURLOPT_FILE] = $fp;
  48.  
  49. $p = curl_init();
  50.  
  51. curl_setopt_array($p, $this->options);
  52.  
  53. $file = curl_exec($p);
  54.  
  55. curl_close($p);
  56.  
  57. fclose($fp);
  58.  
  59. }
  60.  
  61. }

Report this snippet  

You need to login to post a comment.