zendframework / zend-expressive-swoole

Swoole support for Expressive applications
BSD 3-Clause "New" or "Revised" License
158 stars 14 forks source link

$_POST and $_GET are empty #32

Closed jgalvezsoax closed 6 years ago

jgalvezsoax commented 6 years ago

Hello,

I'm trying to set my Zend Expressive application to work with Swoole through the Expressive Swoole library but I have noticed that global vars $_POST and $_GET are empty in my handlers. Also, $_SERVER miss some important keys (for this I will create another issue).

Although $_GET and $_POST are available into handler with $request->getQueryParams() and $request->getParsedBody() I think, since they are not deprecated by PHP team, sometimes is useful to access them directly, specially when you don't have access to Psr\Http\Message\ServerRequestInterface object

In next, I'll do all tests with the next invocation (I will send one get var and one post var)


POST /document?getvar=getvalue HTTP/1.1
Host: 192.168.4.20:8912
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
Authorization: Basic STFkMWdYYWNDcFNnNUpuaTZ5WmdDWTc4MzpCZWZxU0tGN1pWeXJZNDd5QTJveE5BYkNST0NuYjV3aHNtMElVaUUzY24xOFFLUW9sSg==
Cache-Control: no-cache
Postman-Token: 57cfc040-f477-4130-8725-995d4ebf7de3

------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="postvar"

postvalue
------WebKitFormBoundary7MA4YWxkTrZu0gW--

Inspecting the library I have seen in SwooleRequestHandlerRunner.php, in "onRequest", that $_GET and $_POST have no content

echo '$_GET: '. print_r($_GET, true);
$_GET: Array
(
)

echo '$_POST: '. print_r($_POST, true);
$_POST: Array
(
)

But if I print the SwooleHttpRequest content I can see that both vars exist

print_r($request);
Swoole\Http\Request Object
(
    [fd] => 2
    [header] => Array
        (
            [content-type] => multipart/form-data; boundary=--------------------------494044856722120163954730
            [cache-control] => no-cache
            [postman-token] => e13609a1-7cd9-46ac-ae47-5633c0c62150
            [authorization] => Basic STFkMWdYYWNDcFNnNUpuaTZ5WmdDWTc4MzpCZWZxU0tGN1pWeXJZNDd5QTJveE5BYkNST0NuYjV3aHNtMElVaUUzY24xOFFLUW9sSg==
            [user-agent] => PostmanRuntime/7.3.0
            [accept] => */*
            [host] => 192.168.4.20:8912
            [accept-encoding] => gzip, deflate
            [content-length] => 171
            [connection] => keep-alive
        )

    [server] => Array
        (
            [query_string] => getvar=getvalue
            [request_method] => POST
            [request_uri] => /document
            [path_info] => /document
            [request_time] => 1537955948
            [request_time_float] => 1537955948.6113
            [server_port] => 80
            [remote_port] => 49030
            [remote_addr] => 192.168.4.20
            [master_time] => 1537955948
            [server_protocol] => HTTP/1.1
            [server_software] => swoole-http-server
        )

    [request] => 
    [cookie] => 
    [get] => Array
        (
            [getvar] => getvalue
        )

    [files] => 
    [post] => Array
        (
            [postvar] => postvalue
        )

    [tmpfiles] => 
)

So, to have my get and post vars I must do something like this in SwooleRequestHandlerRunner.php, in "onRequest", to get them:

$_GET = [];
foreach($request->get as $key => $value) {
    $_GET[$key] = $value;
}

As I said at the beginning, I think $_GET and $_POST vars should have content in any place of the Zend Application.

huangzhhui commented 6 years ago

@jgalvezsoax All global functions or variables are disable in Swoole, like $_GET and $_POST and so on, use PSR-7 Request instead please.

geerteltink commented 6 years ago

You can not rely on globals when handling async requests. To be honest, it's better to always avoid them.

ezimuel commented 6 years ago

As said by @huangzhhui, the global variables $_GET and $_POST cannot be used using Swoole and should be avoided using a middleware approach with Expressive.
We manage the HTTP request using the PSR-7 standard. $_GET and $_POST are managed by$request->getQueryParams() and $request->getParsedBody(), where $request is a PSR-7 ServerRequestInterface.

jgalvezsoax commented 6 years ago

Thanks for your time