RequestCore

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

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

package

LOG_PHP_SDK

Methods

Constructs a new instance of this class.

__construct(string $url = null, string $proxy = null, array $helpers = null) : $this

Arguments

$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.

Response

$this

A reference to the current instance.

Destructs the instance. Closes opened file handles.

__destruct() : $this

Response

$this

A reference to the current instance.

Adds a custom HTTP header to the cURL request.

add_header(string $key, mixed $value) : $this

Arguments

$key

string

(Required) The custom HTTP header to set.

$value

mixed

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

Response

$this

A reference to the current instance.

Get the HTTP response body from the request.

get_response_body() : string

Response

string

The response body.

Get the HTTP response code from the request.

get_response_code() : string

Response

string

The HTTP response code.

Get the HTTP response headers from the request.

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

Arguments

$header

string

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

Response

string|array

All 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

Response

resource

The 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

Arguments

$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.

Response

\ResponseCore

A 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) : $this

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.

Arguments

$callback

string|array|\function

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

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

Response

$this

A 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) : $this

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.

Arguments

$callback

string|array|\function

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

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

Response

$this

A reference to the current instance.

Removes an HTTP header from the cURL request.

remove_header(string $key) : $this

Arguments

$key

string

(Required) The custom HTTP header to set.

Response

$this

A 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

Arguments

$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.
  • <li><code>limit</code> - <code>integer</code> - 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.</li></ul>

Response

array

Post-processed cURL responses.

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

send_request(boolean $parse = false) : string

Arguments

$parse

boolean

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

Response

string

The resulting unparsed data from the request.

Set the body to send in the request.

set_body(string $body) : $this

Arguments

$body

string

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

Response

$this

A reference to the current instance.

Sets the credentials to use for authentication.

set_credentials(string $user, string $pass) : $this

Arguments

$user

string

(Required) The username to authenticate with.

$pass

string

(Required) The password to authenticate with.

Response

$this

A 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) : $this

Arguments

$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.

Response

$this

A reference to the current instance.

Set the method type for the request.

set_method(string $method) : $this

Arguments

$method

string

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

Response

$this

A reference to the current instance.

Set the proxy to use for making requests.

set_proxy(string $proxy) : $this

Arguments

$proxy

string

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

Response

$this

A reference to the current instance.

Sets the file to read from while streaming up.

set_read_file(string $location) : $this

Arguments

$location

string

(Required) The readable location to read from.

Response

$this

A 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) : $this

Arguments

$resource

resource

(Required) The readable resource to read from.

$size

integer

(Optional) The size of the stream to read.

Response

$this

A reference to the current instance.

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

set_read_stream_size(integer $size) : $this

Arguments

$size

integer

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

Response

$this

A reference to the current instance.

Set the URL to make the request to.

set_request_url(string $url) : $this

Arguments

$url

string

(Required) The URL to make the request to.

Response

$this

A reference to the current instance.

Set the intended starting seek position.

set_seek_position(integer $position) : $this

Arguments

$position

integer

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

Response

$this

A reference to the current instance.

Sets a custom useragent string for the class.

set_useragent(string $ua) : $this

Arguments

$ua

string

(Required) The useragent string to use.

Response

$this

A reference to the current instance.

Sets the file to write to while streaming down.

set_write_file(string $location) : $this

Arguments

$location

string

(Required) The writeable location to write to.

Response

$this

A reference to the current instance.

Sets the resource to write to while streaming down.

set_write_stream(resource $resource) : $this

Arguments

$resource

resource

(Required) The writeable resource to write to.

Response

$this

A 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

Arguments

$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.

Response

\binary

Binary data from a stream.

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

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

Arguments

$curl_handle

resource

(Required) The cURL handle for the request.

$data

\binary

(Required) The data to write.

Response

integer

The number of bytes written.

Constants

GET HTTP Method

HTTP_GET

POST HTTP Method

HTTP_POST

PUT HTTP Method

HTTP_PUT

DELETE HTTP Method

HTTP_DELETE

HEAD HTTP Method

HTTP_HEAD

Properties

The URL being requested.

request_url : 

Type(s)

The headers being sent in the request.

request_headers : 

Type(s)

The body being sent in the request.

request_body : 

Type(s)

The response returned by the request.

response : 

Type(s)

The headers returned by the request.

response_headers : 

Type(s)

The body returned by the request.

response_body : 

Type(s)

The HTTP status code returned by the request.

response_code : 

Type(s)

Additional response data.

response_info : 

Type(s)

The handle for the cURL object.

curl_handle : 

Type(s)

The method by which the request is being made.

method : 

Type(s)

Stores the proxy settings to use for the request.

proxy : 

Type(s)

The username to use for the request.

username : 

Type(s)

The password to use for the request.

password : 

Type(s)

Custom CURLOPT settings.

curlopts : 

Type(s)

The state of debug mode.

debug_mode : 

Type(s)

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

request_class : 

Type(s)

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

response_class : 

Type(s)

Default useragent string to use.

useragent : 

Type(s)

File to read from while streaming up.

read_file : 

Type(s)

The resource to read from while streaming up.

read_stream : 

Type(s)

The size of the stream to read from.

read_stream_size : 

Type(s)

The length already read from the stream.

read_stream_read : 

Type(s)

File to write to while streaming down.

write_file : 

Type(s)

The resource to write to while streaming down.

write_stream : 

Type(s)

Stores the intended starting seek position.

seek_position : 

Type(s)

The location of the cacert.pem file to use.

cacert_location : 

Type(s)

The state of SSL certificate verification.

ssl_verification : 

Type(s)

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

registered_streaming_read_callback : 

Type(s)

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

registered_streaming_write_callback : 

Type(s)