Home  >  Article  >  Backend Development  >  请问 php fsockopen 函数返回值

请问 php fsockopen 函数返回值

WBOY
WBOYOriginal
2016-06-13 10:50:431525browse

请教 php fsockopen 函数返回值
请教下列代码。 如何判断 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)
\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官方网站的例子。你还是把你的代码贴出来吧。

另外,你可以试试看这样,if(get_resource_type($fp) == 'stream') fwrite($fp, $out);
------解决方案--------------------
你这里不是判断了。加个退出就可以了 

if (!$fp) {
echo "$errstr ($errno)
\n";
exit;
} else {
....
------解决方案--------------------
while((!ereg('200 OK', $banreturn)) && (!ereg('302 Found', $banreturn)) && ($attemp $connection = fsockopen($hostname, $port, $errno, $errstr, $timeout);
stream_set_timeout($connection, 5);
fwrite($connection,$headers); //这里会产生错误记录,但不是每次都会产生

为什么不先检查打开是否成功就写?

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn