Overview

Packages

  • Requests
    • Authentication
    • Cookies
    • Proxy
    • Session
    • Transport
    • Utilities

Classes

  • Requests
  • Requests_Response
  • Requests_Response_Headers

Exceptions

  • Requests_Exception
  • Requests_Exception_HTTP
  • Requests_Exception_HTTP_304
  • Requests_Exception_HTTP_305
  • Requests_Exception_HTTP_306
  • Requests_Exception_HTTP_400
  • Requests_Exception_HTTP_401
  • Requests_Exception_HTTP_402
  • Requests_Exception_HTTP_403
  • Requests_Exception_HTTP_404
  • Requests_Exception_HTTP_405
  • Requests_Exception_HTTP_406
  • Requests_Exception_HTTP_407
  • Requests_Exception_HTTP_408
  • Requests_Exception_HTTP_409
  • Requests_Exception_HTTP_410
  • Requests_Exception_HTTP_411
  • Requests_Exception_HTTP_412
  • Requests_Exception_HTTP_413
  • Requests_Exception_HTTP_414
  • Requests_Exception_HTTP_415
  • Requests_Exception_HTTP_416
  • Requests_Exception_HTTP_417
  • Requests_Exception_HTTP_418
  • Requests_Exception_HTTP_428
  • Requests_Exception_HTTP_429
  • Requests_Exception_HTTP_431
  • Requests_Exception_HTTP_500
  • Requests_Exception_HTTP_501
  • Requests_Exception_HTTP_502
  • Requests_Exception_HTTP_503
  • Requests_Exception_HTTP_504
  • Requests_Exception_HTTP_505
  • Requests_Exception_HTTP_511
  • Requests_Exception_HTTP_Unknown
  • Requests_Exception_Transport
  • Requests_Exception_Transport_cURL
  • Overview
  • Package
  • Class
  • Tree
  • Deprecated

Warning! You are currently reading the documentation for Requests 1.x.

It is recommended to upgrade to Requests 2.x at your earliest convenience. Upgrading to Requests 2.x is fairly straight-forward.
A full list of the changes can be found in the Changelog.

Visit the documentation for Requests in Requests 2.x.

Class Requests

Requests for PHP

Inspired by Requests for Python.

Based on concepts from SimplePie_File, RequestCore and WP_Http.

Package: Requests
Located at Requests.php
Methods summary
public static
# autoloader( string $class )

Autoloader for Requests

Autoloader for Requests

Register this with Requests::register_autoloader() if you'd like to avoid having to create your own.

(You can also use spl_autoload_register directly if you'd prefer.)

Parameters

$class
Class name to load

Codecoverageignore

public static
# register_autoloader( )

Register the built-in autoloader

Register the built-in autoloader

Codecoverageignore

public static
# add_transport( string $transport )

Register a transport

Register a transport

Parameters

$transport
Transport class to add, must support the Requests_Transport interface
protected static Requests_Transport
# get_transport( $capabilities = array() )

Get a working transport

Get a working transport

Returns

Requests_Transport

Throws

Requests_Exception
If no valid transport is found (notransport)
public static Requests_Response
# get( string $url, array $headers = array(), array $options = array() )

Send a GET request

Send a GET request

Parameters

$url
$headers
$options

Returns

Requests_Response

See

Requests::request()
public static Requests_Response
# head( string $url, array $headers = array(), array $options = array() )

Send a HEAD request

Send a HEAD request

Parameters

$url
$headers
$options

Returns

Requests_Response

See

Requests::request()
public static Requests_Response
# delete( string $url, array $headers = array(), array $options = array() )

Send a DELETE request

Send a DELETE request

Parameters

$url
$headers
$options

Returns

Requests_Response

See

Requests::request()
public static Requests_Response
# trace( string $url, array $headers = array(), array $options = array() )

Send a TRACE request

Send a TRACE request

Parameters

$url
$headers
$options

Returns

Requests_Response

See

Requests::request()
public static Requests_Response
# post( string $url, array $headers = array(), array $data = array(), array $options = array() )

Send a POST request

Send a POST request

Parameters

$url
$headers
$data
$options

Returns

Requests_Response

See

Requests::request()
public static Requests_Response
# put( string $url, array $headers = array(), array $data = array(), array $options = array() )

Send a PUT request

Send a PUT request

Parameters

$url
$headers
$data
$options

Returns

Requests_Response

See

Requests::request()
public static Requests_Response
# options( string $url, array $headers = array(), array $data = array(), array $options = array() )

Send an OPTIONS request

Send an OPTIONS request

Parameters

$url
$headers
$data
$options

Returns

Requests_Response

See

Requests::request()
public static Requests_Response
# patch( string $url, array $headers, array $data = array(), array $options = array() )

Send a PATCH request

Send a PATCH request

Note: Unlike Requests::post() and Requests::put(), $headers is required, as the specification recommends that should send an ETag

Parameters

$url
$headers
$data
$options

Returns

Requests_Response

See

Requests::request()

Link

https://tools.ietf.org/html/rfc5789
public static Requests_Response
# request( string $url, array $headers = array(), array|null $data = array(), string $type = Requests::GET, array $options = array() )

Main interface for HTTP requests

Main interface for HTTP requests

This method initiates a request and sends it via a transport before parsing.

The $options parameter takes an associative array with the following options:

  • timeout: How long should we wait for a response? Note: for cURL, a minimum of 1 second applies, as DNS resolution operates at second-resolution only. (float, seconds with a millisecond precision, default: 10, example: 0.01)
  • connect_timeout: How long should we wait while trying to connect? (float, seconds with a millisecond precision, default: 10, example: 0.01)
  • useragent: Useragent to send to the server (string, default: php-requests/$version)
  • follow_redirects: Should we follow 3xx redirects? (boolean, default: true)
  • redirects: How many times should we redirect before erroring? (integer, default: 10)
  • blocking: Should we block processing on this request? (boolean, default: true)
  • filename: File to stream the body to instead. (string|boolean, default: false)
  • auth: Authentication handler or array of user/password details to use for Basic authentication (Requests_Auth|array|boolean, default: false)
  • proxy: Proxy details to use for proxy by-passing and authentication (Requests_Proxy|array|string|boolean, default: false)
  • max_bytes: Limit for the response body size. (integer|boolean, default: false)
  • idn: Enable IDN parsing (boolean, default: true)
  • transport: Custom transport. Either a class name, or a transport object. Defaults to the first working transport from getTransport() (string|Requests_Transport, default: getTransport())
  • hooks: Hooks handler. (Requests_Hooker, default: new Requests_Hooks())
  • verify: Should we verify SSL certificates? Allows passing in a custom certificate file as a string. (Using true uses the system-wide root certificate store instead, but this may have different behaviour across transports.) (string|boolean, default: library/Requests/Transport/cacert.pem)
  • verifyname: Should we verify the common name in the SSL certificate? (boolean, default: true)
  • data_format: How should we send the $data parameter? (string, one of 'query' or 'body', default: 'query' for HEAD/GET/DELETE, 'body' for POST/PUT/OPTIONS/PATCH)

Parameters

$url
URL to request
$headers
Extra headers to send with the request
$data
Data to send either as a query string for GET/HEAD requests, or in the body for POST requests
$type
HTTP request type (use Requests constants)
$options
Options for the request (see description for more information)

Returns

Requests_Response

Throws

Requests_Exception
On invalid URLs (nonhttp)
public static array
# request_multiple( array $requests, array $options = array() )

Send multiple HTTP requests simultaneously

Send multiple HTTP requests simultaneously

The $requests parameter takes an associative or indexed array of request fields. The key of each request can be used to match up the request with the returned data, or with the request passed into your multiple.request.complete callback.

The request fields value is an associative array with the following keys:

  • url: Request URL Same as the $url parameter to Requests::request (string, required)
  • headers: Associative array of header fields. Same as the $headers parameter to Requests::request (array, default: array())
  • data: Associative array of data fields or a string. Same as the $data parameter to Requests::request (array|string, default: array())
  • type: HTTP request type (use Requests constants). Same as the $type parameter to Requests::request (string, default: Requests::GET)
  • cookies: Associative array of cookie name to value, or cookie jar. (array|Requests_Cookie_Jar)

If the $options parameter is specified, individual requests will inherit options from it. This can be used to use a single hooking system, or set all the types to Requests::POST, for example.

In addition, the $options parameter takes the following global options:

  • complete: A callback for when a request is complete. Takes two parameters, a Requests_Response/Requests_Exception reference, and the ID from the request array (Note: this can also be overridden on a per-request basis, although that's a little silly) (callback)

Parameters

$requests
Requests data (see description for more information)
$options
Global and default options (see Requests::request)

Returns

array
Responses (either Requests_Response or a Requests_Exception object)
protected static array
# get_default_options( boolean $multirequest = false )

Get the default options

Get the default options

Parameters

$multirequest
Is this a multirequest?

Returns

array
Default option values

See

Requests::request() for values returned by this method
public static string
# get_certificate_path( )

Get default certificate path.

Get default certificate path.

Returns

string
Default certificate path.
public static
# set_certificate_path( string $path )

Set default certificate path.

Set default certificate path.

Parameters

$path
Certificate path, pointing to a PEM file.
protected static array
# set_defaults( string & $url, array & $headers, array|null & $data, string & $type, array & $options )

Set the default values

Set the default values

Parameters

$url
URL to request
$headers
Extra headers to send with the request
$data
Data to send either as a query string for GET/HEAD requests, or in the body for POST requests
$type
HTTP request type
$options
Options for the request

Returns

array
$options
protected static Requests_Response
# parse_response( string $headers, string $url, array $req_headers, array $req_data, array $options )

HTTP response parser

HTTP response parser

Parameters

$headers
Full response text including headers and body
$url
Original request URL
$req_headers
Original $headers array passed to Requests::request(), in case we need to follow redirects
$req_data
Original $data array passed to Requests::request(), in case we need to follow redirects
$options
Original $options array passed to Requests::request(), in case we need to follow redirects

Returns

Requests_Response

Throws

Requests_Exception
On missing head/body separator (requests.no_crlf_separator)
Requests_Exception
On missing head/body separator (noversion)
Requests_Exception
On missing head/body separator (toomanyredirects)
public static null
# parse_multiple( string & $response, array $request )

Callback for transport.internal.parse_response

Callback for transport.internal.parse_response

Internal use only. Converts a raw HTTP response to a Requests_Response while still executing a multiple request.

Parameters

$response
Full response text including headers and body (will be overwritten with Response instance)
$request
Request data as passed into Requests::request_multiple()

Returns

null
$response is either set to a Requests_Response instance, or a Requests_Exception object
protected static string
# decode_chunked( string $data )

Decoded a chunked body as per RFC 2616

Decoded a chunked body as per RFC 2616

Parameters

$data
Chunked body

Returns

string
Decoded body

See

https://tools.ietf.org/html/rfc2616#section-3.6.1
public static array
# flatten( array $array )

Convert a key => value array to a 'key: value' array for headers

Convert a key => value array to a 'key: value' array for headers

Parameters

$array
Dictionary of header values

Returns

array
List of headers
public static array
# flattern( array $array )

Convert a key => value array to a 'key: value' array for headers

Convert a key => value array to a 'key: value' array for headers

Deprecated

Misspelling of Requests::flatten

Parameters

$array
Dictionary of header values

Returns

array
List of headers

Codecoverageignore

public static string
# decompress( string $data )

Decompress an encoded body

Decompress an encoded body

Implements gzip, compress and deflate. Guesses which it is by attempting to decode.

Parameters

$data
Compressed data in one of the above formats

Returns

string
Decompressed string
public static string|boolean
# compatible_gzinflate( string $gz_data )

Decompression of deflated string while staying compatible with the majority of servers.

Decompression of deflated string while staying compatible with the majority of servers.

Certain Servers will return deflated data with headers which PHP's gzinflate() function cannot handle out of the box. The following function has been created from various snippets on the gzinflate() PHP documentation.

Warning: Magic numbers within. Due to the potential different formats that the compressed data may be returned in, some "magic offsets" are needed to ensure proper decompression takes place. For a simple progmatic way to determine the magic offset in use, see: https://core.trac.wordpress.org/ticket/18273

Parameters

$gz_data
String to decompress.

Returns

string|boolean
False on failure.

Since

2.8.1

Link

https://core.trac.wordpress.org/ticket/18273
https://secure.php.net/manual/en/function.gzinflate.php#70875
https://secure.php.net/manual/en/function.gzinflate.php#77336
public static
# match_domain( $host, $reference )
Constants summary
string POST

POST method

POST method

# 'POST'
string PUT

PUT method

PUT method

# 'PUT'
string GET

GET method

GET method

# 'GET'
string HEAD

HEAD method

HEAD method

# 'HEAD'
string DELETE

DELETE method

DELETE method

# 'DELETE'
string OPTIONS

OPTIONS method

OPTIONS method

# 'OPTIONS'
string TRACE

TRACE method

TRACE method

# 'TRACE'
string PATCH

PATCH method

PATCH method

Link

https://tools.ietf.org/html/rfc5789
# 'PATCH'
integer BUFFER_SIZE

Default size of buffer size to read streams

Default size of buffer size to read streams

# 1160
string VERSION

Current version of Requests

Current version of Requests

# '1.8.1'
Properties summary
protected static array $transports

Registered transport classes

Registered transport classes

# array()
public static array $transport

Selected transport name

Selected transport name

Use Requests::get_transport() instead

# array()
protected static string $certificate_path

Default certificate path.

Default certificate path.

See

Requests::get_certificate_path()
Requests::set_certificate_path()
#
Requests 1.8.1 API Documentation API documentation generated by ApiGen