Bluz\Request\RequestFactory::fromGlobals PHP Method

fromGlobals() public static method

public static fromGlobals ( array $server = null, array $query = null, array $body = null, array $cookies = null, array $files = null )
$server array
$query array
$body array
$cookies array
$files array
    public static function fromGlobals(array $server = null, array $query = null, array $body = null, array $cookies = null, array $files = null)
    {
        $server = static::normalizeServer($server ?: $_SERVER);
        $files = static::normalizeFiles($files ?: $_FILES);
        $headers = static::marshalHeaders($server);
        $request = new ServerRequest($server, $files, static::marshalUriFromServer($server, $headers), static::get('REQUEST_METHOD', $server, 'GET'), 'php://input', $headers);
        $contentType = current($request->getHeader('Content-Type'));
        $input = file_get_contents('php://input');
        // support header like "application/json" and "application/json; charset=utf-8"
        if ($contentType !== false && stristr($contentType, 'application/json')) {
            $data = (array) json_decode($input);
        } else {
            switch ($request->getMethod()) {
                case 'POST':
                    $data = $_POST;
                    break;
                default:
                    parse_str($input, $data);
                    break;
            }
        }
        return $request->withCookieParams($cookies ?: $_COOKIE)->withQueryParams($query ?: $_GET)->withParsedBody($body ?: $data);
    }

Usage Example

Beispiel #1
0
 /**
  * get CLI Request
  * @return void
  * @throws ApplicationException
  */
 public function initRequest()
 {
     $arguments = getopt("u:", ["uri:"]);
     if (!array_key_exists('u', $arguments) && !array_key_exists('uri', $arguments)) {
         throw new ApplicationException('Attribute `--uri` is required');
     }
     $uri = $arguments['u'] ?? $arguments['uri'];
     $request = RequestFactory::fromGlobals(['REQUEST_URI' => $uri, 'REQUEST_METHOD' => 'CLI']);
     Request::setInstance($request);
 }
All Usage Examples Of Bluz\Request\RequestFactory::fromGlobals
RequestFactory