فصل 13. Zend_Http
السابق |  | التالي

## فصل 13. Zend_Http

## 13.1. Zend_Http_Client

### 13.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.

| Ammount of redirections
`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.

مثال 13.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>';
    }
    ?>

### 13.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:

مثال 13.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');
        ?>

مثال 13.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 قسم 13.1.3, “Requesting Multiple Domains”

### 13.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()`.

| ملاحظة
A great use for this is when querying multiple RSS feeds.

مثال 13.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();
        ?>

### 13.1.4. Changing the HTTP Timeout

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

| ملاحظة
The default timeout is 10 seconds.

### 13.1.5. Setting HTTP Headers Dynamically

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

| هام
Headers must follow the format: ``Header: value``

### 13.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.

مثال 13.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()`.

السابق |  | التالي
12.9. Catching Gdata Exceptions | البداية | 13.2. Zend_Http_Client - Advanced Usage