Server Freezing when using Passport Password Grants and/or Guzzle

我只是一个虾纸丫 提交于 2019-12-22 08:35:41

问题


We're adapting a laravel website into a laravel SPA, and in the future, a Mobile App that uses the web app, so we need some sort of API authentication. We're using Laravel Passport for that.

I've been using Laravel 5.5 and following the guide on the Docs, but when I try using the new login with postman, the server freezes, the HTTP requests never processing.

After some debugging, I found out that it crashes when I use Guzzle to post into the /oauth/token route. But when I use Postman to access that route, I have no problem.

This is my code:

    public function login(Request $request){
    $http = new Client();

    var_dump(1);
    //die
    $response = $http->post('http://localhost:8000/oauth/token', [  //Con postman esta ruta funciona
            'form_params' => [
            'grant_type' => 'password',
            'client_id' => env('PASSWORD-CLIENT_ID',2),
            'client_secret' => env('PASSWORD-CLIENT_SECRET',2),
            'username' => $request->username, //parece usar correo, no nombre de usuario
            'password' => $request->password,
            'scope' => '*',
        ],
    ]);

    var_dump(2);
    //die;
    return json_decode((string) $response->getBody(), true);
}

Is this a problem with Guzzle, Or with Oauth/Passport?


回答1:


I encountered the same issue, in my case it wasn't Guzzle specifically that froze, I tried curl as well. The problem lied in the fact that I was running it all on 1 instance of the built-in PHP webserver which is single-threaded.

The web server runs only one single-threaded process, so PHP applications will stall if a request is blocked.

Source: http://php.net/manual/en/features.commandline.webserver.php

When making calls to itself the thread blocked waiting for its own reply. The solution is to either seperate the providing application and consuming application into their own instance or to run it on a multi-threaded webserver such as Apache or nginx.




回答2:


same thing happened to me if i serve the application thru CLI. What I did was I changed my development to homestead or xampp virtualhost and it worked.




回答3:


If you are looking for a quick fix to test your updates - you can get this done by opening up two command prompts. The first would be running php artisan serve (locally my default port is 8000 and you would be running your site on http://localhost:8000). The second would run php artisan serve --port 8001.

Then you would update your post request to:

$response = $http->post('http://localhost:8001/oauth/token', [ 
        'form_params' => [
        'grant_type' => 'password',
        'client_id' => env('PASSWORD-CLIENT_ID',2),
        'client_secret' => env('PASSWORD-CLIENT_SECRET',2),
        'username' => $request->username,
        'password' => $request->password,
        'scope' => '*',
    ],
]);

This should help during your testing until you are able to everything on server or a local virtual host.



来源:https://stackoverflow.com/questions/50400150/server-freezing-when-using-passport-password-grants-and-or-guzzle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!