php 如何解决[警告:fsockopen():无法连接]?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25305417/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How To Solve [Warning: fsockopen(): unable to connect]?
提问by Muhammad Hassan
I want to check that my server Ubuntu Server 14.04 LTSis online or not at my shared hosting server. My server has IP not domain but my shared hosting server has domain. For this purpose I used the below code in my shared hosting server to check that my server is online or not.
我想检查我的服务器Ubuntu Server 14.04 LTS在我的共享托管服务器上是否在线。我的服务器有 IP 不是域,但我的共享托管服务器有域。为此,我在共享托管服务器中使用了以下代码来检查我的服务器是否在线。
<?php
$site = "XX.XX.XX.XX";
$port = 7550;
$fp = fsockopen($site,$port,$errno,$errstr,10);
if ($fp === false) {
print($errno." : ".$errstr);
}
if(!$fp)
{
echo "SERVER IS DOWN";
}
else
{
echo "SERVER IS UP ON PORT ".$port." AT ".$site;
fclose($fp);
}
?>
After adding the above code in my shared hosting server php file, when I run it then I got the below error.
在我的共享托管服务器 php 文件中添加上述代码后,当我运行它时,出现以下错误。
Warning: fsockopen(): unable to connect to XX.XX.XX.XX:7550 (Connection timed out) in /home/USERNAME/public_html/index.php on line 4 110 : Connection timed out SERVER IS DOWN
Warning: fsockopen(): unable to connect to XX.XX.XX.XX:7550 (Connection timed out) in /home/USERNAME/public_html/index.php on line 4 110 : Connection timed out SERVER IS DOWN
Now somebody tell me to check that allow_url_fopen = On
is turn on in my shared hosting server or not then I checked my shared hosting server php.ini
file and there it is turn on.
现在有人告诉我检查allow_url_fopen = On
是否在我的共享托管服务器中打开,然后我检查了我的共享托管服务器php.ini
文件并在那里打开。
Now as I was looking for help all around then somebody else told me to check both(my shared hosting server and my server) have fsockopen()
ON or not. Then I wrote the below code in a php file and run on both servers.
现在,当我四处寻求帮助时,有人告诉我检查两个(我的共享托管服务器和我的服务器)是否fsockopen()
打开。然后我在一个 php 文件中编写了以下代码并在两台服务器上运行。
<?php
$fp = fsockopen ("localhost", 80, $errno, $errstr, 10);
if (!$fp) {
echo "$errstr ($errno)
\n";
}else
{
echo "fsockopen Is Working Perfectly.";
}
fclose ($fp);
?>
After running the above .php file on both servers then I got the below result same on both server.
在两台服务器上运行上述 .php 文件后,我在两台服务器上都得到了相同的结果。
fsockopen Is Working Perfectly.
fsockopen Is Working Perfectly.
Important Note: Keep in mind that my server IP remain active as I am using many PC on that IP but my server is turn off. Also I am able to open http://XX.XX.XX.XX:7550
in my Web-Browser using proxy.
重要说明:请记住,我的服务器 IP 保持活动状态,因为我在该 IP 上使用了许多 PC,但我的服务器已关闭。我也可以http://XX.XX.XX.XX:7550
使用代理在我的网络浏览器中打开。
回答by SnakeDrak
Because fsockopen
throws a E_WARNING
exception if hostname is not a valid domain. So, you use a IP with a PORT offlinethen the function throws the exception.
因为如果主机名不是有效域,则会fsockopen
引发E_WARNING
异常。因此,您使用带有 PORT离线的 IP,然后该函数会引发异常。
You can solve with handle the exception or @:
您可以通过处理异常或@来解决:
$fp = @fsockopen($site,$port,$errno,$errstr,10);
If you think that you PORT is online, check the firewall and after check with a external resouce like http://www.yougetsignal.com/tools/open-ports/.
如果您认为您的 PORT 在线,请检查防火墙,然后使用外部资源(如http://www.yougetsignal.com/tools/open-ports/)进行检查。
Read http://php.net/manual/es/function.fsockopen.php.
阅读http://php.net/manual/es/function.fsockopen.php。
UPDATED:You could test with socket_create
, it's better that fosockopen
.
更新:您可以使用 进行测试socket_create
,最好使用fosockopen
.
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$connection = @socket_connect($socket, 'XX.XX.XX.XX', 7550);
if( $connection ){
echo 'ONLINE';
}
else {
echo 'OFFLINE: ' . socket_strerror(socket_last_error( $socket ));
}
You must set a valid protocol, see: http://php.net/manual/es/function.socket-create.php.
您必须设置有效的协议,请参阅:http: //php.net/manual/es/function.socket-create.php。
回答by Torge
If you overwrote the error handler with set set_error_handler()
you need to check if error_reporting()
returns 0 which means an @ was used to suppress errors.
如果用 set 覆盖错误处理程序,则set_error_handler()
需要检查是否error_reporting()
返回 0,这意味着 @ 用于抑制错误。
set_error_handler('errorHandler');
function errorHandler($errno, $errstr, $errfile, $errline) {
if (error_reporting() === 0)
return false;
//handle error
...
}