fsockopen()函數(shù)是PHP中用于建立一個(gè)網(wǎng)絡(luò)連接的函數(shù)。它的使用方法有以下幾種:
$fp = fsockopen('www.example.com', 80, $errno, $errstr, 30);
if (!$fp) {
echo "Error: $errstr ($errno)";
} else {
// 網(wǎng)絡(luò)連接已建立,可以進(jìn)行數(shù)據(jù)傳輸操作
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");
請(qǐng)注意,使用fsockopen()函數(shù)需要確保服務(wù)器已啟用了fsockopen函數(shù),同時(shí)也要確保網(wǎng)絡(luò)連接和服務(wù)器訪問(wèn)權(quán)限。