The default connection adapter is the
Zend_Http_Client_Adapter_Socket adapter - this adapter will be
used unless you explicitly set the connection adapter. The Socket adapter is based on
PHP's built-in fsockopen() function, and does not require any special
extensions or compilation flags.
The Socket adapter allows several extra configuration options that
can be set using Zend_Http_Client->setConfig() or
passed to the client constructor.
Table 79. Zend_Http_Client_Adapter_Socket configuration parameters
| Parameter | Description | Expected Type | Default Value |
|---|---|---|---|
| persistent | Whether to use persistent TCP connections | boolean | FALSE |
| ssltransport | SSL transport layer (eg. 'sslv2', 'tls') | string | ssl |
| sslcert | Path to a PEM encoded SSL certificate | string | NULL |
| sslpassphrase | Passphrase for the SSL certificate file | string | NULL |
| sslusecontext | Enables proxied connections to use SSL even if the proxy connection itself does not. | boolean | FALSE |
Persistent TCP Connections
Using persistent TCP connections can potentially speed up HTTP requests - but in most use cases, will have little positive effect and might overload the HTTP server you are connecting to.
It is recommended to use persistent TCP connections only if you connect to the same server very frequently, and are sure that the server is capable of handling a large number of concurrent connections. In any case you are encouraged to benchmark the effect of persistent connections on both the client speed and server load before using this option.
Additionally, when using persistent connections it is recommended to enable Keep-Alive HTTP requests as described in the configuration section - otherwise persistent connections might have little or no effect.
HTTPS SSL Stream Parameters
ssltransport, sslcert and sslpassphrase are only relevant when connecting using HTTPS.
While the default SSL settings should work for most applications, you might need to change them if the server you are connecting to requires special client setup. If so, you should read the sections about SSL transport layers and options here.
Example 475. Changing the HTTPS transport layer
<?php
// Set the configuration parameters
$config = array(
'adapter' => 'Zend_Http_Client_Adapter_Socket',
'ssltransport' => 'tls'
);
// Instantiate a client object
$client = new Zend_Http_Client('https://www.example.com', $config);
// The following request will be sent over a TLS secure connection.
$response = $client->request();
The result of the example above will be similar to opening a TCP connection using the following PHP command:
fsockopen('tls://www.example.com', 443)
Starting from Zend Framework 1.9,
Zend_Http_Client_Adapter_Socket provides direct access to the
underlying stream context used
to connect to the remote server. This allows the user to pass specific options and
parameters to the TCP stream, and to the SSL
wrapper in case of HTTPS connections.
You can access the stream context using the following methods of
Zend_Http_Client_Adapter_Socket:
setStreamContext($context)Sets the stream context to be used by the adapter. Can accept either a stream context resource created using thestream_context_create()PHP function, or an array of stream context options, in the same format provided to this function. Providing an array will create a new stream context using these options, and set it.getStreamContext()Get the stream context of the adapter. If no stream context was set, will create a default stream context and return it. You can then set or get the value of different context options using regular PHP stream context functions.
Example 476. Setting stream context options for the Socket adapter
<?php
// Array of options
$options = array(
'socket' => array(
// Bind local socket side to a specific interface
'bindto' => '10.1.2.3:50505'
),
'ssl' => array(
// Verify server side certificate,
// do not accept invalid or self-signed SSL certificates
'verify_peer' => true,
'allow_self_signed' => false,
// Capture the peer's certificate
'capture_peer_cert' => true
)
);
// Create an adapter object and attach it to the HTTP client
$adapter = new Zend_Http_Client_Adapter_Socket();
$client = new Zend_Http_Client();
$client->setAdapter($adapter);
// Method 1: pass the options array to setStreamContext()
$adapter->setStreamContext($options);
// Method 2: create a stream context and pass it to setStreamContext()
$context = stream_context_create($options);
$adapter->setStreamContext($context);
// Method 3: get the default stream context and set the options on it
$context = $adapter->getStreamContext();
stream_context_set_option($context, $options);
// Now, preform the request
$response = $client->request();
// If everything went well, you can now access the context again
$opts = stream_context_get_options($adapter->getStreamContext());
echo $opts['ssl']['peer_certificate'];
Note
Note that you must set any stream context options before using the adapter
to preform actual requests. If no context is set before preforming
HTTP requests with the Socket adapter, a default stream
context will be created. This context resource could be accessed after
preforming any requests using the getStreamContext()
method.




