fsockopen()函数是PHP中用于建立一个网络连接的函数。它的使用方法有以下几种:
基本用法:$fp = fsockopen('www.example.com', 80, $errno, $errstr, 30);if (!$fp) { echo "Error: $errstr ($errno)";} else { // 网络连接已建立,可以进行数据传输操作 fwrite($fp, "GET / HTTP/1.0\r\nHost: www.example.com\r\n\r\n"); while (!feof($fp)) { echo fgets($fp, 128); } fclose($fp);}指定协议和端口:$fp = fsockopen('ssl://www.example.com', 443, $errno, $errstr, 30);设置超时时间:$fp = fsockopen('www.example.com', 80, $errno, $errstr, 30);stream_set_timeout($fp, 30);使用代理连接:$proxy_fp = fsockopen('proxy.example.com', 8080, $errno, $errstr, 30);$fp = fsockopen('www.example.com', 80, $errno, $errstr, 30, $proxy_fp);使用用户名和密码进行认证:$fp = fsockopen('www.example.com', 80, $errno, $errstr, 30);fwrite($fp, "GET / HTTP/1.0\r\nAuthorization: Basic " . base64_encode('username:password') . "\r\n\r\n");请注意,使用fsockopen()函数需要确保服务器已启用了fsockopen函数,同时也要确保网络连接和服务器访问权限。