Пример #1
0
 public function testCreateFromGlobals()
 {
     $_SERVER = ['HTTPS' => 'on', 'PHP_AUTH_USER' => 'user1', 'PHP_AUTH_PW' => 'pass1', 'REQUEST_URI' => '/foo/bar?foo=bar', 'HTTP_HOST' => 'some-server.com', 'SCRIPT_NAME' => 'index.php'];
     $uri = Uri::createFromGlobals();
     $this->assertInstanceOf('Psr\\Http\\Message\\UriInterface', $uri);
     $this->assertEquals('https://*****:*****@some-server.com/foo/bar?foo=bar', (string) $uri);
 }
Пример #2
0
 /**
  * Create a request from the superglobal values
  *
  * @return ServerRequest
  */
 public static function createFromGlobal()
 {
     $method = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'GET';
     $stream = fopen('php://input', 'r');
     $streamObject = new Stream($stream);
     $headers = [];
     if (function_exists('apache_request_headers')) {
         $headers = call_user_func('apache_request_headers');
     } else {
         foreach ($_SERVER as $key => $value) {
             if (substr($key, 0, 5) === "HTTP_") {
                 $key = str_replace(" ", "-", ucwords(strtolower(str_replace("_", " ", substr($key, 5)))));
                 $headers[$key] = $value;
             } elseif (strncmp($key, 'CONTENT_', 8) === 0) {
                 $new_key = str_replace(" ", "-", ucwords(strtolower(str_replace("_", " ", $key))));
                 $headers[$new_key] = $_SERVER[$key];
             }
         }
         if (isset($_SERVER['PHP_AUTH_USER'])) {
             $pass = isset($_SERVER['PHP_AUTH_PW']) ? $_SERVER['PHP_AUTH_PW'] : '';
             $headers['AUTHORIZATION'] = 'Basic ' . base64_encode($_SERVER['PHP_AUTH_USER'] . ':' . $pass);
         }
     }
     $request = new static($method, Uri::createFromGlobals(), $streamObject, $headers, $_SERVER, $_COOKIE, UploadedFile::createFromGlobal());
     return $request->withQueryParams($_GET)->withParsedBody($_POST);
 }