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

来自 , 2019-07-06, 写在 PHP, 查看 109 次.
URL http://www.code666.cn/view/99ef04eb
  1. // $url = 'http://yuncode.net';
  2. $url = 'http://yuncode.net:80/php/sock.php?site=yuncode.net';
  3. function HTTP_Post($URL,$data,$cookie, $referer="")
  4. {
  5.  
  6.         // parsing the given URL
  7.     $URL_Info=parse_url($URL);
  8.  
  9.         // Building referrer
  10.     if($referer=="") // if not given use this script as referrer
  11.         $referer="yuncode.net";
  12.  
  13.         // making string from $data
  14.     foreach($data as $key=> $value)
  15.     $values[]="$key=".urlencode($value);
  16.     $data_string=implode("&",$values);
  17.  
  18.         // Find out which port is needed - if not given use standard (=80)
  19.     if(!isset($URL_Info["port"]))
  20.         $URL_Info["port"]=80;
  21.        
  22.         $request = '';
  23.         // building POST-request:
  24.     $request.="POST ".$URL_Info["path"]." HTTP/1.1\n";
  25.     $request.="Host: ".$URL_Info["host"]."\n";
  26.     $request.="Referer: $referer\n";
  27.     $request.="Content-type: application/x-www-form-urlencoded\n";
  28.     $request.="Content-length: ".strlen($data_string)."\n";
  29.     $request.="Connection: close\n";
  30.  
  31.     $request.="Cookie:   $cookie\n";
  32.  
  33.     $request.="\n";
  34.     $request.=$data_string."\n";
  35.  
  36.     $fp = fsockopen($URL_Info["host"],$URL_Info["port"]);
  37.     fputs($fp, $request);
  38.         $result = '';
  39.     while(!feof($fp))
  40.     {
  41.         $result .= fgets($fp, 1024);
  42.     }
  43.     fclose($fp);
  44.  
  45.     return $result;
  46. }
  47.  
  48. $data = array(
  49.         'foo'=>'bar',
  50.         'baz'=>'boom',
  51.         'site'=>'yuncode.net',
  52.         'name'=>'nowa magic');
  53.        
  54. $cookie = '';
  55. $referer = 'http://yuncode.net/';
  56.        
  57. echo HTTP_Post($url, $data, $cookie, $referer);
  58.  

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

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

captcha