Handles all HTTP requests using cURL and manages the responses.

version 2011.06.07
copyright 2006-2011 Ryan Parman
copyright 2006-2010 Foleeo Inc.
copyright 2010-2011 Amazon.com, Inc. or its affiliates.
copyright 2008-2011 Contributors
license Simplified BSD License
package SLS_PHP_SDK

 Methods

Constructs a new instance of this class.

__construct(string $url = null, string $proxy = null, array $helpers = null) : \RequestCore
fluent This method is part of a fluent interface and will return the same instance

Parameters

$url

string

(Optional) The URL to request or service endpoint to query.

$proxy

string

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

$helpers

array

(Optional) An associative array of classnames to use for request, and response functionality. Gets passed in automatically by the calling class.

Returns

\RequestCoreA reference to the current instance.

Destructs the instance. Closes opened file handles.

__destruct() : \RequestCore
fluent This method is part of a fluent interface and will return the same instance

Returns

\RequestCoreA reference to the current instance.

Adds a custom HTTP header to the cURL request.

add_header(string $key, mixed $value) : \RequestCore
fluent This method is part of a fluent interface and will return the same instance

Parameters

$key

string

(Required) The custom HTTP header to set.

$value

mixed

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

Returns

\RequestCoreA reference to the current instance.

Get the HTTP response body from the request.

get_response_body() : string

Returns

stringThe response body.

Get the HTTP response code from the request.

get_response_code() : string

Returns

stringThe HTTP response code.

Get the HTTP response headers from the request.

get_response_header(string $header = null) : string | array

Parameters

$header

string

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

Returns

stringarrayAll or selected header values.

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

prep_request() : resource

Returns

resourceThe handle for the cURL object.

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.

process_response(resource $curl_handle = null, string $response = null) : \ResponseCore

Parameters

$curl_handle

resource

(Optional) The reference to the already executed cURL request.

$response

string

(Optional) The actual response content itself that needs to be parsed.

Returns

\ResponseCoreA object containing a parsed HTTP response.

Register a callback function to execute whenever a data stream is read from using <CFRequest::streaming_read_callback()>.

register_streaming_read_callback(string|array|\function $callback) : \RequestCore

The user-defined callback function should accept three arguments:

  • $curl_handle - resource - Required - The cURL handle resource that represents the in-progress transfer.
  • $file_handle - resource - Required - The file handle resource that represents the file on the local file system.
  • $length - integer - Required - The length in kilobytes of the data chunk that was transferred.
fluent This method is part of a fluent interface and will return the same instance

Parameters

$callback

stringarray\function

(Required) The callback function is called by , so you can pass the following values:

  • The name of a global function to execute, passed as a string.
  • A method to execute, passed as array('ClassName', 'MethodName').
  • An anonymous function (PHP 5.3+).

Returns

\RequestCoreA reference to the current instance.

Register a callback function to execute whenever a data stream is written to using <CFRequest::streaming_write_callback()>.

register_streaming_write_callback(string|array|\function $callback) : \RequestCore

The user-defined callback function should accept two arguments:

  • $curl_handle - resource - Required - The cURL handle resource that represents the in-progress transfer.
  • $length - integer - Required - The length in kilobytes of the data chunk that was transferred.
fluent This method is part of a fluent interface and will return the same instance

Parameters

$callback

stringarray\function

(Required) The callback function is called by , so you can pass the following values:

  • The name of a global function to execute, passed as a string.
  • A method to execute, passed as array('ClassName', 'MethodName').
  • An anonymous function (PHP 5.3+).

Returns

\RequestCoreA reference to the current instance.

Removes an HTTP header from the cURL request.

remove_header(string $key) : \RequestCore
fluent This method is part of a fluent interface and will return the same instance

Parameters

$key

string

(Required) The custom HTTP header to set.

Returns

\RequestCoreA reference to the current instance.

Sends the request using <php:curl_multi_exec()>, enabling parallel requests. Uses the "rolling" method.

send_multi_request(array $handles, array $opt = null) : array

Parameters

$handles

array

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

$opt

array

(Optional) An associative array of parameters that can have the following keys:

  • callback - string|array - Optional - The string name of a function to pass the response data to. If this is a method, pass an array where the [0] index is the class and the [1] index is the method name.
  • limit - integer - Optional - The number of simultaneous requests to make. This can be useful for scaling around slow server responses. Defaults to trusting cURLs judgement as to how many to use.

Returns

arrayPost-processed cURL responses.

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

send_request(boolean $parse = false) : string

Parameters

$parse

boolean

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

Returns

stringThe resulting unparsed data from the request.

Set the body to send in the request.

set_body(string $body) : \RequestCore
fluent This method is part of a fluent interface and will return the same instance

Parameters

$body

string

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

Returns

\RequestCoreA reference to the current instance.

Sets the credentials to use for authentication.

set_credentials(string $user, string $pass) : \RequestCore
fluent This method is part of a fluent interface and will return the same instance

Parameters

$user

string

(Required) The username to authenticate with.

$pass

string

(Required) The password to authenticate with.

Returns

\RequestCoreA reference to the current instance.

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

set_curlopts(array $curlopts) : \RequestCore
fluent This method is part of a fluent interface and will return the same instance

Parameters

$curlopts

array

(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

\RequestCoreA reference to the current instance.

Set the method type for the request.

set_method(string $method) : \RequestCore
fluent This method is part of a fluent interface and will return the same instance

Parameters

$method

string

(Required) One of the following constants: , , , , .

Returns

\RequestCoreA reference to the current instance.

Set the proxy to use for making requests.

set_proxy(string $proxy) : \RequestCore
fluent This method is part of a fluent interface and will return the same instance

Parameters

$proxy

string

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

Returns

\RequestCoreA reference to the current instance.

Sets the file to read from while streaming up.

set_read_file(string $location) : \RequestCore
fluent This method is part of a fluent interface and will return the same instance

Parameters

$location

string

(Required) The readable location to read from.

Returns

\RequestCoreA reference to the current instance.

Sets the resource to read from while streaming up. Reads the stream from its current position until EOF or `$size` bytes have been read. If `$size` is not given it will be determined by <php:fstat()> and <php:ftell()>.

set_read_stream(resource $resource, integer $size = null) : \RequestCore
fluent This method is part of a fluent interface and will return the same instance

Parameters

$resource

resource

(Required) The readable resource to read from.

$size

integer

(Optional) The size of the stream to read.

Returns

\RequestCoreA reference to the current instance.

Sets the length in bytes to read from the stream while streaming up.

set_read_stream_size(integer $size) : \RequestCore
fluent This method is part of a fluent interface and will return the same instance

Parameters

$size

integer

(Required) The length in bytes to read from the stream.

Returns

\RequestCoreA reference to the current instance.

Set the URL to make the request to.

set_request_url(string $url) : \RequestCore
fluent This method is part of a fluent interface and will return the same instance

Parameters

$url

string

(Required) The URL to make the request to.

Returns

\RequestCoreA reference to the current instance.

Set the intended starting seek position.

set_seek_position(integer $position) : \RequestCore
fluent This method is part of a fluent interface and will return the same instance

Parameters

$position

integer

(Required) The byte-position of the stream to begin reading from.

Returns

\RequestCoreA reference to the current instance.

Sets a custom useragent string for the class.

set_useragent(string $ua) : \RequestCore
fluent This method is part of a fluent interface and will return the same instance

Parameters

$ua

string

(Required) The useragent string to use.

Returns

\RequestCoreA reference to the current instance.

Sets the file to write to while streaming down.

set_write_file(string $location) : \RequestCore
fluent This method is part of a fluent interface and will return the same instance

Parameters

$location

string

(Required) The writeable location to write to.

Returns

\RequestCoreA reference to the current instance.

Sets the resource to write to while streaming down.

set_write_stream(resource $resource) : \RequestCore
fluent This method is part of a fluent interface and will return the same instance

Parameters

$resource

resource

(Required) The writeable resource to write to.

Returns

\RequestCoreA reference to the current instance.

A callback function that is invoked by cURL for streaming up.

streaming_read_callback(resource $curl_handle, resource $file_handle, integer $length) : \binary

Parameters

$curl_handle

resource

(Required) The cURL handle for the request.

$file_handle

resource

(Required) The open file handle resource.

$length

integer

(Required) The maximum number of bytes to read.

Returns

\binaryBinary data from a stream.

A callback function that is invoked by cURL for streaming down.

streaming_write_callback(resource $curl_handle, \binary $data) : integer

Parameters

$curl_handle

resource

(Required) The cURL handle for the request.

$data

\binary

(Required) The data to write.

Returns

integerThe number of bytes written.

 Properties

 

The location of the cacert.pem file to use.

$cacert_location 

Default

false
 

The handle for the cURL object.

$curl_handle 

Default

 

Custom CURLOPT settings.

$curlopts 

Default

null
 

The state of debug mode.

$debug_mode 

Default

false
 

The method by which the request is being made.

$method 

Default

 

The password to use for the request.

$password 

Default

null
 

Stores the proxy settings to use for the request.

$proxy 

Default

null
 

File to read from while streaming up.

$read_file 

Default

null
 

The resource to read from while streaming up.

$read_stream 

Default

null
 

The length already read from the stream.

$read_stream_read 

Default

0
 

The size of the stream to read from.

$read_stream_size 

Default

null
 

The user-defined callback function to call when a stream is read from.

$registered_streaming_read_callback 

Default

null
 

The user-defined callback function to call when a stream is written to.

$registered_streaming_write_callback 

Default

null
 

The body being sent in the request.

$request_body 

Default

 

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

$request_class 

Default

'RequestCore'
 

The headers being sent in the request.

$request_headers 

Default

 

The URL being requested.

$request_url 

Default

 

The response returned by the request.

$response 

Default

 

The body returned by the request.

$response_body 

Default

 

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

$response_class 

Default

'ResponseCore'
 

The HTTP status code returned by the request.

$response_code 

Default

 

The headers returned by the request.

$response_headers 

Default

 

Additional response data.

$response_info 

Default

 

Stores the intended starting seek position.

$seek_position 

Default

null
 

The state of SSL certificate verification.

$ssl_verification 

Default

true
 

Default useragent string to use.

$useragent 

Default

'RequestCore/1.4.3'
 

The username to use for the request.

$username 

Default

null
 

File to write to while streaming down.

$write_file 

Default

null
 

The resource to write to while streaming down.

$write_stream 

Default

null

 Constants

 

DELETE HTTP Method

HTTP_DELETE = 'DELETE' 
 

GET HTTP Method

HTTP_GET = 'GET' 
 

HEAD HTTP Method

HTTP_HEAD = 'HEAD' 
 

POST HTTP Method

HTTP_POST = 'POST' 
 

PUT HTTP Method

HTTP_PUT = 'PUT'