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.
__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' string
_buildRequestLine( array $request array() )
Builds a request line according to HTTP/1.1 specs. Activate quirks mode to work outside specs.
$request
optional array() string
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' mixed
_configUri( string|array $uri null )
Parses and sets the specified URI into current request configuration.
$uri
optional null boolean
_escapeToken( string $token , array $chars null )
Escapes a given $token according to RFC 2616 (HTTP 1.1 specs)
$token
$chars
optional null string
_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
array
_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() array
_setAuth( )
Set authentication in request
SocketException
_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 array
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
string
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() mixed
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() mixed
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() mixed
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() mixed
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() mixed
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() mixed
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() mixed
SocketException
reset( 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 boolean
CakeSocket::reset()
setContentResource( resource|boolean $resource )
Set the resource to receive the request content. This resource must support fwrite.
$resource
SocketException
url( 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 mixed
_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.
string
addresses( )
Gets all IP addresses associated with the current connection.
array
connect( )
Connects the socket to the given host and port.
boolean
SocketException
context( )
Gets the connection context.
null|array
disconnect( )
Disconnects the socket from the current connection.
boolean
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 boolean
InvalidArgumentException
SocketException
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 mixed
setLastError( integer $errNum , string $errStr )
Sets the last error.
$errNum
$errStr
write( string $data )
Writes data to the socket.
$data
boolean
protected array
Base configuration settings for the socket connection
array( 'persistent' => false, 'host' => 'localhost', 'protocol' => 'tcp', 'port' => 80, 'timeout' => 30 )
protected array
Used to capture connection warnings which can happen when there are SSL errors for example.
array()
protected array
Contains all the encryption methods available
array( // @codingStandardsIgnoreStart 'sslv2_client' => STREAM_CRYPTO_METHOD_SSLv2_CLIENT, 'sslv3_client' => STREAM_CRYPTO_METHOD_SSLv3_CLIENT, 'sslv23_client' => STREAM_CRYPTO_METHOD_SSLv23_CLIENT, 'tls_client' => STREAM_CRYPTO_METHOD_TLS_CLIENT, 'sslv2_server' => STREAM_CRYPTO_METHOD_SSLv2_SERVER, 'sslv3_server' => STREAM_CRYPTO_METHOD_SSLv3_SERVER, 'sslv23_server' => STREAM_CRYPTO_METHOD_SSLv23_SERVER, 'tls_server' => STREAM_CRYPTO_METHOD_TLS_SERVER // @codingStandardsIgnoreEnd )
public boolean
This boolean contains the current state of the CakeSocket class
false
public boolean
True if the socket stream is encrypted after a CakeSocket::enableCrypto() call
false
public array
This variable contains an array with the last error number (num) and string (str)
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.7/class-HttpSocket.html