PHP fsockopen 不返回任何内容

发布于 2024-09-03 01:31:28 字数 489 浏览 8 评论 0原文

我正在修改 redis 数据库的 PHP db 包装器。

我的函数如下所示:

public function connect() {

    $sock = @fsockopen('localhost', '6379',  $errno, $errstr, 2);

    if ($sock === FALSE) {
        return FALSE;
    }
    else {
        stream_set_timeout($sock, 2); 
        return $sock;
    }
}

我想要做的是从包装器中的另一部分调用此函数:

 if ($this->connect() !== FALSE) {
      // Do stuff
 }

当 fsockopen 不工作时,如何让我的 connect 函数发送 FALSE?

谢谢!

I am modifying a PHP db wrapper for the redis database.

Here's how my function looks:

public function connect() {

    $sock = @fsockopen('localhost', '6379',  $errno, $errstr, 2);

    if ($sock === FALSE) {
        return FALSE;
    }
    else {
        stream_set_timeout($sock, 2); 
        return $sock;
    }
}

What I want to do is to call this function from another part in my wrapper:

 if ($this->connect() !== FALSE) {
      // Do stuff
 }

How can I get my connect function to send a FALSE when the fsockopen isn't working?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

半山落雨半山空 2024-09-10 01:31:28

fsockopen() 页面向下一点(必须滚动到几乎到底部):

即使远程主机无法访问,UDP 套接字有时也会显示已打开且没有错误。仅当您从套接字读取或写入数据时,该错误才会变得明显。这样做的原因是因为UDP是一种“无连接”协议,这意味着操作系统在实际需要发送或接收数据之前不会尝试为套接字建立链接。

我猜这是你的问题,我猜你必须进行测试读/写,看看它是否真的成功。

From a little ways down the fsockopen() page (have to scroll almost to the bottom):

UDP sockets will sometimes appear to have opened without an error, even if the remote host is unreachable. The error will only become apparent when you read or write data to/from the socket. The reason for this is because UDP is a "connectionless" protocol, which means that the operating system does not try to establish a link for the socket until it actually needs to send or receive data.

I'm going to guess that's your problem, I guess you have to do a test read/write to see if it was really successful or not.

墟烟 2024-09-10 01:31:28

尝试以下代码并查看是否按预期工作:

public function connect()
{
    $sock = @fsockopen('localhost', '6379',  $errno, $errstr, 2);

    if (!is_resource($sock))
        return FALSE;

    stream_set_timeout($sock, 2); 
    return $sock;
}

Try the following code and see if this works as intended:

public function connect()
{
    $sock = @fsockopen('localhost', '6379',  $errno, $errstr, 2);

    if (!is_resource($sock))
        return FALSE;

    stream_set_timeout($sock, 2); 
    return $sock;
}
看海 2024-09-10 01:31:28
@fsockopen

你的函数前面有一个@,这将抑制错误。如果错误导致零回报,您将不会得到任何结果。删除 @ 并记录或显示任何产生的错误或警告。

@fsockopen

You've got an @ in front of your function, which is going to suppress errors. If the error is causing zero return, you're not going to get anything. Remove the @ and log or display any resulting errors or warnings.

许久 2024-09-10 01:31:28

我知道现在回答可能已经太晚了。
但是,fsockopen 有点对“localhost”有问题..
尝试使用“127.0.0.1”nim 确保它可以连接。 ;)

I know it might be way late to answer.
But, fsockopen kinda has a problem with 'localhost'..
try using '127.0.0.1' n i m sure it will connect. ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文