Chapter 6. Zend_Http

Table of Contents

6.1. Zend_Http_Client
6.1.1. Introduction
6.1.2. Basic GET Requests with Specified HTTP Headers
6.1.3. Requesting Multiple Domains
6.1.4. Changing the HTTP Timeout
6.1.5. Setting HTTP Headers Dynamically
6.1.6. Making HTTP POST, PUT, and DELETE Requests
6.2. Zend_Http_Response
6.2.1. Introduction

6.1. Zend_Http_Client

6.1.1. Introduction

Zend_Http_Client provides an easy interface with which to perform HTTP requests. Zend_Http_Client is able to perform GET, POST, PUT and DELETE requests.

[Note] Note
Zend_Http_Client follows up to 5 HTTP redirections by default. To change this behavior, pass the maximum number of allowed redirections to the get() method.

Example 6.1. Performing a Basic GET Request

<?php
    require_once 'Zend/Http/Client.php';
    try {
        $http = new Zend_Http_Client('http://example.org');
        $response = $http->get();
        if ($response->isSuccessful()) {
            echo $response->getBody();
        } else {
            echo '<p>An error occurred</p>';
        }
    } catch (Zend_Http_Client_Exception $e) {
        echo '<p>An error occurred (' .$e->getMessage(). ')</p>';
    }
    ?>

6.1.2. Basic GET Requests with Specified HTTP Headers

The Zend_Http_Client constructor creates a Zend_Http_Client instance for sending HTTP requests.

When using Zend_Http_Client on a single URL, in most cases you can supply the URL and relevant headers to the constructor, as in the following examples:

Example 6.2. Creating a Basic Zend_Http_Client

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

    // Specify the URL and a single header
    $http = new Zend_Http_Client('http://example.org', 'Accept: text/html');
    ?>       

Example 6.3. Sending Multiple Headers

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

    // Specify the URL and multiple headers
    $http = new Zend_Http_Client('http://example.org',
                            array('Accept: text/html', 'Accept-Language: en-us,en;q=0.5'));
    ?>       

If you wish to use Zend_Http_Client to send requests to multiple URLs, see Section 6.1.3, “Requesting Multiple Domains”

6.1.3. Requesting Multiple Domains

Zend_Http_Client supports sending requests to multiple domains by setting the URL to query using Zend_Http_Client::setUri().

[Note] Note

A great use for this is when querying multiple RSS feeds.

Example 6.4. Requesting Multiple Domains

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

    // Instantiate our client object
    $http = new Zend_Http_Client();

    // Set the URI to Slashdot's main feed
    $http->setUri('http://rss.slashdot.org/Slashdot/slashdot');

    // Retrieve the feed
    $slashdot = $http->get();

    // Now get the BBC news feed
    $http->setUri('http://newsrss.bbc.co.uk/rss/newsonline_world_edition/technology/rss.xml');

    // Retrieve the feed
    $bbc = $http->get();
    ?>   

6.1.4. Changing the HTTP Timeout

Zend_Http_Client::setTimeout() allows you to set the timeout for the HTTP connection in seconds.

[Note] Note

The default timeout is 10 seconds.

6.1.5. Setting HTTP Headers Dynamically

Using Zend_Http_Client::setHeaders() you supply an array of headers.

[Important] Important

Headers must follow the format: Header: value

6.1.6. Making HTTP POST, PUT, and DELETE Requests

Performing HTTP POST, PUT, and DELETE requests are facilitated in Zend_Http_Client by three methods: post(), put(), and delete(), respectively. The post() and put() methods each take a single string parameter, $data, into which should be placed a string with the data properly encoded, as in the following: name=value&foo=bar. The delete() method has no parameters.

Example 6.5. Sending POST data with Zend_Http_Client

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

    // Instantiate our client object
    $http = new Zend_Http_Client();

    // Set the URI to a POST data processor
    $http->setUri('http://example.org/post/processor');

    // Save specific GET variables as HTTP POST data
    $postData = 'foo=' . urlencode($_GET['foo']) . '&bar=' . urlencode($_GET['bar']);

    // Make the HTTP POST request and save the HTTP response
    $httpResponse = $http->post($postData);
    ?>   

Making a PUT request is the same as in the example above for making a POST request; just substitute the put() method for post().