lithium\net\socket\Curl::open PHP Метод

open() публичный Метод

Opens a curl connection and initializes the internal resource handle.
public open ( array $options = [] ) : mixed
$options array update the config settings if $options['options'] exists, will be passed to $this->set()
Результат 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())
    {
        $this->options = array();
        parent::open($options);
        $config = $this->_config;
        if (empty($config['scheme']) || empty($config['host'])) {
            return false;
        }
        if (!empty($config['options'])) {
            $this->set($config['options']);
        }
        $url = "{$config['scheme']}://{$config['host']}";
        $this->_resource = curl_init($url);
        $this->set(array(CURLOPT_PORT => $config['port'], CURLOPT_HEADER => true, CURLOPT_RETURNTRANSFER => true));
        if (!is_resource($this->_resource)) {
            return false;
        }
        $this->_isConnected = true;
        $this->timeout($config['timeout']);
        if (isset($config['encoding'])) {
            $this->encoding($config['encoding']);
        }
        return $this->_resource;
    }

Usage Example

Пример #1
0
 public function testWriteAndRead()
 {
     $stream = new Curl($this->_testConfig);
     $this->assertTrue(is_resource($stream->open()));
     $this->assertTrue(is_resource($stream->resource()));
     $stream->set(CURLOPT_URL, $this->_testUrl);
     $this->assertTrue($stream->write(null));
     $this->assertTrue($stream->read());
     $response = $stream->send(new Request(), array('response' => 'lithium\\net\\http\\Response'));
     $this->assertEqual(trim(file_get_contents($this->_testUrl)), trim($response->body()));
     $this->assertNull($stream->eof());
 }
All Usage Examples Of lithium\net\socket\Curl::open