×

php 采集

获得需要session和登录的页面内容的PHP函数

天外来信 天外来信 发表于2012-07-07 01:20:36 浏览2971 评论0

抢沙发发表评论

  1. function getCookieUrl($host_url, $script_name, $method = 'GET', $data = '', $cookie_str = '')  
  2. {  
  3.     $sock = fsockopen($host_url, 80, $errno, $errstr, 30);  
  4.     if (!$sock) die("$errstr ($errno)\n");  
  5.     $method = strToUpper($method);  
  6.     $method = ($method == 'GET') ? 'GET' : 'POST';  
  7.     if (substr($script_name, 0, 1) != '/') {  
  8.         $script_name = '/' . $script_name;  
  9.     }  
  10.   
  11.     fwrite($sock, $method . " " . $script_name . " HTTP/1.0\r\n");  
  12.     fwrite($sock, "Host: " . $host_url . "\r\n");  
  13.     if (!empty($cookie_str)) {  
  14.         fwrite($sock, "COOKIE: " . $cookie_str . "\r\n");  
  15.     }  
  16.     fwrite($sock, "Content-type: application/x-www-form-urlencoded\r\n");  
  17.     if (!empty($data)) {  
  18.         fwrite($sock, "Content-length: " . strlen($data) . "\r\n");  
  19.     }  
  20.     fwrite($sock, "Accept: */*\r\n");  
  21.     fwrite($sock, "\r\n");  
  22.     if (!empty($data)) {  
  23.         fwrite($sock, "$data\r\n");  
  24.     }  
  25.     fwrite($sock, "\r\n");  
  26.   
  27.     $body = "";  
  28.     while (!feof($sock)) {  
  29.         $body .fgets($sock, 4096);  
  30.     }  
  31.     fclose($sock);  
  32.     return $body;  
  33.   
  34. }  

评论列表

访客