class Client

This class is an implemetation of an HTTP client in PHP.

It supports basic HTTP 1.0 and 1.1 requests. For a more complete implementation try Zend_Http_Client.

Methods

__construct(string $uri = null, array $config = null)

Constructor method. Will create a new HTTP client. Accepts the target URL and optionally configuration array.

setUri(string $uri)

Set the URI for the next request

string
getUri(bool $asString = true)

Get the URI for the next request

setConfig(array $config = array())

Set configuration parameters for this HTTP client

setHeaders(string $name, string $value = null)

Set a request header

setMethod(string $method)

Set the next request's method

string
getMethod()

Get the method for the next request

string|array|null
getHeader(string $key)

Get the value of a specific header

setParameterGet(string $name, string $value = null)

Set a GET parameter for the request.

string
getParameterGet(string $name)

Get a GET parameter for the request.

array
getParametersGet()

Get all the GET parameters

int
getRedirectionsCount()

Get the number of redirections done on the last request

setRawData(string|resource $data)

Set the raw (already encoded) POST data.

string
getRawData()

Get the raw (already encoded) POST data.

resetParameters(bool $clearAll = false)

Clear all GET and POST parameters

request(null|string $method = null)

Send the HTTP request and return an HTTP response object

array
prepareHeaders($host, $port)

Prepare the request headers

Details

at line 114
__construct(string $uri = null, array $config = null)

Constructor method. Will create a new HTTP client. Accepts the target URL and optionally configuration array.

Parameters

string $uri
array $config Configuration key-value pairs.

at line 132
Client setUri(string $uri)

Set the URI for the next request

Parameters

string $uri

Return Value

Client

Exceptions

InvalidArgumentException

at line 156
string getUri(bool $asString = true)

Get the URI for the next request

Parameters

bool $asString

Return Value

string

at line 169
Client setConfig(array $config = array())

Set configuration parameters for this HTTP client

Parameters

array $config

Return Value

Client

Exceptions

InvalidArgumentException

at line 192
Client setHeaders(string $name, string $value = null)

Set a request header

Parameters

string $name Header name (e.g. 'Accept')
string $value Header value or null

Return Value

Client

at line 217
Client setMethod(string $method)

Set the next request's method

Validated the passed method and sets it.

Parameters

string $method

Return Value

Client

Exceptions

InvalidArgumentException

at line 233
string getMethod()

Get the method for the next request

Return Value

string

at line 248
string|array|null getHeader(string $key)

Get the value of a specific header

Note that if the header has more than one value, an array will be returned.

Parameters

string $key

Return Value

string|array|null The header value or null if it is not set

at line 266
Client setParameterGet(string $name, string $value = null)

Set a GET parameter for the request.

Parameters

string $name
string $value

Return Value

Client

at line 286
string getParameterGet(string $name)

Get a GET parameter for the request.

Parameters

string $name

Return Value

string value

at line 300
array getParametersGet()

Get all the GET parameters

Return Value

array

at line 310
int getRedirectionsCount()

Get the number of redirections done on the last request

Return Value

int

at line 329
Client setRawData(string|resource $data)

Set the raw (already encoded) POST data.

This function is here for two reasons: 1. For advanced user who would like to set their own data, already encoded 2. For backwards compatibilty: If someone uses the old post($data) method. this method will be used to set the encoded data.

$data can also be stream (such as file) from which the data will be read.

Parameters

string|resource $data

Return Value

Client

at line 340
string getRawData()

Get the raw (already encoded) POST data.

Return Value

string

at line 358
Client resetParameters(bool $clearAll = false)

Clear all GET and POST parameters

Should be used to reset the request parameters if the client is used for several concurrent requests.

clearAll parameter controls if we clean just parameters or also headers

Parameters

bool $clearAll Should all data be cleared?

Return Value

Client

at line 388
Response request(null|string $method = null)

Send the HTTP request and return an HTTP response object

Parameters

null|string $method

Return Value

Response

Exceptions

Exception

at line 535
protected array prepareHeaders($host, $port)

Prepare the request headers

Parameters

$host
$port

Return Value

array