Documentation

Introduction - Zend_Http

Introduction

Zend_Http_Client provides an easy interface for preforming Hyper-Text Transfer Protocol (HTTP) requests. Zend_Http_Client supports most simple features expected from an HTTP client, as well as some more complex features such as HTTP authentication and file uploads. Successful requests (and most unsuccessful ones too) return a Zend_Http_Response object, which provides access to the response's headers and body (see this section).

Using Zend_Http_Client

The class constructor optionally accepts a URL as its first parameter (can be either a string or a Zend_Uri_Http object), and an array or Zend_Config object containing configuration options. Both can be left out, and set later using the setUri() and setConfig() methods.

Example #1 Instantiating a Zend_Http_Client Object

  1. $client = new Zend_Http_Client('http://example.org', array(
  2.     'maxredirects' => 0,
  3.     'timeout'      => 30));
  4.  
  5. // This is actually exactly the same:
  6. $client = new Zend_Http_Client();
  7. $client->setUri('http://example.org');
  8. $client->setConfig(array(
  9.     'maxredirects' => 0,
  10.     'timeout'      => 30));
  11.  
  12. // You can also use a Zend_Config object to set the client's configuration
  13. $config = new Zend_Config_Ini('httpclient.ini', 'secure');
  14. $client->setConfig($config);

Note: Zend_Http_Client uses Zend_Uri_Http to validate URLs. This means that some special characters like the pipe symbol ('|') or the caret symbol ('^') will not be accepted in the URL by default. This can be modified by setting the 'allow_unwise' option of Zend_Uri to 'TRUE'. See this section for more information.

Configuration Parameters

The constructor and setConfig() method accept an associative array of configuration parameters, or a Zend_Config object. Setting these parameters is optional, as they all have default values.

Zend_Http_Client configuration parameters
Parameter Description Expected Values Default Value
maxredirects Maximum number of redirections to follow (0 = none) integer 5
strict Whether perform validation on header names. When set to FALSE, validation functions will be skipped. Usually this should not be changed boolean TRUE
strictredirects Whether to strictly follow the RFC when redirecting (see this section) boolean FALSE
useragent User agent identifier string (sent in request headers) string 'Zend_Http_Client'
timeout Connection timeout (seconds) integer 10
httpversion HTTP protocol version (usually '1.1' or '1.0') string '1.1'
adapter Connection adapter class to use (see this section) mixed 'Zend_Http_Client_Adapter_Socket'
keepalive Whether to enable keep-alive connections with the server. Useful and might improve performance if several consecutive requests to the same server are performed. boolean FALSE
storeresponse Whether to store last response for later retrieval with getLastResponse(). If set to FALSE getLastResponse() will return NULL. boolean TRUE
encodecookies Whether to pass the cookie value through urlencode/urldecode. Enabling this breaks support with some web servers. Disabling this limits the range of values the cookies can contain. boolean TRUE

Performing Basic HTTP Requests

Performing simple HTTP requests is very easily done using the request() method, and rarely needs more than three lines of code:

Example #2 Performing a Simple GET Request

  1. $client = new Zend_Http_Client('http://example.org');
  2. $response = $client->request();
The request() method takes one optional parameter - the request method. This can be either GET, POST, PUT, HEAD, DELETE, TRACE, OPTIONS or CONNECT as defined by the HTTP protocol [1] http://www.w3.org/Protocols/rfc2616/rfc2616.html . For convenience, these are all defined as class constants: Zend_Http_Client::GET, Zend_Http_Client::POST and so on.

If no method is specified, the method set by the last setMethod() call is used. If setMethod() was never called, the default request method is GET (see the above example).

Example #3 Using Request Methods Other Than GET

  1. // Preforming a POST request
  2. $response = $client->request('POST');
  3.  
  4. // Yet another way of preforming a POST request
  5. $client->setMethod(Zend_Http_Client::POST);
  6. $response = $client->request();

Adding GET and POST parameters

Adding GET parameters to an HTTP request is quite simple, and can be done either by specifying them as part of the URL, or by using the setParameterGet() method. This method takes the GET parameter's name as its first parameter, and the GET parameter's value as its second parameter. For convenience, the setParameterGet() method can also accept a single associative array of name => value GET variables - which may be more comfortable when several GET parameters need to be set.

Example #4 Setting GET Parameters

  1. // Setting a get parameter using the setParameterGet method
  2. $client->setParameterGet('knight', 'lancelot');
  3.  
  4. // This is equivalent to setting such URL:
  5. $client->setUri('http://example.com/index.php?knight=lancelot');
  6.  
  7. // Adding several parameters with one call
  8. $client->setParameterGet(array(
  9.     'first_name'  => 'Bender',
  10.     'middle_name' => 'Bending'
  11.     'made_in'     => 'Mexico',
  12. ));

While GET parameters can be sent with every request method, POST parameters are only sent in the body of POST requests. Adding POST parameters to a request is very similar to adding GET parameters, and can be done with the setParameterPost() method, which is similar to the setParameterGet() method in structure.

Example #5 Setting POST Parameters

  1. // Setting a POST parameter
  2. $client->setParameterPost('language', 'fr');
  3.  
  4. // Setting several POST parameters, one of them with several values
  5. $client->setParameterPost(array(
  6.     'language'  => 'es',
  7.     'country'   => 'ar',
  8.     'selection' => array(45, 32, 80)
  9. ));
Note that when sending POST requests, you can set both GET and POST parameters. On the other hand, while setting POST parameters for a non-POST request will not trigger and error, it is useless. Unless the request is a POST request, POST parameters are simply ignored.

Accessing Last Request and Response

Zend_Http_Client provides methods of accessing the last request sent and last response received by the client object. Zend_Http_Client->getLastRequest() takes no parameters and returns the last HTTP request sent by the client as a string. Similarly, Zend_Http_Client->getLastResponse() returns the last HTTP response received by the client as a Zend_Http_Response object.

[1] See RFC 2616 - .

Copyright

© 2006-2021 by Zend by Perforce. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.

Contacts