Índice
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.
Nota | |
---|---|
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.
|
Exemplo 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>'; } ?>
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:
Exemplo 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'); ?>
Exemplo 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 Seção 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()
.
Nota | |
---|---|
A great use for this is when querying multiple RSS feeds. |
Exemplo 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(); ?>
Zend_Http_Client::setTimeout()
allows you to set the timeout for the HTTP connection in
seconds.
Nota | |
---|---|
The default timeout is 10 seconds. |
Using Zend_Http_Client::setHeaders()
you supply an array of
headers.
Importante | |
---|---|
Headers must follow the format:
|
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.
Exemplo 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()
.