Zend_Http_Client_Adapter_Socket::connect PHP Méthode

connect() public méthode

Connect to the remote server
public connect ( string $host, integer $port = 80, boolean $secure = false )
$host string
$port integer
$secure boolean
    public function connect($host, $port = 80, $secure = false)
    {
        // If the URI should be accessed via SSL, prepend the Hostname with ssl://
        $host = ($secure ? $this->config['ssltransport'] : 'tcp') . '://' . $host;
        // If we are connected to the wrong host, disconnect first
        if ($this->connected_to[0] != $host || $this->connected_to[1] != $port) {
            if (is_resource($this->socket)) {
                $this->close();
            }
        }
        // Now, if we are not connected, connect
        if (!is_resource($this->socket) || !$this->config['keepalive']) {
            $context = $this->getStreamContext();
            if ($secure || $this->config['sslusecontext']) {
                if ($this->config['sslcert'] !== null) {
                    if (!stream_context_set_option($context, 'ssl', 'local_cert', $this->config['sslcert'])) {
                        require_once 'Zend/Http/Client/Adapter/Exception.php';
                        throw new Zend_Http_Client_Adapter_Exception('Unable to set sslcert option');
                    }
                }
                if ($this->config['sslpassphrase'] !== null) {
                    if (!stream_context_set_option($context, 'ssl', 'passphrase', $this->config['sslpassphrase'])) {
                        require_once 'Zend/Http/Client/Adapter/Exception.php';
                        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();
                require_once 'Zend/Http/Client/Adapter/Exception.php';
                throw new Zend_Http_Client_Adapter_Exception('Unable to Connect to ' . $host . ':' . $port . '. Error #' . $errno . ': ' . $errstr);
            }
            // Set the stream timeout
            if (!stream_set_timeout($this->socket, (int) $this->config['timeout'])) {
                require_once 'Zend/Http/Client/Adapter/Exception.php';
                throw new Zend_Http_Client_Adapter_Exception('Unable to set the connection timeout');
            }
            // Update connected_to
            $this->connected_to = array($host, $port);
        }
    }

Usage Example

Exemple #1
0
 /**
  * Connect to the remote server
  *
  * Will try to connect to the proxy server. If no proxy was set, will
  * fall back to the target server (behave like regular Socket adapter)
  *
  * @param string  $host
  * @param int     $port
  * @param boolean $secure
  * @param int     $timeout
  */
 public function connect($host, $port = 80, $secure = false)
 {
     // If no proxy is set, fall back to Socket adapter
     if (!$this->config['proxy_host']) {
         return parent::connect($host, $port, $secure);
     }
     // Go through a proxy - the connection is actually to the proxy server
     $host = $this->config['proxy_host'];
     $port = $this->config['proxy_port'];
     // If we are connected to the wrong proxy, disconnect first
     if ($this->connected_to[0] != $host || $this->connected_to[1] != $port) {
         if (is_resource($this->socket)) {
             $this->close();
         }
     }
     // Now, if we are not connected, connect
     if (!is_resource($this->socket) || !$this->config['keepalive']) {
         $this->socket = @fsockopen($host, $port, $errno, $errstr, (int) $this->config['timeout']);
         if (!$this->socket) {
             $this->close();
             require_once 'Zend/Http/Client/Adapter/Exception.php';
             throw new Zend_Http_Client_Adapter_Exception('Unable to Connect to proxy server ' . $host . ':' . $port . '. Error #' . $errno . ': ' . $errstr);
         }
         // Set the stream timeout
         if (!stream_set_timeout($this->socket, (int) $this->config['timeout'])) {
             require_once 'Zend/Http/Client/Adapter/Exception.php';
             throw new Zend_Http_Client_Adapter_Exception('Unable to set the connection timeout');
         }
         // Update connected_to
         $this->connected_to = array($host, $port);
     }
 }
All Usage Examples Of Zend_Http_Client_Adapter_Socket::connect