为什么我的邮递员永远挂着 GuzzleHttp\Client 请求?
Why my postman hang forever with with GuzzleHttp\Client request?
在 lumen 8 I 中使用 lumen-passport
得到 client_secret 和 client_id 并用 GuzzleHttp\Client 发出请求,我的邮递员永远挂起:
我的邮递员:https://imgur.com/a/LbtUIVi
在routes/web.php :
$router->group(['prefix'=>'api/v1'], function() 使用($router){
$router->post('/register','AuthController@register');
$router->post('/login', 'AuthController@login');
在我的掌控之中:
$client = new Client();
try {
$clientResponse = $client->post( 'http://localhost:8000/api/v1/oauth/token' /* config('service.passport.login_endpoint' ) */ , [
"form_params" => [
"client_secret" => 'DfiXey63ABDjgX7upuNaGmGvvASzhGq9kjZTV9nm', //config('service.passport.client_secret'),
"client_id" => 2, //config('service.passport.client_id'),
"grant_type" => "client_credentials",
"username" => $request->email,
"password" => $request->password
]
]);
\Log::info( varDump(-3, ' -3 login $clientResponse::') );
return $clientResponse;
} catch (BadResponseException $e) {
\Log::info( varDump(-4, ' -4 login ::') );
\Log::info( varDump($e->getMessage(), ' -4 login $e->getMessage()::') );
return response()->json(['status' => 'error', 'message' => $e->getMessage()]);
}
在 bootstrap/app.php 我有 :
// Enable auth middleware (shipped with Lumen)
$app->routeMiddleware([
'auth' => App\Http\Middleware\Authenticate::class,
]);
//$app->configure('app');
$app->register(App\Providers\AppServiceProvider::class);
// Enable auth middleware (shipped with Lumen)
$app->routeMiddleware([
'auth' => App\Http\Middleware\Authenticate::class,
]);
//$app->configure('app');
$app->register(App\Providers\AppServiceProvider::class);
// Finally register two service providers - original one and Lumen adapter
$app->register(Laravel\Passport\PassportServiceProvider::class);
$app->register(Dusterio\LumenPassport\PassportServiceProvider::class);
// Finally register two service providers - original one and Lumen adapter
$app->register(Laravel\Passport\PassportServiceProvider::class);
$app->register(Dusterio\LumenPassport\PassportServiceProvider::class);
...
\Dusterio\LumenPassport\LumenPassport::routes($app, ['prefix' => 'api/v1/oauth']);
在控制台下我看到了:
ProjectName$ php -S localhost:8000 -t public
[Tue Jun 1 07:27:23 2021] PHP 7.4.18 Development Server (http://localhost:8000) started
[Tue Jun 1 07:27:29 2021] 127.0.0.1:45940 Accepted
[Tue Jun 1 07:27:29 2021] 127.0.0.1:45944 Accepted
[Tue Jun 1 07:27:29 2021] 127.0.0.1:45940 [200]: GET /
[Tue Jun 1 07:27:29 2021] 127.0.0.1:45940 Closing
[Tue Jun 1 07:27:31 2021] 127.0.0.1:45948 Accepted
[Tue Jun 1 07:27:31 2021] 127.0.0.1:45944 [200]: GET /
[Tue Jun 1 07:27:31 2021] 127.0.0.1:45944 Closing
[Tue Jun 1 07:27:41 2021] 127.0.0.1:45956 Accepted
我尝试调试 guzzle 源代码,发现它挂在文件 /vendor/guzzlehttp/guzzle/src/Client.php 中,
在方法中:
private function transfer(RequestInterface $request, array $options): PromiseInterface
{
$request = $this->applyOptions($request, $options);
/** @var HandlerStack $handler */
$handler = $options['handler'];
\Log::info( varDump(-21, ' -21 transfer::') );
\Log::info( varDump($handler, ' $handler transfer::') );
try {
return P\Create::promiseFor($handler($request, $options)); // I suppose it hang forever here
} catch (\Exception $e) {
return P\Create::rejectionFor($e);
}
}
正在检查日志文件中 $handler 的内容我看到了:
[2021-06-01 04:54:47] local.INFO: (Object of GuzzleHttp\HandlerStack) : $handler transfer:: : Array
(
[ GuzzleHttp\HandlerStack handler] => Closure Object
(
[static] => Array
(
[default] => Closure Object
(
[static] => Array
(
[default] => GuzzleHttp\Handler\CurlMultiHandler Object
(
[factory:GuzzleHttp\Handler\CurlMultiHandler:private] => GuzzleHttp\Handler\CurlFactory Object
(
[handles:GuzzleHttp\Handler\CurlFactory:private] => Array
(
)
[maxHandles:GuzzleHttp\Handler\CurlFactory:private] => 50
)
[selectTimeout:GuzzleHttp\Handler\CurlMultiHandler:private] => 1
[active:GuzzleHttp\Handler\CurlMultiHandler:private] =>
[handles:GuzzleHttp\Handler\CurlMultiHandler:private] => Array
(
)
[delays:GuzzleHttp\Handler\CurlMultiHandler:private] => Array
(
)
[options:GuzzleHttp\Handler\CurlMultiHandler:private] => Array
(
)
)
[sync] => GuzzleHttp\Handler\CurlHandler Object
(
[factory:GuzzleHttp\Handler\CurlHandler:private] => GuzzleHttp\Handler\CurlFactory Object
(
[handles:GuzzleHttp\Handler\CurlFactory:private] => Array
(
)
[maxHandles:GuzzleHttp\Handler\CurlFactory:private] => 3
)
)
)
[parameter] => Array
(
[$request] => <required>
[$options] => <required>
)
)
[streaming] => GuzzleHttp\Handler\StreamHandler Object
(
[lastHeaders:GuzzleHttp\Handler\StreamHandler:private] => Array
(
)
)
)
[parameter] => Array
(
[$request] => <required>
[$options] => <required>
)
)
[ GuzzleHttp\HandlerStack stack] => Array
(
[0] => Array
(
[0] => Closure Object
(
[static] => Array
(
[bodySummarizer] =>
)
[parameter] => Array
(
[$handler] => <required>
)
)
[1] => http_errors
)
[1] => Array
(
[0] => Closure Object
(
[parameter] => Array
(
[$handler] => <required>
)
)
[1] => allow_redirects
)
[2] => Array
(
[0] => Closure Object
(
[parameter] => Array
(
[$handler] => <required>
)
)
[1] => cookies
)
[3] => Array
(
[0] => Closure Object
(
[parameter] => Array
(
[$handler] => <required>
)
)
[1] => prepare_body
)
)
[ GuzzleHttp\HandlerStack cached] =>
)
知道哪里出了问题吗?本地服务器配置错误?
PHP 7.4.18 under kubuntu
"dusterio/lumen-passport": "^0.3.4",
"guzzlehttp/guzzle": "^7.3",
"laravel/lumen-framework": "^8.0",
提前致谢!
我在 lumen 方面没有那么多经验,但我正在使用环境开发这个没有 Guzzle 的解决方案。
public function login(Request $request){
$email = $request->email;
$password = $request->password;
//Check if field is not empty
if (empty($email) or empty($password)) {
return response()->json(['status' => 'error', 'message' => 'You must fill all fields']);
}
$user = User::where('email', '=', $email)->exists();
if ($user === false) {
return response()->json(['status' => 'error', 'message' => 'User doesnt exist']);
}
//$client = new \GuzzleHttp\Client();
try{
$tokenRequest = $request->create(
env('PASSPORT_LOGIN_ENDPOINT'),
'POST'
);
$tokenRequest->request->add([
"grant_type" => "password",
"username" => $request->email,
"password" => $request->password,
"client_id" => env('PASSPORT_CLIENT_ID'),
"client_secret" => env('PASSPORT_CLIENT_SECRET'),
]);
$response = app()->handle($tokenRequest);
return $response;
} catch (\Exception $e) {
return response()->json(['status' => 'error', 'message' => $e->getMessage()]);
}
}
在 lumen 8 I 中使用 lumen-passport 得到 client_secret 和 client_id 并用 GuzzleHttp\Client 发出请求,我的邮递员永远挂起:
我的邮递员:https://imgur.com/a/LbtUIVi
在routes/web.php :
$router->group(['prefix'=>'api/v1'], function() 使用($router){
$router->post('/register','AuthController@register');
$router->post('/login', 'AuthController@login');
在我的掌控之中:
$client = new Client();
try {
$clientResponse = $client->post( 'http://localhost:8000/api/v1/oauth/token' /* config('service.passport.login_endpoint' ) */ , [
"form_params" => [
"client_secret" => 'DfiXey63ABDjgX7upuNaGmGvvASzhGq9kjZTV9nm', //config('service.passport.client_secret'),
"client_id" => 2, //config('service.passport.client_id'),
"grant_type" => "client_credentials",
"username" => $request->email,
"password" => $request->password
]
]);
\Log::info( varDump(-3, ' -3 login $clientResponse::') );
return $clientResponse;
} catch (BadResponseException $e) {
\Log::info( varDump(-4, ' -4 login ::') );
\Log::info( varDump($e->getMessage(), ' -4 login $e->getMessage()::') );
return response()->json(['status' => 'error', 'message' => $e->getMessage()]);
}
在 bootstrap/app.php 我有 :
// Enable auth middleware (shipped with Lumen)
$app->routeMiddleware([
'auth' => App\Http\Middleware\Authenticate::class,
]);
//$app->configure('app');
$app->register(App\Providers\AppServiceProvider::class);
// Enable auth middleware (shipped with Lumen)
$app->routeMiddleware([
'auth' => App\Http\Middleware\Authenticate::class,
]);
//$app->configure('app');
$app->register(App\Providers\AppServiceProvider::class);
// Finally register two service providers - original one and Lumen adapter
$app->register(Laravel\Passport\PassportServiceProvider::class);
$app->register(Dusterio\LumenPassport\PassportServiceProvider::class);
// Finally register two service providers - original one and Lumen adapter
$app->register(Laravel\Passport\PassportServiceProvider::class);
$app->register(Dusterio\LumenPassport\PassportServiceProvider::class);
...
\Dusterio\LumenPassport\LumenPassport::routes($app, ['prefix' => 'api/v1/oauth']);
在控制台下我看到了:
ProjectName$ php -S localhost:8000 -t public
[Tue Jun 1 07:27:23 2021] PHP 7.4.18 Development Server (http://localhost:8000) started
[Tue Jun 1 07:27:29 2021] 127.0.0.1:45940 Accepted
[Tue Jun 1 07:27:29 2021] 127.0.0.1:45944 Accepted
[Tue Jun 1 07:27:29 2021] 127.0.0.1:45940 [200]: GET /
[Tue Jun 1 07:27:29 2021] 127.0.0.1:45940 Closing
[Tue Jun 1 07:27:31 2021] 127.0.0.1:45948 Accepted
[Tue Jun 1 07:27:31 2021] 127.0.0.1:45944 [200]: GET /
[Tue Jun 1 07:27:31 2021] 127.0.0.1:45944 Closing
[Tue Jun 1 07:27:41 2021] 127.0.0.1:45956 Accepted
我尝试调试 guzzle 源代码,发现它挂在文件 /vendor/guzzlehttp/guzzle/src/Client.php 中, 在方法中:
private function transfer(RequestInterface $request, array $options): PromiseInterface
{
$request = $this->applyOptions($request, $options);
/** @var HandlerStack $handler */
$handler = $options['handler'];
\Log::info( varDump(-21, ' -21 transfer::') );
\Log::info( varDump($handler, ' $handler transfer::') );
try {
return P\Create::promiseFor($handler($request, $options)); // I suppose it hang forever here
} catch (\Exception $e) {
return P\Create::rejectionFor($e);
}
}
正在检查日志文件中 $handler 的内容我看到了:
[2021-06-01 04:54:47] local.INFO: (Object of GuzzleHttp\HandlerStack) : $handler transfer:: : Array
(
[ GuzzleHttp\HandlerStack handler] => Closure Object
(
[static] => Array
(
[default] => Closure Object
(
[static] => Array
(
[default] => GuzzleHttp\Handler\CurlMultiHandler Object
(
[factory:GuzzleHttp\Handler\CurlMultiHandler:private] => GuzzleHttp\Handler\CurlFactory Object
(
[handles:GuzzleHttp\Handler\CurlFactory:private] => Array
(
)
[maxHandles:GuzzleHttp\Handler\CurlFactory:private] => 50
)
[selectTimeout:GuzzleHttp\Handler\CurlMultiHandler:private] => 1
[active:GuzzleHttp\Handler\CurlMultiHandler:private] =>
[handles:GuzzleHttp\Handler\CurlMultiHandler:private] => Array
(
)
[delays:GuzzleHttp\Handler\CurlMultiHandler:private] => Array
(
)
[options:GuzzleHttp\Handler\CurlMultiHandler:private] => Array
(
)
)
[sync] => GuzzleHttp\Handler\CurlHandler Object
(
[factory:GuzzleHttp\Handler\CurlHandler:private] => GuzzleHttp\Handler\CurlFactory Object
(
[handles:GuzzleHttp\Handler\CurlFactory:private] => Array
(
)
[maxHandles:GuzzleHttp\Handler\CurlFactory:private] => 3
)
)
)
[parameter] => Array
(
[$request] => <required>
[$options] => <required>
)
)
[streaming] => GuzzleHttp\Handler\StreamHandler Object
(
[lastHeaders:GuzzleHttp\Handler\StreamHandler:private] => Array
(
)
)
)
[parameter] => Array
(
[$request] => <required>
[$options] => <required>
)
)
[ GuzzleHttp\HandlerStack stack] => Array
(
[0] => Array
(
[0] => Closure Object
(
[static] => Array
(
[bodySummarizer] =>
)
[parameter] => Array
(
[$handler] => <required>
)
)
[1] => http_errors
)
[1] => Array
(
[0] => Closure Object
(
[parameter] => Array
(
[$handler] => <required>
)
)
[1] => allow_redirects
)
[2] => Array
(
[0] => Closure Object
(
[parameter] => Array
(
[$handler] => <required>
)
)
[1] => cookies
)
[3] => Array
(
[0] => Closure Object
(
[parameter] => Array
(
[$handler] => <required>
)
)
[1] => prepare_body
)
)
[ GuzzleHttp\HandlerStack cached] =>
)
知道哪里出了问题吗?本地服务器配置错误?
PHP 7.4.18 under kubuntu
"dusterio/lumen-passport": "^0.3.4",
"guzzlehttp/guzzle": "^7.3",
"laravel/lumen-framework": "^8.0",
提前致谢!
我在 lumen 方面没有那么多经验,但我正在使用环境开发这个没有 Guzzle 的解决方案。
public function login(Request $request){
$email = $request->email;
$password = $request->password;
//Check if field is not empty
if (empty($email) or empty($password)) {
return response()->json(['status' => 'error', 'message' => 'You must fill all fields']);
}
$user = User::where('email', '=', $email)->exists();
if ($user === false) {
return response()->json(['status' => 'error', 'message' => 'User doesnt exist']);
}
//$client = new \GuzzleHttp\Client();
try{
$tokenRequest = $request->create(
env('PASSPORT_LOGIN_ENDPOINT'),
'POST'
);
$tokenRequest->request->add([
"grant_type" => "password",
"username" => $request->email,
"password" => $request->password,
"client_id" => env('PASSPORT_CLIENT_ID'),
"client_secret" => env('PASSPORT_CLIENT_SECRET'),
]);
$response = app()->handle($tokenRequest);
return $response;
} catch (\Exception $e) {
return response()->json(['status' => 'error', 'message' => $e->getMessage()]);
}
}