转自: http://blog.sina.com.cn/s/blog_7e7249c30100sb74.html
开网络的 Socket 链接。
语法: resuce fsockopen(string hostname, int port, int [errno], string
[errstr], int [timeout]);
返回值: 资源;
函数种类: 网络系统;
内容说明:
目前这个函数提供两个 Socket 资料流界面,分别为 Internet 用的 AF_INET 及 Unix 用的
AF_UNIX。
在 Internet
中,参数 hostname 及 port 分别代表网址及端口号。
在 UNIX 中,参数hostname 表示到 socket 的路径,port 配置为 0。timeout可省略表示多久没有连
上就中断。该函数返回文件指针,供文件函数使用,包括 fgets()、fgetss()、fputs()、fclose()
feof()。参数 errno 及 errstr 可省略,做错误处理使用。该函数使用阻塞模式 (blocking mode) 处理,
可用 set_socket_blocking() 转换成无阻塞模式。
实例:
<?php
$fp = fsockopen("php.wilson.gs", 80, &$errno, &$errstr, 10);
if(!$fp) {
echo "$errstr ($errno)<br>\n";
} else {
fputs($fp,"GET / HTTP/1.0\nHost: php.wilson.gs\n\n");
while(!feof($fp)) {
echo fgets($fp,128);
}
fclose($fp);
}
?>
备注:fputs() 函数写入文件(可安全用于二进制文件)。fputs() 函数是 fwrite() 函数的别名。
在 UNIX 中,参数hostname 表示到 socket 的路径,port 配置为 0。timeout可省略表示多久没有连
上就中断。该函数返回文件指针,供文件函数使用,包括 fgets()、fgetss()、fputs()、fclose()
feof()。参数 errno 及 errstr 可省略,做错误处理使用。该函数使用阻塞模式 (blocking mode) 处理,
可用 set_socket_blocking() 转换成无阻塞模式。
实例:
<?php
$fp = fsockopen("php.wilson.gs", 80, &$errno, &$errstr, 10);
if(!$fp) {
echo "$errstr ($errno)<br>\n";
} else {
fputs($fp,"GET / HTTP/1.0\nHost: php.wilson.gs\n\n");
while(!feof($fp)) {
echo fgets($fp,128);
}
fclose($fp);
}
?>
备注:fputs() 函数写入文件(可安全用于二进制文件)。fputs() 函数是 fwrite() 函数的别名。