请教下列代码。 如何判断 fsockopen 连接 成功了?因为如果失败后 fwrite 就会在我空间里产生大量的错误信息
PHP Warning: fwrite(): supplied argument is not a valid stream resource in /home2/我想加入判断,如果 fsockopen 失败就跳过,避免错误执行 fwrite谢谢大家! 请帮忙$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$out = "GET / HTTP/1.1\r\n";
$out .= "Host: www.example.com\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
while (!feof($fp)) {
echo fgets($fp, 128);
}
fclose($fp);
}
PHP Warning: fwrite(): supplied argument is not a valid stream resource in /home2/我想加入判断,如果 fsockopen 失败就跳过,避免错误执行 fwrite谢谢大家! 请帮忙$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$out = "GET / HTTP/1.1\r\n";
$out .= "Host: www.example.com\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
while (!feof($fp)) {
echo fgets($fp, 128);
}
fclose($fp);
}
echo "$errstr ($errno)<br />\n";
exit;
} else {
....
function checkurl($url, $se=0) { $checkbanurl = "http://baidu.com"; $serveronline = 1; if (!ereg('http://', $checkbanurl)) { $serveronline = 0; } else { if (ereg('\"', $checkbanurl) || ereg('\'', $checkbanurl) || $checkbanurl == "http://") { $serveronline = 0; } else { $urlinfo = parse_url($checkbanurl); $hostname = $urlinfo['host']; if ($hostname == "" || gethostbyname($hostname) == $hostname) { $serveronline = 0; } else { $port = 80; $timeout = 5; $headers = "GET ".$checkbanurl." HTTP/1.0\r\n"; $headers .= "Host: ".$hostname."\r\n"; $headers .= "Referer: http://".$_SERVER["SERVER_NAME"]."\r\n"; $headers .= "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)\r\n"; $headers .= "Connection: close\r\n"; $headers .= "Accept: */*\r\n"; $headers .= "\r\n"; $banreturn = ""; $attempts = 0; while((!ereg('200 OK', $banreturn)) && (!ereg('302 Found', $banreturn)) && ($attemp<2)) { $connection = fsockopen($hostname, $port, $errno, $errstr, $timeout); stream_set_timeout($connection, 5); fwrite($connection,$headers); //这里会产生错误记录,但不是每次都会产生 $banreturn = ""; while (!feof($connection) && !$checktimeout['timed_out']) { $banreturn .= fread($connection, 10000); $checktimeout = stream_get_meta_data($connection); } fclose($connection);
$attemp = $attemp+1; } } } } return false;}
$connection = fsockopen($hostname, $port, $errno, $errstr, $timeout);
stream_set_timeout($connection, 5);
fwrite($connection,$headers); //这里会产生错误记录,但不是每次都会产生为什么不先检查打开是否成功就写?