这里有新鲜出炉的 PHP 教程,程序狗速度看过来!
PHP(外文名: Hypertext Preprocessor,中文名:"超文本预处理器")是一种通用开源脚本语言。语法吸收了 C 语言、Java 和 Perl 的特点,入门门槛较低,易于学习,使用广泛,主要适用于 web 开发领域。PHP 的文件后缀名为 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 获取网页内容
具体源代码如下:
- <?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 获取网页内容实例详解的知识,有需要的小伙伴可以参考下,谢谢大家对本站的支持!
来源: http://www.phperz.com/article/17/0802/342519.html