当前位置: 移动技术网 > IT编程>开发语言>PHP > php源码 fsockopen获取网页内容实例详解

php源码 fsockopen获取网页内容实例详解

2017年12月12日  | 移动技术网IT编程  | 我要评论

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: $errno\ndesc: $errdesc\n" );
}

$request = "get $page http/1.0\r\n";
$request .= "host: $host\r\n";
$request .= "referer: http://www.manongjc.com/page.html\r\n";
$request .= "user-agent: php test client\r\n\r\n";

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





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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网