HTTP_Request2 – Class representing a HTTP request message

Request URL and GET Parameters

Request URL can be set in HTTP_Request2 constructor or via HTTP_Request2::setUrl() method. Both of these accept either a string or an instance of Net_URL2. URL is stored internally as an instance of Net_URL2 that can be accessed via HTTP_Request2::getUrl().

GET request parameters can be added to URL via Net_URL2::setQueryVariable() and Net_URL2::setQueryVariables():

Setting GET parameters

// Explicitly set request method and use_brackets
$request = new HTTP_Request2('',
HTTP_Request2::METHOD_GET, array('use_brackets' => true));
$url $request->getUrl();
'package_name' => array('HTTP_Request2''Net_URL2'),
'status'       => 'Open'

// This will output a page with open bugs for Net_URL2 and HTTP_Request2
echo $request->send()->getBody();

HTTP Authentication

HTTP_Request2 supports both Basic and Digest authentication schemes defined in RFC 2617. Authentication credentials can be set via HTTP_Request2::setAuth() method or given in the request URL (but in the latter case authentication scheme will default to Basic).

Setting authentication credentials

// This will set credentials for basic auth
$request = new HTTP_Request2('');

// This will set credentials for Digest auth

There is currently an issue with Digest authentication support in Curl Adapter due to an underlying PHP cURL extension problem.

Request Headers

Additional request headers can be set via HTTP_Request2::setHeader() method. It also allows removing previosly set headers

Setting request headers

// setting one header

// setting several headers in one go
'Connection' => 'close',
'Referer'    => 'http://localhost/'

// removing a header


Cookies can be added to the request via setHeader() method, but a specialized HTTP_Request2::addCookie() method is also provided

Adding cookies to the request


Using cookie jar to manage cookies across requests

Since release 2.0.0beta1 the package contains HTTP_Request2_CookieJar class that can be used manage cookies across requests. You can enable this functionality by passing either an existing instance of HTTP_Request2_CookieJar or TRUE to create a new instance to HTTP_Request2::setCookieJar() method. Cookie Jar can be later accessed by HTTP_Request2::getCookieJar().

Cookie jar will automatically store cookies set in HTTP response and pass them to requests with URLs matching cookie parameters. You can also manually add cookies to jar by using HTTP_Request2_CookieJar::store() method.

By default Public Suffix List is used to check whether a cookie domain matches the given URL. The same list is used to restrict cookie setting by Firefox, Chrome and Opera browsers. It can be disabled if needed by HTTP_Request2_CookieJar::usePublicSuffixList().

HTTP_Request2_CookieJar implements Serializable interface and thus can be easily serialized and stored somewhere. You can control whether session cookies stored in a jar should be serialized by calling HTTP_Request2_CookieJar::serializeSessionCookies().

When HTTP_Request2 instance has a cookie jar set, HTTP_Request2::addCookie() method will add a cookie to jar, rather than directly to 'Cookie' header, using current request URL for setting its 'domain' and 'path' components.

Request Body

If you are doing a POST request with Content-Type 'application/x-www-form-urlencoded' or 'multipart/form-data' (in other words, emulating POST form submission), you can add parameters via HTTP_Request2::addPostParameter() and file uploads via HTTP_Request2::addUpload(). HTTP_Request2 will take care of generating proper request body. File uploads will be streamed from disk by HTTP_Request2_MultipartBody to reduce memory consumption.

Emulating POST form submission

= new HTTP_Request2('');
'email' => '',
'phone' => '+7 (495) 123-45-67'

addUpload() can accept either a string with a local file name or a pointer to an open file, as returned by fopen(). You currently can't directly pass a string with the file contents, however you can pass a pointer to php://memory or php://temp:


HTTP request body can also be set directly, by providing a string or a filename to HTTP_Request2::setBody() method. This is the only way to set a request body for non-POST request.

Setting "raw" request body

= new HTTP_Request2('');
setHeader('Content-type: text/xml; charset=utf-8')
"<?xml version=\"1.0\" encoding=\"utf-8\"?" ">\r\n" .
"<methodCall>\r\n" .
" <methodName></methodName>\r\n" .
" <params>\r\n" .
"  <param><value><string>Hello, world!</string></value></param>\r\n" .
"  <param><value><int>42</int></value></param>\r\n" .
" </params>\r\n" .

HTTP Redirects

Since release 0.5.0 HTTP_Request2 can automatically follow HTTP redirects if follow_redirects parameter is set to TRUE.

HTTP_Request2 will only follow redirects to HTTP(S) URLs, redirects to other protocols will result in an Exception.

HTTP_Request2::send will return only the final response, if you are interested in the intermediate ones you should use Observers.

Configuration parameters for HTTP_Request2 (Previous) Classes that actually perform the request (Next)
Last updated: Sat, 16 Feb 2019 — Download Documentation
Do you think that something on this page is wrong? Please file a bug report.
View this page in:
  • English

User Notes:

Note by: manidan
i think this function should be added

public function addGetParameter($name, $value)
$this->getUrl()->setQueryVariable($name, $value);
return $this;

to Simplify adding GET parametters .
Note by: cweiske
To move cookies between requests, you can do the following:
= new HTTP_Request2('');
foreach (
$response->getCookies() as $arCookie) {
$req2->addCookie($arCookie['name'], $arCookie['value']);