导航首页 » 技术教程 » php源码 fsockopen获取网页内容实例详解
php源码 fsockopen获取网页内容实例详解 157 2024-01-25   

PHP fsockopen函数说明:

Open Internet or Unix domain socket connection(打开套接字链接)

Initiates a socket connection to the resource specified by target .

fsockopen() returns a file pointer which may be used together with the other file functions (such as fgets() , fgetss() , fwrite() , fclose() , and feof() ).就是返回一个文件句柄

开启PHP fsockopen这个函数

PHP fsockopen需要 PHP.ini 中 allow_url_fopen 选项开启。

使用fsockopen获取网页内容

具体源代码如下:

<?php
$host = "www.manongjc.com";
$page = "/index.htm";
$fp = fsockopen( "$host", 80, $errno, $errdesc );
if ( ! $fp ) {
 die ( "Couldn't connect to $host:nError: $errnonDesc: $errdescn" );
}

$request = "GET $page HTTP/1.0rn";
$request .= "Host: $hostrn";
$request .= "Referer: http://www.manongjc.com/page.htmlrn";
$request .= "User-Agent: PHP test clientrnrn";

$page = array();
fputs ( $fp, $request );
while ( ! feof( $fp ) ) {
 $page[] = fgets( $fp, 1024 );
}
fclose( $fp );
print "the server returned ".(count($page))." lines!";
?>





以上就是php源码 fsockopen获取网页内容实例详解的知识,有需要的小伙伴可以参考下,谢谢大家对本站的支持!


!!!站长长期在线接!!!

网站、小程序:定制开发/二次开发/仿制开发等

各种疑难杂症解决/定制接口/定制采集等

站长微信:lxwl520520

站长QQ:1737366103