curl获取远程文件内容

  1. /**
  2. 获取远程文件内容
  3. @param $url 文件http地址
  4. */
  5. function fopen_url($url)
  6. {
  7. if (function_exists('file_get_contents')) {
  8. $file_content = @file_get_contents($url);
  9. } elseif (ini_get('allow_url_fopen') && ($file = @fopen($url, 'rb'))){
  10. $i = 0;
  11. while (!feof($file) && $i++ < 1000) {
  12. $file_content .= strtolower(fread($file, 4096));
  13. }
  14. fclose($file);
  15. } elseif (function_exists('curl_init')) {
  16. $curl_handle = curl_init();
  17. curl_setopt($curl_handle, CURLOPT_URL, $url);
  18. curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT,2);
  19. curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER,1);
  20. curl_setopt($curl_handle, CURLOPT_FAILONERROR,1);
  21. curl_setopt($curl_handle, CURLOPT_USERAGENT, 'Trackback Spam Check');
  22. $file_content = curl_exec($curl_handle);
  23. curl_close($curl_handle);
  24. } else {
  25. $file_content = '';
  26. }
  27. return $file_content;
  28. }