lithium\net\socket\Context::open PHP Method

open() public method

Opens the socket and sets its timeout value.
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.
    public function open(array $options = array())
    {
        parent::open($options);
        $config = $this->_config;
        if (!$config['scheme'] || !$config['host']) {
            return false;
        }
        $url = "{$config['scheme']}://{$config['host']}:{$config['port']}";
        $context = array($config['scheme'] => array('timeout' => $this->_timeout));
        if (is_object($config['message'])) {
            $url = $config['message']->to('url');
            $context = $config['message']->to('context', array('timeout' => $this->_timeout));
        }
        $this->_resource = fopen($url, $config['mode'], false, stream_context_create($context));
        return $this->_resource;
    }

Usage Example

Example #1
0
 public function testWriteAndRead()
 {
     $stream = new Context($this->_testConfig);
     $this->assertTrue(is_resource($stream->open()));
     $this->assertTrue(is_resource($stream->resource()));
     $response = $stream->send(new Request(), array('response' => 'lithium\\net\\http\\Response'));
     $this->assertTrue($response instanceof Response);
     $this->assertEqual(trim(file_get_contents($this->_testUrl)), trim($response->body()));
     $this->assertTrue($stream->eof());
 }
All Usage Examples Of lithium\net\socket\Context::open