On this page
Class Socket
CakePHP network socket connection class.
Core base class for network communication.
- Cake\Network\Socket uses Cake\Core\InstanceConfigTrait
Method Detail
__constructsource public
__construct( array $config [] )Constructor.
Parameters
- 
     array $configoptional []
- Socket configuration, which will be merged with the base configuration
See
Socket::$_baseConfig_connectionErrorHandlersource protected
_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.
Parameters
- 
     integer $code
- Code number.
- 
     string $message
- Message.
_setSslContextsource protected
_setSslContext( string $host )Configure the SSL context options.
Parameters
- 
     string $host
- The host name being connected to.
addresssource public
address( )Get the IP address of the current connection.
Returns
stringIP address
addressessource public
addresses( )Get all IP addresses associated with the current connection.
Returns
arrayIP addresses
connectsource public
connect( )Connect the socket to the given host and port.
Returns
booleanSuccess
Throws
Cake\Network\Exception\SocketException\Cake\Network\Exception\SocketException
contextsource public
context( )Get the connection context.
Returns
null|arrayNull when there is no connection, an array when there is.
disconnectsource public
disconnect( )Disconnect the socket from the current connection.
Returns
booleanSuccess
enableCryptosource public
enableCrypto( string $type , string $clientOrServer 'client' , boolean $enable true )Encrypts current stream socket, using one of the defined encryption methods
Parameters
- 
     string $type
- can be one of 'ssl2', 'ssl3', 'ssl23' or 'tls'
- 
     string $clientOrServeroptional 'client'
- can be one of 'client', 'server'. Default is 'client'
- 
     boolean $enableoptional true
- enable or disable encryption. Default is true (enable)
Returns
booleanTrue on success
Throws
InvalidArgumentExceptionWhen an invalid encryption scheme is chosen.
Cake\Network\Exception\SocketExceptionWhen attempting to enable SSL/TLS fails
See
stream_socket_enable_cryptoreadsource public
read( integer $length 1024 )Read data from the socket. Returns false if no data is available or no connection could be established.
Parameters
- 
     integer $lengthoptional 1024
- Optional buffer length to read; defaults to 1024
Returns
mixedSocket data
resetsource public
reset( array $state null )Resets the state of this Socket instance to it's initial state (before Object::__construct got executed)
Parameters
- 
     array $stateoptional null
- Array with key and values to reset
Returns
booleanTrue on success
setLastErrorsource public
setLastError( integer $errNum , string $errStr )Set the last error.
Parameters
- 
     integer $errNum
- Error code
- 
     string $errStr
- Error string
writesource public
write( string $data )Write data to the socket.
Parameters
- 
     string $data
- The data to write to the socket
Returns
booleanSuccess
Methods used from Cake\Core\InstanceConfigTrait
_configDeletesource protected
_configDelete( string $key )Delete a single config key
Parameters
- 
     string $key
- Key to delete.
Throws
Cake\Core\Exception\Exceptionif attempting to clobber existing config
_configReadsource protected
_configRead( string|null $key )Read a config variable
Parameters
- 
     string|null $key
- Key to read.
Returns
mixedmixed
_configWritesource protected
_configWrite( string|array $key , mixed $value , boolean|string $merge false )Write a config variable
Parameters
- 
     string|array $key
- Key to write to.
- 
     mixed $value
- Value to write.
- 
     boolean|string $mergeoptional false
- True to merge recursively, 'shallow' for simple merge, false to overwrite, defaults to false.
Throws
Cake\Core\Exception\Exceptionif attempting to clobber existing config
configsource public
config( string|array|null $key null , mixed|null $value null , boolean $merge true )Usage
Reading the whole config:
$this->config();
Reading a specific value:
$this->config('key');
Reading a nested value:
$this->config('some.nested.key');
Setting a specific value:
$this->config('key', $value);
Setting a nested value:
$this->config('some.nested.key', $value);
Updating multiple config settings at the same time:
$this->config(['one' => 'value', 'another' => 'value']);
Parameters
- 
     string|array|null $keyoptional null
- The key to get/set, or a complete array of configs.
- 
     mixed|null $valueoptional null
- The value to set.
- 
     boolean $mergeoptional true
- Whether to recursively merge or overwrite existing config, defaults to true.
Returns
mixedConfig value being read, or the object itself on write operations.
Throws
Cake\Core\Exception\ExceptionWhen trying to set a key that is invalid.
configShallowsource public
configShallow( string|array $key , mixed|null $value null )Merge provided config with existing config. Unlike config() which does a recursive merge for nested keys, this method does a simple merge.
Setting a specific value:
$this->config('key', $value);
Setting a nested value:
$this->config('some.nested.key', $value);
Updating multiple config settings at the same time:
$this->config(['one' => 'value', 'another' => 'value']);
Parameters
- 
     string|array $key
- The key to set, or a complete array of configs.
- 
     mixed|null $valueoptional null
- The value to set.
Returns
mixed$this The object itself.
Properties summary
$_connectionErrorssource
protected array
Used to capture connection warnings which can happen when there are SSL errors for example.
[]$_defaultConfigsource
protected array
Default configuration settings for the socket connection
[
    'persistent' => false,
    'host' => 'localhost',
    'protocol' => 'tcp',
    'port' => 80,
    'timeout' => 30
]$_encryptMethodssource
protected array
Contains all the encryption methods available
[
    // @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
]$encryptedsource
public boolean
True if the socket stream is encrypted after a Cake\Network\Socket::enableCrypto() call
false$lastErrorsource
public array
This variable contains an array with the last error number (num) and string (str)
[]Properties used from Cake\Core\InstanceConfigTrait
$_configInitializedsource
protected boolean
Whether the config property has already been configured with defaults
false© 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/3.1/class-Cake.Network.Socket.html