首页 > 解决方案 > 使用代理的php套接字连接

问题描述

我有一个具有套接字连接的 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;
    }
}

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

标签: phpsocketswebsockethttp-proxy

解决方案


推荐阅读