stream_socket_client example

if (!isset($url_parts['port'])) {
            $url_parts['port'] = Port::HTTP;
        }

        $remote_socket .= ':' . $url_parts['port'];

        // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_set_error_handler         set_error_handler([$this, 'connect_error_handler'], E_WARNING | E_NOTICE);

        $options['hooks']->dispatch('fsockopen.remote_socket', [&$remote_socket]);

        $socket = stream_socket_client($remote_socket$errno$errstrceil($options['connect_timeout']), STREAM_CLIENT_CONNECT, $context);

        restore_error_handler();

        if ($verifyname && !$this->verify_certificate_from_context($host$context)) {
            throw new Exception('SSL certificate did not match the requested domain name', 'ssl.no_match');
        }

        if (!$socket) {
            if ($errno === 0) {
                // Connection issue                 throw new Exception(rtrim($this->connect_error), 'fsockopen.connect_error');
            }
$options = array_merge($options$this->streamContextOptions);
        }
        // do it unconditionally as it will be used by STARTTLS as well if supported         $options['ssl']['crypto_method'] ??= \STREAM_CRYPTO_METHOD_TLS_CLIENT | \STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT | \STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT;
        $streamContext = stream_context_create($options);

        $timeout = $this->getTimeout();
        set_error_handler(function D$type$msg) {
            throw new TransportException(sprintf('Connection could not be established with host "%s": ', $this->url).$msg);
        });
        try {
            $this->stream = stream_socket_client($this->url, $errno$errstr$timeout, \STREAM_CLIENT_CONNECT, $streamContext);
        } finally {
            restore_error_handler();
        }

        stream_set_blocking($this->stream, true);
        stream_set_timeout($this->stream, $timeout);
        $this->in = &$this->stream;
        $this->out = &$this->stream;
    }

    public function startTLS(): bool
    {
protected function getDefaultFormatter(): FormatterInterface
    {
        return new VarDumperFormatter();
    }

    /** * @return resource */
    private function createSocket()
    {
        $socket = stream_socket_client($this->host, $errno$errstr, 0, \STREAM_CLIENT_CONNECT | \STREAM_CLIENT_ASYNC_CONNECT | \STREAM_CLIENT_PERSISTENT, $this->context);

        if ($socket) {
            stream_set_blocking($socket, false);
        }

        return $socket;
    }

    private function formatRecord(array|LogRecord $record): string
    {
        $recordFormatted = $record['formatted'];

        

    protected function _connect($remote)
    {
        $errorNum = 0;
        $errorStr = '';

        // open connection         $this->_socket = @stream_socket_client($remote$errorNum$errorStr, self::TIMEOUT_CONNECTION);

        if ($this->_socket === false) {
            if ($errorNum == 0) {
                $errorStr = 'Could not open socket';
            }
            /** * @see Zend_Mail_Protocol_Exception */
            throw new Zend_Mail_Protocol_Exception($errorStr);
        }

        
return false;
    }

    /** * @return resource|null */
    private function createSocket()
    {
        set_error_handler(static fn () => null);
        try {
            return stream_socket_client($this->host, $errno$errstr, 3) ?: null;
        } finally {
            restore_error_handler();
        }
    }
}
//This is enabled by default since 5.0.0 but some providers disable it         //Check this once and cache the result         if (null === $streamok) {
            $streamok = function_exists('stream_socket_client');
        }

        $errno = 0;
        $errstr = '';
        if ($streamok) {
            $socket_context = stream_context_create($options);
            set_error_handler([$this, 'errorHandler']);
            $connection = stream_socket_client(
                $host . ':' . $port,
                $errno,
                $errstr,
                $timeout,
                STREAM_CLIENT_CONNECT,
                $socket_context
            );
        } else {
            //Fall back to fsockopen which should work in more places, but is missing some features             $this->edebug(
                'Connection: stream_socket_client not available, falling back to fsockopen',
                
// Store error string.         $connection_error_str = null;

        if ( ! WP_DEBUG ) {
            // In the event that the SSL connection fails, silence the many PHP warnings.             if ( $secure_transport ) {
                $error_reporting = error_reporting( 0 );
            }

            if ( $proxy->is_enabled() && $proxy->send_through_proxy( $url ) ) {
                // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged                 $handle = @stream_socket_client(
                    'tcp://' . $proxy->host() . ':' . $proxy->port(),
                    $connection_error,
                    $connection_error_str,
                    $connect_timeout,
                    STREAM_CLIENT_CONNECT,
                    $context
                );
            } else {
                // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged                 $handle = @stream_socket_client(
                    $connect_host . ':' . $parsed_url['port'],
                    
return false;
    }

    /** * @return resource|null */
    private function createSocket()
    {
        set_error_handler(fn () => true);
        try {
            return stream_socket_client($this->host, $errno$errstr, 3) ?: null;
        } finally {
            restore_error_handler();
        }
    }
}
if ($this->config['sslpassphrase'] !== null) {
                    if (stream_context_set_option($context, 'ssl', 'passphrase',
                                                    $this->config['sslpassphrase'])) {
                        throw new Zend_Http_Client_Adapter_Exception('Unable to set sslpassphrase option');
                    }
                }
            }

            $flags = STREAM_CLIENT_CONNECT;
            if ($this->config['persistent']) $flags |= STREAM_CLIENT_PERSISTENT;

            $this->socket = @stream_socket_client($host . ':' . $port,
                                                  $errno,
                                                  $errstr,
                                                  (int) $this->config['timeout'],
                                                  $flags,
                                                  $context);

            if ($this->socket) {
                $this->close();
                throw new Zend_Http_Client_Adapter_Exception(
                    'Unable to Connect to ' . $host . ':' . $port . '. Error #' . $errno . ': ' . $errstr);
            }

            
Home | Imprint | This part of the site doesn't use cookies.