当前位置:Gxlcms > PHP教程 > PHP经典抓取网络数据方法index.phpforum.phpphpno

PHP经典抓取网络数据方法index.phpforum.phpphpno

时间:2021-07-01 10:21:17 帮助过:24人阅读

1、file_get_contents

以get的方式获取数据

  1. $url = 'blog.csdn.net/guugle2010';
  2. $html = file_get_contents($url);
  3. echo $html;

以post方式获取数据

  1. $data = array(
  2. 'name' => 'guugle',
  3. 'blog' => 'blog.csdn.net/guugle2010'
  4. );
  5. $data = http_build_query($data);
  6. $options = array(
  7. 'http' => array(
  8. 'method' => 'POST',
  9. 'header' => 'Content-type:application/x-www-form-urlencode',
  10. 'content' => $data
  11. )
  12. );
  13. $url = "http://localhost/test.php";
  14. $context = stream_context_create($options);
  15. $result = file_get_contents($url, false, $context);
  16. echo $result;
2、fopen方式
  1. $url = 'http://blog.csdn.net/guugle2010';
  2. $handle = fopen($url, r);
  3. $html = '';
  4. while(!feof($handle)){
  5. $html .= fgets($handle);
  6. }
  7. echo $html;
  8. fclose($handle);
3、curl库,需要打开curl扩展
  1. $url = 'http://blog.csdn.net/guugle2010';
  2. $ch = curl_init();
  3. $timeout = 5;
  4. curl_setopt ($ch, CURLOPT_URL, $url);
  5. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  6. curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  7. $file_contents = curl_exec($ch);
  8. curl_close($ch);
  9. echo $file_contents;

4、用fsocketopen打开链接

以get的方式获取完整的数据(包括header和body)

  1. $url = 'http://blog.csdn.net/guugle2010';
  2. function get_url($url,$cookie=false)
  3. {
  4. $url = parse_url($url);
  5. $query = $url['path']."?".$url['query'];
  6. echo "Query:".$query;
  7. $fp = fsockopen( $url['host'], $url['port']?$url['port']:80 , $errno, $errstr, 30);
  8. if (!$fp)
  9. {
  10. return false;
  11. }
  12. else {
  13. $request = "GET $query HTTP/1.1\r\n";
  14. $request .= "Host: $url[host]\r\n";
  15. $request .= "Connection: Close\r\n";
  16. if($cookie) $request.="Cookie: $cookie\n";
  17. $request.="\r\n";
  18. fwrite($fp,$request);
  19. $result = '';
  20. while(!feof($fp))
  21. {
  22. $result .= @fgets($fp, 1024);
  23. }
  24. fclose($fp);
  25. return $result;
  26. }
  27. }
  28. //获取url的html部分,去掉header
  29. function GetUrlHTML($url,$cookie=false)
  30. {
  31. $rowdata = get_url($url,$cookie);
  32. if($rowdata)
  33. {
  34. $body= stristr($rowdata,"\r\n\r\n");
  35. $body=substr($body,4,strlen($body));
  36. return $body;
  37. }
  38. return false;
  39. }
  40. echo get_url($url);
  41. echo GetUrlHTML($url);

以post方式获取完整数据(包括header和body)
  1. $url = 'http://blog.csdn.net/guugle2010';
  2. function HTTP_Post($URL,$data,$cookie, $referer="")
  3. {
  4. // parsing the given URL
  5. $URL_Info=parse_url($URL);
  6. // Building referrer
  7. if($referer=="") // if not given use this script as referrer
  8. $referer="<span style="font-family: Arial, Helvetica, sans-serif;">blog.csdn.net</span><span style="font-family: Arial, Helvetica, sans-serif;">";</span>
  9. // making string from $data
  10. foreach($data as $key=> $value)
  11. $values[]="$key=".urlencode($value);
  12. $data_string=implode("&",$values);
  13. // Find out which port is needed - if not given use standard (=80)
  14. if(!isset($URL_Info["port"]))
  15. $URL_Info["port"]=80;
  16. $request = '';
  17. // building POST-request:
  18. $request.="POST ".$URL_Info["path"]." HTTP/1.1\n";
  19. $request.="Host: ".$URL_Info["host"]."\n";
  20. $request.="Referer: $referer\n";
  21. $request.="Content-type: application/x-www-form-urlencoded\n";
  22. $request.="Content-length: ".strlen($data_string)."\n";
  23. $request.="Connection: close\n";
  24. $request.="Cookie: $cookie\n";
  25. $request.="\n";
  26. $request.=$data_string."\n";
  27. $fp = fsockopen($URL_Info["host"],$URL_Info["port"]);
  28. fputs($fp, $request);
  29. $result = '';
  30. while(!feof($fp))
  31. {
  32. $result .= fgets($fp, 1024);
  33. }
  34. fclose($fp);
  35. return $result;
  36. }
  37. $data = array(
  38. 'site'=>'<span style="font-family: Arial, Helvetica, sans-serif;">blog.csdn.net/guugle2010</span><span style="font-family: Arial, Helvetica, sans-serif;">', </span>
  39. 'name'=>'guugle');
  40. $cookie = '';
  41. $referer = 'http://blog.csdn.net/';
  42. echo HTTP_Post($url, $data, $cookie, $referer);

以上就介绍了PHP经典抓取网络数据方法,包括了php,网络方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

人气教程排行