public open ( array $options = [] ) : mixed | ||
$options | array | update the config settings |
return | mixed | Returns `false` if the socket configuration does not contain the `'scheme'` or `'host'` settings, or if configuration fails, otherwise returns a resource stream. Throws exception if there is a network error. |
public function open(array $options = array())
{
parent::open($options);
$config = $this->_config;
if (!$config['scheme'] || !$config['host']) {
return false;
}
$scheme = $config['scheme'] !== 'udp' ? 'tcp' : 'udp';
$port = $config['port'] ?: 80;
$host = "{$scheme}://{$config['host']}:{$port}";
$flags = STREAM_CLIENT_CONNECT;
if ($config['persistent']) {
$flags = STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT;
}
$errorCode = $errorMessage = null;
$this->_resource = stream_socket_client($host, $errorCode, $errorMessage, $config['timeout'], $flags);
if ($errorCode || $errorMessage) {
throw new NetworkException($errorMessage);
}
$this->timeout($config['timeout']);
if (!empty($config['encoding'])) {
$this->encoding($config['encoding']);
}
return $this->_resource;
}
public function testSend() { $stream = new Stream($this->_testConfig); $result = $stream->open(); $data = "GET / HTTP/1.1\r\n"; $data .= "Host: localhost\r\n"; $data .= "Connection: Close\r\n\r\n"; $result = $stream->send($data, array('classes' => array('response' => '\\lithium\\net\\http\\Response'))); $this->assertNotEqual(null, $result); }