国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PHP > 正文

php中fsockopen采集網(wǎng)頁內(nèi)容實(shí)例

2024-05-04 21:48:34
字體:
供稿:網(wǎng)友

fsockopen是php中一個比較實(shí)用的函數(shù)了,下面我來介紹利用fsockopen函數(shù)來采集網(wǎng)頁的程序.

用法:int fsockopen(string hostname,int port,int [errno],string [errstr],int [timeout]);

一個采集網(wǎng)頁實(shí)例,代碼如下:

  1. <?php 
  2. function get_url ($url,$cookie=false) 
  3. $url = parse_url($url); 
  4. $query = $url[path].”?”.$url[query]; 
  5. echo “Query:”.$query
  6. $fp = fsockopen$url[host], $url[port]?$url[port]:80 , $errno$errstr, 30); 
  7. if (!$fp) { 
  8. return false; 
  9. else { 
  10. $request = “GET $query HTTP/1.1rn”; 
  11. $request .= “Host: $url[host]rn”; 
  12. $request .= “Connection: Closern”; 
  13. if($cookie$request.=”Cookie:   $cookien”; 
  14. $request.=”rn”; 
  15. fwrite($fp,$request); 
  16. while(!@feof($fp)) { 
  17. $result .= @fgets($fp, 1024); 
  18. fclose($fp); 
  19. return $result
  20. //獲取url的html部分,去掉header 
  21. function GetUrlHTML($url,$cookie=false) 
  22. $rowdata = get_url($url,$cookie); 
  23. if($rowdata)//開源代碼Vevb.com 
  24. $bodystristr($rowdata,”rnrn”); 
  25. $body=substr($body,4,strlen($body)); 
  26. return $body
  27.  
  28.     return false; 
  29. ?> 

被禁用后的解決方法:

服務(wù)器同時禁用了fsockopen pfsockopen,那么用其他函數(shù)代替,如stream_socket_client(),注意:stream_socket_client()和fsockopen()的參數(shù)不同.

fsockopen:替換為 stream_socket_client,然后,將原fsockopen函數(shù)中的端口參數(shù)“80”刪掉,并加到$host.

例,代碼如下:

  1. $fp = fsockopen($host, 80, $errno$errstr, 30); 
  2. //或 
  3. $fp = fsockopen($host$port$errno$errstr$connection_timeout); 
  4. //修改后: 
  5. $fp = stream_socket_client("tcp://".$host."80"$errno$errstr, 30); 
  6. //或 
  7. $fp = stream_socket_client("tcp://".$host.":".$port$errno$errstr$connection_timeout);

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 平原县| 吉林市| 苍溪县| 林州市| 乡城县| 白城市| 浦县| 灵宝市| 广德县| 景泰县| 清镇市| 宝鸡市| 儋州市| 通海县| 岑巩县| 松滋市| 黄大仙区| 和硕县| 合江县| 永和县| 六盘水市| 遵化市| 南宫市| 会昌县| 彭山县| 班玛县| 嵊州市| 治县。| 西充县| 肥城市| 明水县| 新河县| 许昌县| 当涂县| 墨竹工卡县| 阳曲县| 治县。| 平远县| 沾化县| 鸡西市| 平罗县|