RequestCore

Handles all linear and parallel HTTP requests using cURL and manages the responses.

Version

2009.12.17

Copyright

2006-2009 LifeNexus Digital, Inc., and contributors.

License

Simplified BSD Licensehttp://opensource.org/licenses/bsd-license.php

Constants

HTTP_GET

HTTP method type: Get

HTTP_POST

HTTP method type: Post

HTTP_PUT

HTTP method type: Put

HTTP_DELETE

HTTP method type: Delete

HTTP_HEAD

HTTP method type: Head

RequestCore

Container for all request-related methods.

Properties

request_url

The URL being requested.

request_headers

The headers being sent in the request.

request_body

The body being sent in the request.

response

The response returned by the request.

response_headers

The headers returned by the request.

response_body

The body returned by the request.

response_code

The HTTP status code returned by the request.

response_info

Additional response data.

curl_handle

The handle for the cURL object.

method

The method by which the request is being made.

proxy

Stores the proxy settings to use for the request.

username

The username to use for the request.

password

The password to use for the request.

curlopts

Custom CURLOPT settings.

request_class

The default class to use for HTTP Requests (defaults to RequestCore).

response_class

The default class to use for HTTP Responses (defaults to ResponseCore).

useragent

Default useragent string to use.

Functions

__construct()

public function __construct($url =  null,
$proxy =  null,
$helpers =  null)

The constructor

Access

public

Parameters

urlstring (Optional) The URL to request or service endpoint to query.
proxystring (Optional) The faux-url to use for proxy settings.  Takes the following format: proxy://user:pass@hostname:port
helpersarray (Optional) An associative array of classnames to use for request, and response functionality.  Gets passed in automatically by the calling class.

Returns

$this

set_credentials()

public function set_credentials($user,
$pass)

Sets the credentials to use for authentication.

Access

public

Parameters

userstring (Required) The username to authenticate with.
passstring (Required) The password to authenticate with.

Returns

$this

add_header()

public function add_header($key,
$value)

Adds a custom HTTP header to the cURL request.

Access

public

Parameters

keystring (Required) The custom HTTP header to set.
valuemixed (Required) The value to assign to the custom HTTP header.

Returns

$this

remove_header()

public function remove_header($key)

Removes an HTTP header from the cURL request.

Access

public

Parameters

keystring (Required) The custom HTTP header to set.

Returns

$this

set_method()

public function set_method($method)

Set the method type for the request.

Access

public

Parameters

methodstring (Required) One of the following constants: HTTP_GET, HTTP_POST, HTTP_PUT, HTTP_HEAD, HTTP_DELETE.

Returns

$this

set_useragent()

public function set_useragent($ua)

Sets a custom useragent string for the class.

Access

public

Parameters

methodstring (Required) The useragent string to use.

Returns

$this

set_body()

public function set_body($body)

Set the body to send in the request.

Access

public

Parameters

bodystring (Required) The textual content to send along in the body of the request.

Returns

$this

set_request_url()

public function set_request_url($url)

Set the URL to make the request to.

Access

public

Parameters

bodystring (Required) The textual content to send along in the body of the request.

Returns

$this

set_curlopts()

public function set_curlopts($curlopts)

Set additional CURLOPT settings.  These will merge with the default settings, and override if there is a duplicate.

Access

public

Parameters

curloptsarray (Optional) A set of key-value pairs that set CURLOPT options.  These will merge with the existing CURLOPTs, and ones passed here will override the defaults.  Keys should be the CURLOPT_* constants, not strings.

Returns

$this

set_proxy()

public function set_proxy($proxy)

Set the proxy to use for making requests.

Access

public

Parameters

proxystring (Optional) The faux-url to use for proxy settings.  Takes the following format: proxy://user:pass@hostname:port

Returns

$this

prep_request()

public function prep_request()

Prepares and adds the details of the cURL request.  This can be passed along to a curl_multi_exec() function.

Access

public

Returns

The handle for the cURL object.

process_response()

public function process_response($curl_handle =  null,
$response =  null)

Take the post-processed cURL data and break it down into useful header/body/info chunks.  Uses the data stored in the curl_handle and response properties unless replacement data is passed in via parameters.

Access

public

Parameters

curl_handlestring (Optional) The reference to the already executed cURL request.
responsestring (Optional) The actual response content itself that needs to be parsed.

Returns

ResponseCore object

send_request()

public function send_request($parse =  false)

Sends the request, calling necessary utility functions to update built-in properties.

Access

public

Parameters

parseboolean (Optional) Whether to parse the response with ResponseCore or not.

Returns

string The resulting unparsed data from the request.

send_multi_request()

public function send_multi_request($handles)

Sends the request using curl_multi_exec(), enabling parallel requests.

Access

public

Parameters

handlesarray (Required) An indexed array of cURL handles to process simultaneously.

Returns

array Post-processed cURL responses.

get_response_header()

public function get_response_header($header =  null)

Get the HTTP response headers from the request.

Access

public

Parameters

headerstring (Optional) A specific header value to return.  Defaults to all headers.

Returns

string | array All or selected header values.

get_response_body()

public function get_response_body()

Get the HTTP response body from the request.

Access

public

Returns

string The response body.

get_response_code()

public function get_response_code()

Get the HTTP response code from the request.

Access

public

Returns

string The HTTP response code.

ResponseCore

Container for all response-related methods.

Properties

header

Stores the HTTP header information.

body

Stores the SimpleXML response.

status

Stores the HTTP response code.

Functions

__construct()

public function __construct($header,  
$body,  
$status =  null)

The constructor

Access

public

Parameters

headerarray (Required) Associative array of HTTP headers (typically returned by <RequestCore::getResponseHeader()>).
bodystring (Required) XML-formatted response from AWS.
statusinteger (Optional) HTTP response status code from the request.

Returns

object Contains an array ‘header’ property (HTTP headers as an associative array), a SimpleXMLElement or string ‘body’ property, and an integer ‘status’ code.

isOK()

public function isOK($codes =  array(200, 201, 204))

Did we receive the status code we expected?

Access

public

Parameters

codesinteger|array (Optional) The status code(s) to expect.  Pass an integer for a single acceptable value, or an array of integers for multiple acceptable values.  Defaults to array 200|204.

Returns

boolean Whether we received the expected status code or not.