使用代理的PHP套接字连接

时间:2018-09-14 03:38:59

标签: php sockets websocket http-proxy

我有一个具有套接字连接的PHP应用程序。我正在考虑在其中集成代理,以便可以使用代理进行连接。我当前的连接代码如下所示:

public function connect()
{
    if ($this->isConnected()) {
        return true;
    }

    /* Create a TCP/IP socket. */
    $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
    if ($socket !== false) {
        $result = socket_connect($socket, 'e'.rand(1, 16).'.myserver.net', Constants::PORT);
        if ($result === false) {
            $socket = false;
        }
    }

    if ($socket !== false) {
        socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, ['sec' => Constants::TIMEOUT_SEC, 'usec' => Constants::TIMEOUT_USEC]);
        socket_set_option($socket, SOL_SOCKET, SO_SNDTIMEO, ['sec' => Constants::TIMEOUT_SEC, 'usec' => Constants::TIMEOUT_USEC]);

        $this->socket = $socket;
        $this->eventManager()->fire('onConnect',
            [
                $this->phoneNumber,
                $this->socket,
            ]
        );
        $this->logFile('info', 'Connected to server');

        return true;
    } else {
        $this->logFile('error', 'Failed to connect server');
        $this->eventManager()->fire('onConnectError',
            [
                $this->phoneNumber,
                $this->socket,
            ]
        );

        return false;
    }
}

让我知道是否有人对此有可靠的想法。谢谢

0 个答案:

没有答案
相关问题