php几种采集远程服务器内容代码

  1. //方法一模仿用户访问网页
  2. function readpr($link,$url)
  3. {
  4. $fp = fsockopen ($url, 80, $errno, $errstr, 30);
  5. if (!$fp)
  6. {
  7. echo "$errstr ($errno) ";
  8. exit(1);
  9. }
  10. else
  11. {
  12. $out = "get $link http/1.0 ";
  13. $out .= "host: $url ";
  14. $out .= "user-agent: mozilla/4.0 (compatible; googletoolbar 2.0.114.9-big; linux 2.6) ";
  15. $out .= "connection: close ";
  16. fwrite($fp, $out);
  17. do{
  18. $line = fgets($fp, 128);
  19. }while ($line !== " ");
  20. $data = fread($fp,8192);
  21. fclose ($fp);
  22. return $data;
  23. }
  24. }
  25. //方法二用curl_init读取远程网页内容
  26. 代码如下 复制代码
  27. function init()
  28. {
  29. $ch = curl_init();
  30. curl_setopt ($ch, curlopt_url, $url);
  31. curl_setopt ($ch, curlopt_returntransfer, 1);
  32. curl_setopt ($ch, curlopt_connecttimeout, $timeout);
  33. $file_contents = curl_exec($ch);
  34. curl_close($ch);
  35. }
  36. //方法三最简单的用file_get_contents
  37. function getfiles($value)
  38. {
  39. $get_file = @file_get_contents($value);
  40. }//开源代码phpfensi.com
  41. //方法四用fopen采集远程网页内容
  42. function getfiles($value)
  43. {
  44. return fopen($value);
  45. }