示例#1
0
 /**
  * Execute the request using PHP stream. (not recommended)
  *
  * @param   Request   $request  Request to execute
  * @return  Response
  */
 protected function _native_execute(Request $request)
 {
     // Reset the headers
     Request_Client_External::$_processed_headers = array();
     // Calculate stream mode
     $mode = $request->method() === HTTP_Request::GET ? 'r' : 'r+';
     // Process cookies
     if ($cookies = $request->cookie()) {
         $request->headers('cookie', http_build_query($cookies, NULL, '; '));
     }
     // Create the context
     $options = array($request->protocol() => array('method' => $request->method(), 'header' => (string) $request->headers(), 'content' => $request->body(), 'user-agent' => 'Kohana Framework ' . Kohana::VERSION . ' (' . Kohana::CODENAME . ')'));
     // Create the context stream
     $context = stream_context_create($options);
     stream_context_set_option($context, $this->_options);
     $stream = fopen($request->uri(), $mode, FALSE, $context);
     $meta_data = stream_get_meta_data($stream);
     // Get the HTTP response code
     $http_response = array_shift($meta_data['wrapper_data']);
     if (preg_match_all('/(\\w+\\/\\d\\.\\d) (\\d{3})/', $http_response, $matches) !== FALSE) {
         $protocol = $matches[1][0];
         $status = (int) $matches[2][0];
     } else {
         $protocol = NULL;
         $status = NULL;
     }
     // Process headers
     array_map(array('Request_Client_External', '_parse_headers'), array(), $meta_data['wrapper_data']);
     // Create a response
     $response = $request->create_response();
     $response->status($status)->protocol($protocol)->headers(Request_Client_External::$_processed_headers)->body(stream_get_contents($stream));
     // Close the stream after use
     fclose($stream);
     return $response;
 }