php源码 fsockopen获取网页内容实例详解
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获取网页内容实例详解的知识,有需要的小伙伴可以参考下,谢谢大家对本站的支持!
相关推荐
zyyjay 2020-11-09
xuebingnan 2020-11-05
samtrue 2020-11-22
stefan0 2020-11-22
yifangs 2020-10-13
songshijiazuaa 2020-09-24
hebiwtc 2020-09-18
天步 2020-09-17
83911535 2020-11-13
whatsyourname 2020-11-13
zhouyuqi 2020-11-10
Noneyes 2020-11-10
mathchao 2020-10-28
王志龙 2020-10-28
wwwsurfphpseocom 2020-10-28
diskingchuan 2020-10-23
savorTheFlavor 2020-10-23