[PHP] PHP抓取网络数据的6种常见方法 4 →→→→→进入此内容的聊天室

来自 , 2019-07-31, 写在 PHP, 查看 115 次.
URL http://www.code666.cn/view/55d99a37
  1. // $url = 'http://yuncode.net';
  2. $url = 'http://yuncode.net:80/php/sock.php?site=yuncode.net';
  3. function get_url($url,$cookie=false)
  4. {
  5.     $url = parse_url($url);
  6.     $query = $url['path']."?".$url['query'];
  7.     echo "Query:".$query;
  8.     $fp = fsockopen( $url['host'], $url['port']?$url['port']:80 , $errno, $errstr, 30);
  9.     if (!$fp)
  10.     {
  11.         return false;
  12.     }
  13.     else {
  14.         $request = "GET $query HTTP/1.1\r\n";
  15.         $request .= "Host: $url[host]\r\n";
  16.         $request .= "Connection: Close\r\n";
  17.         if($cookie) $request.="Cookie:   $cookie\n";
  18.         $request.="\r\n";
  19.         fwrite($fp,$request);
  20.                 $result = '';
  21.         while(!feof($fp))
  22.         {
  23.             $result .= @fgets($fp, 1024);
  24.         }
  25.         fclose($fp);
  26.         return $result;
  27.     }
  28. }
  29. //获取url的html部分,去掉header
  30. function GetUrlHTML($url,$cookie=false)
  31. {
  32.     $rowdata = get_url($url,$cookie);
  33.     if($rowdata)
  34.     {
  35.         $body= stristr($rowdata,"\r\n\r\n");
  36.         $body=substr($body,4,strlen($body));
  37.         return $body;
  38.     }
  39.  
  40.     return false;
  41. }
  42.  
  43. echo get_url($url);
  44.  
  45. echo GetUrlHTML($url);
  46.  

回复 "PHP抓取网络数据的6种常见方法 4"

这儿你可以回复上面这条便签

captcha