HttpSocket Class Info:

Class Declaration:

class HttpSocket extends CakeSocket

File name:
cake/libs/http_socket.php
Description:

Cake network socket connection class.

Core base class for HTTP network communication. HttpSocket can be used as an Object Oriented replacement for cURL in many places.

Class Inheritance

CakeSocket Object

Package
cake
Subpackage
cake.cake.libs

Properties:

Show/Hide parent properties
  • _baseConfig array

    Base configuration settings for the socket connection

  • config array

    Default configuration settings for the HttpSocket

  • connected boolean

    This boolean contains the current state of the CakeSocket class

  • connection resource

    Reference to socket connection resource

  • description string

    Object description

  • lastError array

    This variable contains an array with the last error number (num) and string (str)

  • lineBreak string

    String that represents a line break.

  • quirksMode boolean

    When one activates the $quirksMode by setting it to true, all checks meant to enforce RFC 2616 (HTTP/1.1 specs). will be disabled and additional measures to deal with non-standard responses will be enabled.

  • request array

    The default values to use for a request

  • response array

    • The default structure for storing the response
    • @var array
    • @access public

Method Summary:

Show/Hide parent methods

abort

top

Abort socket operation.

Method defined in:
cake/libs/cake_socket.php on line 253
Return

boolean Success

Access

public

address

top

Get the IP address of the current connection.

Method defined in:
cake/libs/cake_socket.php on line 155
Return

string IP address

Access

public

addresses

top

Get all IP addresses associated with the current connection.

Method defined in:
cake/libs/cake_socket.php on line 169
Return

array IP addresses

Access

public

buildCookies

top

Builds cookie headers for a request.

Parameters:
  • array $cookies required

    Array of cookies to send with the request.

Method defined in:
cake/libs/http_socket.php on line 994
Return

string Cookie header string to be sent with the request.

Access

public

Todo

Refactor token escape mechanism to be configurable

_buildHeader

top

Builds the header.

Parameters:
  • array $header required

    Header to build

  • $mode optional 'standard'

Method defined in:
cake/libs/http_socket.php on line 872
Return

string Header built from array

Access

protected

_buildRequestLine

top

Builds a request line according to HTTP/1.1 specs. Activate quirks mode to work outside specs.

Parameters:
  • array $request optional array ( )

    Needs to contain a 'uri' key. Should also contain a 'method' key, otherwise defaults to GET.

  • string $versionToken optional 'HTTP/1.1'

    The version token to use, defaults to HTTP/1.1

Method defined in:
cake/libs/http_socket.php on line 821
Return

string Request line

Access

protected

_buildUri

top

Takes a $uri array and turns it into a fully qualified URL string

Parameters:
  • mixed $uri optional array ( )

    Either A $uri array, or a request string. Will use $this->config if left empty.

  • string $uriTemplate optional '%scheme://%user:%pass@%host:%port/%path?%query#%fragment'

    The Uri template/format to use.

Method defined in:
cake/libs/http_socket.php on line 649
Return

mixed A fully qualified URL formated according to $uriTemplate, or false on failure

Access

protected

cakeError

top

Used to report user friendly errors. If there is a file app/error.php or app/app_error.php this file will be loaded error.php is the AppError class it should extend ErrorHandler class.

Parameters:
  • string $method required

    Method to be called in the error class (AppError or ErrorHandler classes)

  • array $messages optional array ( )

    Message that is to be displayed by the error class

Method defined in:
cake/libs/object.php on line 187
Return

error message

Access

public

_configUri

top

Parses and sets the specified URI into current request configuration.

Parameters:
Method defined in:
cake/libs/http_socket.php on line 616
Return

array Current configuration settings

Access

protected

connect

top

Connect the socket to the given host and port.

Method defined in:
cake/libs/cake_socket.php on line 107
Return

boolean Success

Access

public

__construct

top

Build an HTTP Socket using the specified configuration.

You can use a url string to set the url and use default configurations for all other options:

$http =& new HttpSockect('http://cakephp.org/');

Or use an array to configure multiple options:

$http =& new HttpSocket(array(
   'host' => 'cakephp.org',
   'timeout' => 20
));

See HttpSocket::$config for options that can be used.

Parameters:
  • mixed $config optional array ( )

    Configuration information, either a string url or an array of options.

Method defined in:
cake/libs/http_socket.php on line 165
Access

public

_decodeBody

top

Generic function to decode a $body with a given $encoding. Returns either an array with the keys 'body' and 'header' or false on failure.

Parameters:
  • string $body required

    A string continaing the body to decode.

  • mixed $encoding optional 'chunked'

    Can be false in case no encoding is being used, or a string representing the encoding.

Method defined in:
cake/libs/http_socket.php on line 528
Return

mixed Array of response headers and body or false.

Access

protected

_decodeChunkedBody

top

Decodes a chunked message $body and returns either an array with the keys 'body' and 'header' or false as a result.

Parameters:
  • string $body required

    A string continaing the chunked body to decode.

Method defined in:
cake/libs/http_socket.php on line 554
Return

mixed Array of response headers and body or false.

Access

protected

delete

top

Issues a DELETE request to the specified URI, query, and request.

Parameters:
  • mixed $uri optional NULL

    URI to request (see {@link _parseUri()})

  • array $data optional array ( )

    Query to append to URI

  • array $request optional array ( )

    An indexed array with indexes such as 'method' or uri

Method defined in:
cake/libs/http_socket.php on line 396
Return

mixed Result of request

Access

public

disconnect

top

Disconnect the socket from the current connection.

Method defined in:
cake/libs/cake_socket.php on line 262
Return

boolean Success

Access

public

dispatchMethod

top

Calls a method on this object with the given parameters. Provides an OO wrapper for call_user_func_array

Parameters:
  • string $method required

    Name of the method to call

  • array $params optional array ( )

    Parameter list to use when calling $method

Method defined in:
cake/libs/object.php on line 107
Return

mixed Returns the result of the method call

Access

public

_escapeToken

top

Escapes a given $token according to RFC 2616 (HTTP 1.1 specs)

Parameters:
  • string $token required

    Token to escape

  • $chars optional NULL

Method defined in:
cake/libs/http_socket.php on line 1025
Return

string Escaped token

Access

protected

Todo

Test $chars parameter

get

top

Issues a GET request to the specified URI, query, and request.

Using a string uri and an array of query string parameters:

$response = $http->get('http://google.com/search', array('q' => 'cakephp', 'client' => 'safari'));

Would do a GET request to http://google.com/search?q=cakephp&client=safari

You could express the same thing using a uri array and query string parameters:

$response = $http->get(
    array('host' => 'google.com', 'path' => '/search'),
    array('q' => 'cakephp', 'client' => 'safari')
);

Parameters:
  • mixed $uri optional NULL

    URI to request. Either a string uri, or a uri array, see HttpSocket::_parseUri()

  • array $query optional array ( )

    Querystring parameters to append to URI

  • array $request optional array ( )

    An indexed array with indexes such as 'method' or uri

Method defined in:
cake/libs/http_socket.php on line 335
Return

mixed Result of request, either false on failure or the response to the request.

Access

public

host

top

Get the host name of the current connection.

Method defined in:
cake/libs/cake_socket.php on line 141
Return

string Host name

Access

public

_httpSerialize

top

Serializes an array for transport.

Parameters:
  • array $data optional array ( )

    Data to serialize

Method defined in:
cake/libs/http_socket.php on line 855
Return

string Serialized variable

Access

protected

lastError

top

Get the last error as a string.

Method defined in:
cake/libs/cake_socket.php on line 183
Return

string Last error

Access

public

log

top

Convience method to write a message to CakeLog. See CakeLog::write() for more information on writing to logs.

Parameters:
  • string $msg required

    Log message

  • integer $type optional 2

    Error type constant. Defined in app/config/core.php.

Method defined in:
cake/libs/object.php on line 148
Return

boolean Success of log write

Access

public

Object

top

A hack to support __construct() on PHP 4 Hint: descendant classes have no PHP4 class_name() constructors, so this constructor gets called first and calls the top-layer __construct() which (if present) should call parent::__construct()

Method defined in:
cake/libs/object.php on line 43
Return

Object

parseCookies

top

Parses cookies in response headers.

Parameters:
  • array $header required

    Header array containing one ore more 'Set-Cookie' headers.

Method defined in:
cake/libs/http_socket.php on line 952
Return

mixed Either false on no cookies, or an array of cookies recieved.

Access

public

Todo

Make this 100% RFC 2965 confirm

_parseHeader

top

Parses an array based header.

Parameters:
  • array $header required

    Header as an indexed array (field => value)

Method defined in:
cake/libs/http_socket.php on line 901
Return

array Parsed header

Access

protected

_parseQuery

top

This function can be thought of as a reverse to PHP5's http_build_query(). It takes a given query string and turns it into an array and supports nesting by using the php bracket syntax. So this menas you can parse queries like:

  • ?key[subKey]=value
  • ?key[]=value1&key[]=value2

A leading '?' mark in $query is optional and does not effect the outcome of this function. For the complete capabilities of this implementation take a look at HttpSocketTest::testparseQuery()

Parameters:
  • mixed $query required

    A query string to parse into an array or an array to return directly "as is"

Method defined in:
cake/libs/http_socket.php on line 763
Return

array The $query parsed into a possibly multi-level array. If an empty $query is given, an empty array is returned.

Access

protected

_parseResponse

top

Parses the given message and breaks it down in parts.

Parameters:
  • string $message required

    Message to parse

Method defined in:
cake/libs/http_socket.php on line 464
Return

array Parsed message (with indexed elements such as raw, status, header, body)

Access

protected

_parseUri

top

Parses the given URI and breaks it down into pieces as an indexed array with elements such as 'scheme', 'port', 'query'.

Parameters:
  • string $uri optional NULL

    URI to parse

  • mixed $base optional array ( )

    If true use default URI config, otherwise indexed array to set 'scheme', 'host', 'port', etc.

Method defined in:
cake/libs/http_socket.php on line 697
Return

array Parsed URI

Access

protected

_persist

top

Checks for a persistent class file, if found file is opened and true returned If file is not found a file is created and false returned If used in other locations of the model you should choose a unique name for the persistent file There are many uses for this method, see manual for examples

Parameters:
  • string $name required

    name of the class to persist

  • $return required

  • string $object required

    the object to persist

  • $type optional NULL

Method defined in:
cake/libs/object.php on line 218
Return

boolean Success

Access

protected

Todo

add examples to manual

post

top

Issues a POST request to the specified URI, query, and request.

post() can be used to post simple data arrays to a url:

$response = $http->post('http://example.com', array(
    'username' => 'batman',
    'password' => 'bruce_w4yne'
));

Parameters:
  • mixed $uri optional NULL

    URI to request. See HttpSocket::_parseUri()

  • array $data optional array ( )

    Array of POST data keys and values.

  • array $request optional array ( )

    An indexed array with indexes such as 'method' or uri

Method defined in:
cake/libs/http_socket.php on line 368
Return

mixed Result of request, either false on failure or the response to the request.

Access

public

put

top

Issues a PUT request to the specified URI, query, and request.

Parameters:
  • mixed $uri optional NULL

    URI to request, See HttpSocket::_parseUri()

  • array $data optional array ( )

    Array of PUT data keys and values.

  • array $request optional array ( )

    An indexed array with indexes such as 'method' or uri

Method defined in:
cake/libs/http_socket.php on line 382
Return

mixed Result of request

Access

public

read

top

Read data from the socket. Returns false if no data is available or no connection could be established.

Parameters:
  • integer $length optional 1024

    Optional buffer length to read; defaults to 1024

Method defined in:
cake/libs/cake_socket.php on line 227
Return

mixed Socket data

Access

public

request

top

Issue the specified request. HttpSocket::get() and HttpSocket::post() wrap this method and provide a more granular interface.

Parameters:
  • mixed $request optional array ( )

    Either an URI string, or an array defining host/uri

Method defined in:
cake/libs/http_socket.php on line 186
Return

mixed false on error, request body on success

Access

public

requestAction

top

Calls a controller's method from any location. Can be used to connect controllers together or tie plugins into a main application. requestAction can be used to return rendered views or fetch the return value from controller actions.

Parameters:
  • mixed $url required

    String or array-based url.

  • array $extra optional array ( )

    if array includes the key "return" it sets the AutoRender to true.

Method defined in:
cake/libs/object.php on line 80
Return

mixed Boolean true or false on success/failure, or contents of rendered action if 'return' is set in $extra.

Access

public

reset

top

Resets the state of this HttpSocket instance to it's initial state (before Object::__construct got executed) or does the same thing partially for the request and the response property only.

Parameters:
  • boolean $full optional true

    If set to false only HttpSocket::response and HttpSocket::request are reseted

Method defined in:
cake/libs/http_socket.php on line 1068
Return

boolean True on success

Access

public

_savePersistent

top

You should choose a unique name for the persistent file

There are many uses for this method, see manual for examples

Parameters:
  • string $name required

    name used for object to cache

  • object $object required

    the object to persist

Method defined in:
cake/libs/object.php on line 247
Return

boolean true on save, throws error if file can not be created

Access

protected

_set

top

Allows setting of multiple properties of the object in a single line of code. Will only set properties that are part of a class declaration.

Parameters:
  • array $properties optional array ( )

    An associative array containing properties and corresponding values.

Method defined in:
cake/libs/object.php on line 166
Return

void

Access

protected

setLastError

top

Set the last error.

Parameters:
  • integer $errNum required

    Error code

  • string $errStr required

    Error string

Method defined in:
cake/libs/cake_socket.php on line 198
Access

public

_stop

top

Stop execution of the current script. Wraps exit() making testing easier.

Parameters:
  • $status optional 0

Method defined in:
cake/libs/object.php on line 135
Return

void

Access

public

_tokenEscapeChars

top

Gets escape chars according to RFC 2616 (HTTP 1.1 specs).

Parameters:
  • boolean $hex optional true

    true to get them as HEX values, false otherwise

  • $chars optional NULL

Method defined in:
cake/libs/http_socket.php on line 1039
Return

array Escape chars

Access

protected

Todo

Test $chars parameter

toString

top

Object-to-string conversion. Each class can override this method as necessary.

Method defined in:
cake/libs/object.php on line 64
Return

string The name of this class

Access

public

_unescapeToken

top

Unescapes a given $token according to RFC 2616 (HTTP 1.1 specs)

Parameters:
  • string $token required

    Token to unescape

  • $chars optional NULL

Method defined in:
cake/libs/http_socket.php on line 1011
Return

string Unescaped token

Access

protected

Todo

Test $chars parameter

url

top

Normalizes urls into a $uriTemplate. If no template is provided a default one will be used. Will generate the url using the current config information.

Usage:

After configuring part of the request parameters, you can use url() to generate urls.

$http->configUri('http://www.cakephp.org');
$url = $http->url('/search?q=bar');

Would return http://www.cakephp.org/search?q=bar

url() can also be used with custom templates:

$url = $http->url('http://www.cakephp/search?q=socket', '/%path?%query');

Would return /search?q=socket.

Parameters:
  • mixed $url optional NULL

    Either a string or array of url options to create a url with.

  • string $uriTemplate optional NULL

    A template string to use for url formatting.

Method defined in:
cake/libs/http_socket.php on line 429
Return

mixed Either false on failure or a string containing the composed url.

Access

public

write

top

Write data to the socket.

Parameters:
  • string $data required

    The data to write to the socket

Method defined in:
cake/libs/cake_socket.php on line 209
Return

boolean Success

Access

public