/** * Method to test copyTo(). * * @return void * * @covers \Windwalker\Http\Helper\StreamHelper::copyTo */ public function testCopyTo() { $dest = __DIR__ . '/test.txt'; StreamHelper::copyTo($src = new Stream(__FILE__, Stream::MODE_READ_ONLY_FROM_BEGIN), $dest); $this->assertEquals($src->__toString(), file_get_contents($dest)); @unlink($dest); }
/** * Send a request to the server and return a Response object with the response. * * @param RequestInterface $request The request object to store request params. * * @return ResponseInterface * * @since 2.1 */ protected function doRequest(RequestInterface $request) { // Create the stream context options array with the required method offset. $options = array('method' => $request->getMethod()); // Set HTTP Version $options['protocol_version'] = $request->getProtocolVersion(); // If data exists let's encode it and make sure our Content-Type header is set. $data = (string) $request->getBody(); if (isset($data)) { // If the data is a scalar value simply add it to the stream context options. if (is_scalar($data)) { $options['content'] = $data; } else { $options['content'] = http_build_query($data); } if (!$request->getHeader('Content-Type')) { $request = $request->withHeader('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8'); } // Add the relevant headers. $request = $request->withHeader('Content-Length', strlen($options['content'])); } // Speed up stream get URL // @see http://stackoverflow.com/questions/3629504/php-file-get-contents-very-slow-when-using-full-url // @see http://stackoverflow.com/questions/13679976/how-to-speed-up-file-get-contents $request = $request->withHeader('Connection', 'Close'); // Build the headers string for the request. if ($headers = $request->getHeaders()) { // Add the headers string into the stream context options array. $options['header'] = HeaderHelper::toHeaderLine($headers, true); } // If an explicit timeout is given user it. if ($this->getOption('timeout')) { $options['timeout'] = (int) $this->getOption('timeout'); } // If an explicit user agent is given use it. if ($this->getOption('userAgent')) { $options['user_agent'] = $this->getOption('userAgent'); } // Ignore HTTP errors so that we can capture them. $options['ignore_errors'] = 1; // Follow redirects. $options['follow_location'] = (int) $this->getOption('follow_location', 1); foreach ((array) $this->getOption('options') as $key => $value) { $options[$key] = $value; } // Create the stream context for the request. $context = stream_context_create(array('http' => $options)); // Capture PHP errors $php_errormsg = ''; $track_errors = ini_get('track_errors'); ini_set('track_errors', true); $connection = @fopen($request->getRequestTarget(), Stream::MODE_READ_ONLY_FROM_BEGIN, false, $context); if (!$connection) { if (!$php_errormsg) { // Error but nothing from php? Create our own $php_errormsg = sprintf('Could not connect to resource: %s', $request->getRequestTarget()); } // Restore error tracking to give control to the exception handler ini_set('track_errors', $track_errors); throw new \RuntimeException($php_errormsg); } $stream = new Stream($connection); if ($dest = $this->getOption('target_file')) { $content = ''; StreamHelper::copyTo($stream, $dest); } else { $content = $stream->getContents(); } $metadata = $stream->getMetadata(); $stream->close(); if (isset($metadata['wrapper_data']['headers'])) { $headers = $metadata['wrapper_data']['headers']; } elseif (isset($metadata['wrapper_data'])) { $headers = $metadata['wrapper_data']; } else { $headers = array(); } return $this->getResponse($headers, $content); }