CakePHP 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.
HttpSocket $_auth protected array$_contentResource protected mixed$_proxy protected array$config public array$quirksMode public booleanWhen 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 public array$response public array$responseClass public string_baseConfig, _connectionErrors, _encryptMethods, connected, connection, description, encrypted, lastError 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 means you can parse queries like:
Parses the given URI and breaks it down into pieces as an indexed array with elements such as 'scheme', 'port', 'query'.
Issue the specified request. HttpSocket::get() and HttpSocket::post() wrap this method and provide a more granular interface.
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.
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.
__construct( string|array $config array() )
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 HttpSocket('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.
$config optional array() CakeSocket::__construct() _buildHeader( array $header , string $mode 'standard' )
Builds the header.
$header $mode optional 'standard' _buildRequestLine( array $request array() )
Builds a request line according to HTTP/1.1 specs. Activate quirks mode to work outside specs.
$request optional array() SocketException_buildUri( string|array $uri array() , string $uriTemplate '%scheme://%user:%pass@%host:%port/%path?%query#%fragment' )
Takes a $uri array and turns it into a fully qualified URL string
$uri optional array() $uriTemplate optional '%scheme://%user:%pass@%host:%port/%path?%query#%fragment' _configUri( string|array $uri null )
Parses and sets the specified URI into current request configuration.
$uri optional null _escapeToken( string $token , array $chars null )
Escapes a given $token according to RFC 2616 (HTTP 1.1 specs)
$token $chars optional null _parseQuery( string|array $query )
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 means you can parse queries like:
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()
$query The $query parsed into a possibly multi-level array. If an empty $query is given, an empty array is returned.
_parseUri( string|array $uri null , boolean|array $base array() )
Parses the given URI and breaks it down into pieces as an indexed array with elements such as 'scheme', 'port', 'query'.
$uri optional null $base optional array() _setProxy( )
Set the proxy configuration and authentication
SocketException_tokenEscapeChars( boolean $hex true , array $chars null )
Gets escape chars according to RFC 2616 (HTTP 1.1 specs).
$hex optional true $chars optional null buildCookies( array $cookies )
Builds cookie headers for a request.
Cookies can either be in the format returned in responses, or a simple key => value pair.
$cookies configAuth( string $method , string|array $user null , string $pass null )
Set authentication settings.
Accepts two forms of parameters. If all you need is a username + password, as with Basic authentication you can do the following:
$http->configAuth('Basic', 'mark', 'secret'); If you are using an authentication strategy that requires more inputs, like Digest authentication you can call configAuth() with an array of user information.
$http->configAuth('Digest', array(
'user' => 'mark',
'pass' => 'secret',
'realm' => 'my-realm',
'nonce' => 1235
)); To remove any set authentication strategy, call configAuth() with no parameters:
$http->configAuth();
$method $user optional null $pass optional null configProxy( string|array $host , integer $port 3128 , string $method null , string $user null , string $pass null )
Set proxy settings
$host $port optional 3128 $method optional null $user optional null $pass optional null delete( string|array $uri null , array $data array() , array $request array() )
Issues a DELETE request to the specified URI, query, and request.
$uri optional null HttpSocket::_parseUri())$data optional array() $request optional array() get( string|array $uri null , array $query array() , array $request array() )
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')
); $uri optional null $query optional array() $request optional array() head( string|array $uri null , array $query array() , array $request array() )
Issues a HEAD request to the specified URI, query, and request.
By definition HEAD request are identical to GET request except they return no response body. This means that all information and examples relevant to GET also applys to HEAD.
$uri optional null $query optional array() $request optional array() patch( string|array $uri null , array $data array() , array $request array() )
Issues a PATCH request to the specified URI, query, and request.
$uri optional null $data optional array() $request optional array() post( string|array $uri null , array $data array() , array $request array() )
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'
)); $uri optional null $data optional array() $request optional array() put( string|array $uri null , array $data array() , array $request array() )
Issues a PUT request to the specified URI, query, and request.
$uri optional null $data optional array() $request optional array() request( string|array $request array() )
Issue the specified request. HttpSocket::get() and HttpSocket::post() wrap this method and provide a more granular interface.
$request optional array() SocketExceptionreset( boolean $full true )
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.
$full optional true CakeSocket::reset() setContentResource( resource|boolean $resource )
Set the resource to receive the request content. This resource must support fwrite.
$resource SocketExceptionurl( string|array $url null , string $uriTemplate null )
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.
After configuring part of the request parameters, you can use url() to generate URLs.
$http = new HttpSocket('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.
$url optional null $uriTemplate optional null _connectionErrorHandler( integer $code , string $message )
socket_stream_client() does not populate errNum, or $errStr when there are connection errors, as in the case of SSL verification failure.
Instead we need to handle those errors manually.
$code $message _setSslContext( string $host )
Configure the SSL context options.
$host address( )
Gets the IP address of the current connection.
addresses( )
Gets all IP addresses associated with the current connection.
connect( )
Connects the socket to the given host and port.
SocketExceptioncontext( )
Gets the connection context.
disconnect( )
Disconnects the socket from the current connection.
enableCrypto( string $type , string $clientOrServer 'client' , boolean $enable true )
Encrypts current stream socket, using one of the defined encryption methods.
$type $clientOrServer optional 'client' $enable optional true SocketExceptionlastError( )
Gets the last error as a string.
read( integer $length 1024 )
Reads data from the socket. Returns false if no data is available or no connection could be established.
$length optional 1024 setLastError( integer $errNum , string $errStr )
Sets the last error.
$errNum $errStr write( string $data )
Writes data to the socket.
$data public array
Configuration settings for the HttpSocket and the requests
array(
'persistent' => false,
'host' => 'localhost',
'protocol' => 'tcp',
'port' => 80,
'timeout' => 30,
'ssl_verify_peer' => true,
'ssl_allow_self_signed' => false,
'ssl_verify_depth' => 5,
'ssl_verify_host' => true,
'request' => array(
'uri' => array(
'scheme' => array('http', 'https'),
'host' => 'localhost',
'port' => array(80, 443)
),
'redirect' => false,
'cookies' => array(),
)
) public 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.
false
public array
Contain information about the last request (read only)
array(
'method' => 'GET',
'uri' => array(
'scheme' => 'http',
'host' => null,
'port' => 80,
'user' => null,
'pass' => null,
'path' => null,
'query' => null,
'fragment' => null
),
'version' => '1.1',
'body' => '',
'line' => null,
'header' => array(
'Connection' => 'close',
'User-Agent' => 'CakePHP'
),
'raw' => null,
'redirect' => false,
'cookies' => array(),
)
© 2005–2016 The Cake Software Foundation, Inc.
Licensed under the MIT License.
CakePHP is a registered trademark of Cake Software Foundation, Inc.
We are not endorsed by or affiliated with CakePHP.
http://api.cakephp.org/2.8/class-HttpSocket.html