Chapitre 15. Zend_Http

Table des matières

15.1. Zend_Http_Client - Introduction
15.1.1. Introduction
15.1.2. Configuration Parameters
15.1.3. Performing Basic HTTP Requests
15.1.4. Adding GET and POST parameters
15.1.5. Accessing Last Request and Response
15.2. Zend_Http_Client - Utilisation avancée
15.2.1. Redirections HTTP
15.2.2. Ajout de cookies et gestion de leur persistence
15.2.3. Définir des entêtes personnalisés
15.2.4. Envoi de fichiers
15.2.5. Envoyer des données brutes via POST
15.2.6. Authentification HTTP
15.2.7. Envoyer plusieurs requêtes avec le même client
15.3. Zend_Http_Client - Connection Adapters
15.3.1. Overview
15.3.2. The Socket Adapter
15.3.3. The Proxy Adapter
15.3.4. The Test Adapter
15.3.5. Creating your own connection adapters
15.4. Zend_Http_Cookie and Zend_Http_CookieJar
15.4.1. Introduction
15.4.2. Instantiating Zend_Http_Cookie Objects
15.4.3. Zend_Http_Cookie getter methods
15.4.4. Zend_Http_Cookie: Matching against a scenario
15.4.5. The Zend_Http_CookieJar Class: Instantiation
15.4.6. Adding Cookies to a Zend_Http_CookieJar object
15.4.7. Retrieving Cookies From a Zend_Http_CookieJar object
15.5. Zend_Http_Response
15.5.1. Introduction

15.1. Zend_Http_Client - Introduction

15.1.1. Introduction

Zend_Http_Client provides an easy interface for preforming Hyper-Text Transfer Protocol (HTTP) requests. Zend_Http_Client supports most simple features expected from an HTTP client, as well as some more complex features such as HTTP authentication and file uploads. Successful requests (and most unsuccessful ones too) return a Zend_Http_Response object, which provides access to the response's headers and body (see Section 15.5, « Zend_Http_Response »).

The class constructor optionally accepts a URL as it's first parameter (can be either a string or a Zend_Uri_Http object), and an optional array of configuration parameters. Both can be left out, and set later using the setUri() and setConfig() methods.

Exemple 15.1. Instantiating a Zend_Http_Client object

<?php
    require_once 'Zend/Http/Client.php';

    $client = new Zend_Http_Client('http://example.org', array(
        'maxredirects' => 0,
        'timeout'      => 30));
        
    // This is actually exactly the same:
    $client = new Zend_Http_Client();
    $client->setUri('http://example.org');
    $client->setConfig(array(
        'maxredirects' => 0,
        'timeout'      => 30));

?>

15.1.2. Configuration Parameters

The constructor and setConfig() method accept an associative array of configuration parameters. Setting these parameters is optional, as they all have default values.

Tableau 15.1. Zend_Http_Client configuration parameters

Parameter Description Expected Values Default Value
maxredirects Maximum number of redirections to follow (0 = none) integer 5
strictredirects Whether to strictly follow the RFC when redirecting (see Section 15.2.1, « Redirections HTTP ») boolean false
useragent User agent identifier string (sent in request headers) string 'Zend_Http_Client'
timeout Connection timeout (seconds) integer 10
httpversion HTTP protocol version float (1.1 or 1.0) 1.1
adapter Connection adapter class to use (see Section 15.3, « Zend_Http_Client - Connection Adapters ») mixed 'Zend_Http_Client_Adapter_Socket'
keepalive Whether to enable keep-alive connections with the server. Useful and might improve performance if several consecutive requests to the same server are performned. boolean false

15.1.3. Performing Basic HTTP Requests

Performing simple HTTP requests is very easily done using the request() method, and rarely needs more than three lines of code:

Exemple 15.2. Preforming a Simple GET Request

<?php
    require_once 'Zend/Http/Client.php';

    $client = new Zend_Http_Client('http://example.org');
    $response = $client->request();
?>

The request() method takes one optional parameter - the request method. This can be either GET, POST, PUT, HEAD, DELETE, TRACE, OPTIONS or CONNECT as defined by the HTTP protocol [1]. For convenience, these are all defined as class constants: Zend_Http_Request::GET, Zend_Http_Request::POST and so on.

If no method is specified, the method set by the last setMethod() call is used. If setMethod() was never called, the default request method is GET (see the above example).

Exemple 15.3. Using Request Methods Other Than GET

<?php
    // Preforming a POST request
    $response = $client->request('POST');
    
    // Yet another way of preforming a POST request
    $client->setMethod(Zend_Http_Client::POST);
    $response = $client->request();
?>

15.1.4. Adding GET and POST parameters

Adding GET parameters to an HTTP request is quite simple, and can be done either by specifying them as part of the URL, or by using the setParameterGet() method. This method takes the GET parameter's name as it's first parameter, and the GET parameter's value as it's second parameter. For convenience, the setParameterGet() method can also accept a single associative array of name => value GET variables - which may be more comfortable when several GET parameters need to be set.

Exemple 15.4. Setting GET Parameters

<?php
    // Setting a get parameter using the setParameterGet method
    $client->setParameterGet('knight', 'lancelot');

    // This is equivalent to setting such URL:
    $client->setUri('http://example.com/index.php?knight=lancelot');
    
    // Adding several parameters with one call
    $client->setParameterGet(array(
        'first_name'  => 'Bender',
        'middle_name' => 'Bending'
        'made_in'     => 'Mexico',
    ));
?>

While GET parameters can be sent with every request method, POST parameters are only sent in the body of POST requests. Adding POST parameters to a request is very similar to adding GET parameters, and can be done with the setParameterPost() method, which is similar to the setParameterGet() method in structure.

Exemple 15.5. Setting POST Parameters

<?php
    // Setting a POST parameter
    $client->setParameterPost('language', 'fr');
    
    // Setting several POST parameters, one of them with several values
    $client->setParameterPost(array(
        'language'  => 'es',
        'country'   => 'ar',
        'selection' => array(45, 32, 80)
    ));
?>

Note that when sending POST requests, you can set both GET and POST parameters. On the other hand, while setting POST parameters for a non-POST request will not trigger and error, it is useless. Unless the request is a POST request, POST parameters are simply ignored.

15.1.5. Accessing Last Request and Response

Zend_Http_Client provides methods of accessing the last request sent and last response received by the client object. Zend_Http_Client->getLastRequest() takes no parameters and returns the last HTTP request sent by the client as a string. Similarly, Zend_Http_Client->getLastResponse() returns the last HTTP response received by the client as a Zend_Http_Response object.