Basic cURL wrapper function for PHP


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. /**
  2.  * Basic cURL wrapper function for PHP
  3.  * @link http://snipplr.com/view/51161/basic-curl-wrapper-function-for-php/
  4.  * @param string $url URL to fetch
  5.  * @param array $curlopt Array of options for curl_setopt_array
  6.  * @return string
  7.  */
  8. function file_get_contents_curl($url, $curlopt = array()){
  9. $ch = curl_init();
  10. $default_curlopt = array(
  11. CURLOPT_TIMEOUT => 2,
  12. CURLOPT_RETURNTRANSFER => 1,
  13. CURLOPT_FOLLOWLOCATION => 1,
  14. CURLOPT_USERAGENT => "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.13) Gecko/20101203 AlexaToolbar/alxf-1.54 Firefox/3.6.13 GTB7.1"
  15. );
  16. $curlopt = array(CURLOPT_URL => $url) + $curlopt + $default_curlopt;
  17. curl_setopt_array($ch, $curlopt);
  18. $response = curl_exec($ch);
  19. if($response === false)
  20. curl_close($ch);
  21. return $response;
  22. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.