diff --git a/composer.json b/composer.json index f37dc89e..44687c32 100644 --- a/composer.json +++ b/composer.json @@ -34,6 +34,7 @@ "require": { "php": ">=7.1.0", + "guzzlehttp/guzzle": "^6", "psr/log": "~1.0" }, diff --git a/src/Engine/SocketIO/Version1X.php b/src/Engine/SocketIO/Version1X.php index ea2d7ef5..1a87182f 100644 --- a/src/Engine/SocketIO/Version1X.php +++ b/src/Engine/SocketIO/Version1X.php @@ -13,6 +13,7 @@ use InvalidArgumentException; use UnexpectedValueException; +use GuzzleHttp\Client; use ElephantIO\EngineInterface; use ElephantIO\Payload\Encoder; @@ -179,12 +180,19 @@ protected function handshake() } // add customer headers + $request_headers = []; + $context[$protocol]['header'] = isset($context[$protocol]['header']) ? $context[$protocol]['header'] : []; if (isset($this->options['headers'])) { - $headers = isset($context['http']['header']) ? $context['http']['header'] : []; - $context['http']['header'] = array_merge($headers, $this->options['headers']); + $context[$protocol]['header'] = \array_merge($context[$protocol]['header'], $this->options['headers']); } - $url = \sprintf( + foreach ($context[$protocol]['header'] as $item) { + if (\preg_match('/^([^:]*):\s*([^;]*)/i', $item, $matches)) { + $request_headers[$matches[1]] = $matches[2]; + } + } + + $url = \sprintf( '%s://%s:%d/%s/?%s', $this->url['scheme'], $this->url['host'], @@ -193,7 +201,10 @@ protected function handshake() \http_build_query($query) ); - $result = @\file_get_contents($url, false, \stream_context_create($context)); + $client = new Client([]); + + $response = $client->request('GET', $url, ['headers' => $request_headers]); + $result = $response->getBody()->getContents(); if (false === $result) { $message = null; @@ -214,13 +225,7 @@ protected function handshake() throw new UnsupportedTransportException('websocket'); } - $cookies = []; - foreach ($http_response_header as $header) { - if (\preg_match('/^Set-Cookie:\s*([^;]*)/i', $header, $matches)) { - $cookies[] = $matches[1]; - } - } - $this->cookies = $cookies; + $this->cookies = $response->getHeader('Set-Cookie'); $this->session = new Session( $decoded['sid'],