【问题标题】:is there function too pause php code execution instead sleep()?是否有函数也暂停 php 代码执行而不是 sleep()?
【发布时间】:2021-06-04 07:53:17
【问题描述】:

我有 api 来管理网络设备和类似的东西

我的代码

public function connect($ip, $login, $password)
{
    for ($ATTEMPT = 1; $ATTEMPT <= $this->attempts; $ATTEMPT++) {
        $this->connected = false;
        $PROTOCOL = ($this->ssl ? 'ssl://' : '' );
        $context = stream_context_create(array('ssl' => array('ciphers' => 'ADH:ALL', 'verify_peer' => false, 'verify_peer_name' => false)));
        $this->debug('Connection attempt #' . $ATTEMPT . ' to ' . $PROTOCOL . $ip . ':' . $this->port . '...');
        $this->socket = @stream_socket_client($PROTOCOL . $ip.':'. $this->port, $this->error_no, $this->error_str, $this->timeout, STREAM_CLIENT_CONNECT,$context);
        if ($this->socket) {
            socket_set_timeout($this->socket, $this->timeout);
            $this->write('/login', false);
            $this->write('=name=' . $login, false);
            $this->write('=password=' . $password);
            $RESPONSE = $this->read(false);
            if (isset($RESPONSE[0])) {
                if ($RESPONSE[0] == '!done') {
                    if (!isset($RESPONSE[1])) {
                        // Login method post-v6.43
                        $this->connected = true;
                        break;
                    } else {
                        // Login method pre-v6.43
                        $MATCHES = array();
                        if (preg_match_all('/[^=]+/i', $RESPONSE[1], $MATCHES)) {
                            if ($MATCHES[0][0] == 'ret' && strlen($MATCHES[0][1]) == 32) {
                                $this->write('/login', false);
                                $this->write('=name=' . $login, false);
                                $this->write('=password=' . $password);
                                $RESPONSE = $this->read(false);
                                if (isset($RESPONSE[0]) && $RESPONSE[0] == '!done') {
                                    $this->connected = true;
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            fclose($this->socket);
        }
        sleep($this->delay);
    }

    if ($this->connected) {
        $this->debug('Connected...');
    } else {
        $this->debug('Error...');
    }
    return $this->connected;
}

出于安全原因,在托管服务中阻塞了 sleep() 功能,我希望使用它来连接设备。现在的问题是有什么函数可以在 php 中使用它而不是 sleep() 函数吗? 提供任何帮助

【问题讨论】:

    标签: php sleep


    【解决方案1】:

    你可以试试usleep()。但是,我不知道您的托管服务是否也阻止了 usleep()。

    【讨论】:

      猜你喜欢
      • 2016-04-11
      • 2021-06-17
      • 2016-01-29
      • 1970-01-01
      • 2020-04-25
      • 1970-01-01
      • 2021-07-02
      相关资源
      最近更新 更多