فصل 13. Zend_Http

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

13.1. Zend_Http_Client
13.1.1. Introduction
13.1.2. Basic GET Requests with Specified HTTP Headers
13.1.3. Requesting Multiple Domains
13.1.4. Changing the HTTP Timeout
13.1.5. Setting HTTP Headers Dynamically
13.1.6. Making HTTP POST, PUT, and DELETE Requests
13.2. Zend_Http_Client - Advanced Usage
13.2.1. HTTP Redirections
13.2.2. Adding Cookies and Using Cookie Persistence
13.2.3. Setting Custom Request Headers
13.2.4. File Uploads
13.2.5. Sending Raw POST Data
13.2.6. HTTP Authentication
13.2.7. Sending Multiple Requests With the Same Client
13.3. Zend_Http_Client - Connection Adapters
13.3.1. Overview
13.3.2. The Socket Adapter
13.3.3. The Proxy Adapter
13.3.4. The Test Adapter
13.3.5. Creating your own connection adapters
13.4. Zend_Http_Cookie and Zend_Http_CookieJar
13.4.1. Introduction
13.4.2. Instantiating Zend_Http_Cookie Objects
13.4.3. Zend_Http_Cookie getter methods
13.4.4. Zend_Http_Cookie: Matching against a scenario
13.4.5. The Zend_Http_CookieJar Class: Instantiation
13.4.6. Adding Cookies to a Zend_Http_CookieJar object
13.4.7. Retrieving Cookies From a Zend_Http_CookieJar object
13.5. Zend_Http_Response
13.5.1. Introduction

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().