1. $post =1; 
  2. $url = parse_url($url); 
  3. $host ='http://www.phpfensi.com'
  4. $path ='/'
  5. $query ='?action=phpfensi.com'
  6. $port =80; 
  7.   
  8. if($post) { 
  9.   $out = "post $path http/1.0 "
  10.   $out .= "accept: */* "
  11.   //$out .= "referer: $boardurl "; 
  12.   $out .= "accept-language: zh-cn "
  13.   $out .= "content-type: application/x-www-form-urlencoded "
  14.   $out .= "user-agent: $_server[http_user_agent] "
  15.   $out .= "host: $host "
  16.   $out .= 'content-length: '.strlen($post)." "
  17.   $out .= "connection: close "
  18.   $out .= "cache-control: no-cache "
  19.   $out .= "cookie: $cookie "
  20.   $out .= $post
  21.  } else { 
  22.   $out = "get $path http/1.0 "
  23.   $out .= "accept: */* "
  24.   //$out .= "referer: $boardurl "; 
  25.   $out .= "accept-language: zh-cn "
  26.   $out .= "user-agent: $_server[http_user_agent] "
  27.   $out .= "host: $host "
  28.   $out .= "connection: close "
  29.   $out .= "cookie: $cookie "
  30.  } 
  31.  $fp = @fsockopen(($ip ? $ip : $host), $port$errno$errstr$timeout); 
  32.  if(!$fp)  
  33.  { 
  34.   return '';//note $errstr : $errno  
  35.  } else { 
  36.   return '成功訪問'
  37.  }

fsockopen語法:resource fsockopen(string $hostname [,int $port = -1 [, int &$errno [,string &$errstr [, float $timeout = ini_get("default_socket_timeout") ]]]] ) 

啟動一個套接字連接到指定的主機的資源,php支持在互聯(lián)網(wǎng)領(lǐng)域的目標(biāo)和unix在所支持的套接字傳輸列表說明,所支持的傳輸列表也可以檢索使用stream_get_transports().

該插座預(yù)設(shè)會被啟用,阻塞模式,你可以切換到非阻塞模式使用stream_set_blocking(),如果上面實例看不懂,就來看個簡的吧,代碼如下:

  1. $fp = fsockopen("www.phpfensi.com", 80, $errno$errstr, 30); 
  2.  if (!$fp) { 
  3.   echo "$errstr ($errno) "
  4.  } else { 
  5.   $out = "get / http/1.1 "
  6.   $out .= "host: www.phpfensi.com"
  7.   $out .= "connection: close "
  8.   fwrite($fp$out); 
  9.   while (!feof($fp)) { 
  10.    echo fgets($fp, 128); 
  11.   } 
  12.   fclose($fp); 
  13.  }
轉(zhuǎn)載請注明來源:php fsockopen函數(shù)發(fā)送post,get請求獲得網(wǎng)頁內(nèi)容,反防采集

  哈爾濱品用軟件有限公司致力于為哈爾濱的中小企業(yè)制作大氣、美觀的優(yōu)秀網(wǎng)站,并且能夠搭建符合百度排名規(guī)范的網(wǎng)站基底,使您的網(wǎng)站無需額外費用,即可穩(wěn)步提升排名至首頁。歡迎體驗最佳的哈爾濱網(wǎng)站建設(shè)