PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Creating your own connection adapters

You can create your own connection adapters and use them. You could, for example, create a connection adapter that uses persistent sockets, or a connection adapter with caching abilities, and use them as needed in your application.

In order to do so, you must create your own adapter class that implements the Zend_Http_Client_Adapter_Interface interface. The following example shows the skeleton of a user-implemented adapter class. All the public functions defined in this example must be defined in your adapter as well:

Example 497. Creating your own connection adapter

<?php
class MyApp_Http_Client_Adapter_BananaProtocol
    
implements Zend_Http_Client_Adapter_Interface
{
    
/**
     * Set the configuration array for the adapter
     *
     * @param array $config
     */
    
public function setConfig($config = array())
    {
        
// This rarely changes - you should usually copy the
        // implementation in Zend_Http_Client_Adapter_Socket.
    
}

    
/**
     * Connect to the remote server
     *
     * @param string  $host
     * @param int     $port
     * @param boolean $secure
     */
    
public function connect($host$port 80$secure false)
    {
        
// Set up the connection to the remote server
    
}

    
/**
     * Send request to the remote server
     *
     * @param string        $method
     * @param Zend_Uri_Http $url
     * @param string        $http_ver
     * @param array         $headers
     * @param string        $body
     * @return string Request as text
     */
    
public function write($method,
                          
$url,
                          
$http_ver '1.1',
                          
$headers = array(),
                          
$body '')
    {
        
// Send request to the remote server.
        // This function is expected to return the full request
        // (headers and body) as a string
    
}

    
/**
     * Read response from server
     *
     * @return string
     */
    
public function read()
    {
        
// Read response from remote server and return it as a string
    
}

    
/**
     * Close the connection to the server
     *
     */
    
public function close()
    {
        
// Close the connection to the remote server - called last.
    
}
}

// Then, you could use this adapter:
$client = new Zend_Http_Client(array(
    
'adapter' => 'MyApp_Http_Client_Adapter_BananaProtocol'
));

Zend Framework