Home Backend Development PHP Tutorial 请问 php fsockopen 函数返回值

请问 php fsockopen 函数返回值

Jun 13, 2016 pm 01:10 PM
connection fsockopen fwrite quot

请教 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 of this Website
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve connection error How to solve connection error Nov 07, 2023 am 10:44 AM

How to solve connection error

What does CONNECTION_REFUSED mean? What does CONNECTION_REFUSED mean? Jul 31, 2023 pm 02:48 PM

What does CONNECTION_REFUSED mean?

PHP Warning: mysqli_connect(): (HY000/2002): Solution to Connection refused PHP Warning: mysqli_connect(): (HY000/2002): Solution to Connection refused Jun 23, 2023 am 08:54 AM

PHP Warning: mysqli_connect(): (HY000/2002): Solution to Connection refused

How to use fopen, fwrite and fclose in php for file operations? How to use fopen, fwrite and fclose in php for file operations? Jun 01, 2023 am 08:46 AM

How to use fopen, fwrite and fclose in php for file operations?

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决

PHP web crawler uses fsockopen to implement HTTP requests PHP web crawler uses fsockopen to implement HTTP requests Jun 17, 2023 am 11:02 AM

PHP web crawler uses fsockopen to implement HTTP requests

PHP writes a string to a file PHP writes a string to a file Mar 21, 2024 am 10:16 AM

PHP writes a string to a file

How to use Connection to manage transactions in java How to use Connection to manage transactions in java May 05, 2023 pm 08:40 PM

How to use Connection to manage transactions in java

See all articles