private function createResponse(array $request, $url, array $hdrs, $stream) { $parts = explode(' ', array_shift($hdrs), 3); $response = array('status' => $parts[1], 'reason' => isset($parts[2]) ? $parts[2] : null, 'headers' => Core::headersFromLines($hdrs), 'effective_url' => $url); $stream = $this->checkDecode($request, $response, $stream); // If not streaming, then drain the response into a stream. if (empty($request['client']['stream'])) { $dest = isset($request['client']['save_to']) ? $request['client']['save_to'] : fopen('php://temp', 'r+'); $stream = $this->drain($stream, $dest); } $response['body'] = $stream; return new CompletedFutureArray($response); }
/** * Creates a response hash from a cURL result. * * @param callable $handler Handler that was used. * @param array $request Request that sent. * @param array $response Response hash to update. * @param array $headers Headers received during transfer. * @param resource $body Body fopen response. * * @return array */ public static function createResponse(callable $handler, array $request, array $response, array $headers, $body) { if (isset($response['transfer_stats']['url'])) { $response['effective_url'] = $response['transfer_stats']['url']; } if (!empty($headers)) { $startLine = explode(' ', array_shift($headers), 3); $headerList = Core::headersFromLines($headers); $response['headers'] = $headerList; $response['status'] = isset($startLine[1]) ? (int) $startLine[1] : null; $response['reason'] = isset($startLine[2]) ? $startLine[2] : null; $response['body'] = $body; Core::rewindBody($response); } return !empty($response['curl']['errno']) || !isset($response['status']) ? self::createErrorResponse($handler, $request, $response) : $response; }
public function testParsesHeadersFromLinesWithMultipleLines() { $lines = ['Foo: bar', 'Foo: baz', 'Foo: 123']; $this->assertEquals(['Foo' => ['bar', 'baz', '123']], Core::headersFromLines($lines)); }