فصل 11. Zend_Http

قائمة المحتويات

11.1. Zend_Http_Client
11.1.1. Introduction
11.1.2. Basic GET Requests with Specified HTTP Headers
11.1.3. Requesting Multiple Domains
11.1.4. Changing the HTTP Timeout
11.1.5. Setting HTTP Headers Dynamically
11.1.6. Making HTTP POST, PUT, and DELETE Requests
11.2. Zend_Http_Client - Advanced Usage
11.2.1. HTTP Redirections
11.2.2. Adding Cookies and Using Cookie Persistence
11.2.3. Setting Custom Request Headers
11.2.4. File Uploads
11.2.5. Sending Raw POST Data
11.2.6. HTTP Authentication
11.2.7. Sending Multiple Requests With the Same Client
11.3. Zend_Http_Client - Connection Adapters
11.3.1. Overview
11.3.2. The Socket Adapter
11.3.3. The Proxy Adapter
11.3.4. The Test Adapter
11.3.5. Creating your own connection adapters
11.4. Zend_Http_Cookie and Zend_Http_CookieJar
11.4.1. Introduction
11.4.2. Instantiating Zend_Http_Cookie Objects
11.4.3. Zend_Http_Cookie getter methods
11.4.4. Zend_Http_Cookie: Matching against a scenario
11.4.5. The Zend_Http_CookieJar Class: Instantiation
11.4.6. Adding Cookies to a Zend_Http_CookieJar object
11.4.7. Retrieving Cookies From a Zend_Http_CookieJar object
11.5. Zend_Http_Response
11.5.1. Introduction

11.1. Zend_Http_Client

11.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.

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

11.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:

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

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

11.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.

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

11.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.

11.1.5. Setting HTTP Headers Dynamically

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

[هام] هام

Headers must follow the format: Header: value

11.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.

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