服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - PHP教程 - php源码 fsockopen获取网页内容实例详解

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

2021-03-03 16:29PHP教程网 PHP教程

这篇文章主要介绍了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获取网页内容

具体源代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?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.zzvips.com/tags.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获取网页内容实例详解的知识,有需要的小伙伴可以参考下,谢谢大家对本站的支持!

延伸 · 阅读

精彩推荐