PHP fsockopen 函数
Description
resource fsockopen ( string target [, int port [, int &errno [, string &errstr [, float timeout]]]] )
Initiates a socket connection to the resource specified by target. PHP supports targets in the Internet and Unix domains as described in 附录 N. A list of supported transports can also be retrieved using stream_get_transports().
注: If you need to set a timeout for reading/writing data over the socket, use stream_set_timeout(), as the timeout parameter to fsockopen() only applies while connecting the socket.
As of PHP 4.3.0, if you have compiled in OpenSSL support, you may prefix the hostname with either ‘ssl://’ or ‘tls://’ to use an SSL or TLS client connection over TCP/IP to connect to the remote host.
fsockopen() returns a file pointer which may be used together with the other file functions (such as fgets(), fgetss(), fwrite(), fclose(), and feof()).
If the call fails, it will return FALSE and if the optional errno and errstr arguments are present they will be set to indicate the actual system level error that occurred in the system-level connect() call. If the value returned in errno is 0 and the function returned FALSE, it is an indication that the error occurred before the connect() call. This is most likely due to a problem initializing the socket. Note that the errno and errstr arguments will always be passed by reference.
Depending on the environment, the Unix domain or the optional connect timeout may not be available.
The socket will by default be opened in blocking mode. You can switch it to non-blocking mode by using stream_set_blocking().
打开网络的 Socket 链接。
语法: int fsockopen(string hostname, int port, int [errno], string [errstr], int [timeout]);
返回值: 整数
函数种类: 网络系统
内容说明目前这个函数提供二个 Socket 资料流界面,分别为 Internet 用的 AF_INET 及 Unix 用的 AF_UNIX。当在 Internet 情形下使用时,参数 hostname 及 port 分别代表网址及埠号。在 UNIX 情形可做 IPC,hostname 参数表示到 socket 的路径,port 配置为 0。可省略的 timeout 选项表示多久没有连上就中断。在使用本函数之后会返回文件指针,供文件函数使用,包括 fgets()、fgetss()、fputs()、fclose() 与 feof()。参数 errno 及 errstr 也是可省略的,主要当做错误处理使用。使用本函数,会使用搁置模式 (blocking mode) 处理,可用 set_socket_blocking() 转换成无搁置模式。
使用范例
本例用来模拟成HTTP连接。
<?php $fp = fsockopen("www.wenan8.com", 80, &$errno, &$errstr, 10); if(!$fp) { echo "$errstr ($errno)<br>\n"; } else { fputs($fp,"GET / HTTP/1.0\nHost: www.wenan8.com\n\n"); while(!feof($fp)) { echo fgets($fp,128); } fclose($fp); } ?>

Recent Comments