问题
Set up a new project with Laravel 5.8 and I also installed Laravel Passport & Guzzle. Created a local server with php artisan serve on port 8000. Postman is able to get a response at /oauth/token (but only when the body is in JSON).
When I try to access the API through a controller method my browser returns a 500 error and no errors are showing up in the logs. When I try to point guzzle to the same controller method through a route postman keeps loading and it's even needed to force close my php process. When I try to make a get request to the API (another route) with Guzzle it also keeps hanging in postman. If I target the route directly in postman it shows the contents. Axios also returns the contents needed.
$http = new Client();
$result = $http->post('http://localhost:8000' . '/oauth/token', [
'form_params' => [
'grant_type' => 'password',
'client_id' => config('app.api_client'),
'client_secret' => config('app.api_secret'),
'username' => $user->email,
'password' => $password,
],
]);
I also tried
$http = new Client();
$result = $http->post('/oauth/token', [
'form_params' => [
'base_uri' => config('app.url'),
'headers' => [
'Accept' => 'application/json',
],
'grant_type' => 'password',
'client_id' => config('app.api_client'),
'client_secret' => config('app.api_secret'),
'username' => $user->email,
'password' => $password,
],
]);
And I also tried the first version with the headers, and both with scope => ''
, but no luck.
Does anyone know what I'm doing wrong?
回答1:
The issue is when using php artisan serve
, it uses a PHP server which is single-threaded.
The web server runs only one single-threaded process, so PHP applications will stall if a request is blocked.
You can do this solution:
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.
Or 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:
$result = $http->post('http://localhost:8001' . '/oauth/token', [
'form_params' => [
'grant_type' => 'password',
'client_id' => config('app.api_client'),
'client_secret' => config('app.api_secret'),
'username' => $user->email,
'password' => $password,
],
]);
This should help during your testing until you are able to everything on server or a local virtual host.
来源:https://stackoverflow.com/questions/56797452/laravel-passport-trying-to-get-oauth-token-guzzle-keeps-hanging-also-on-other