问题
Currently using Laravel 5.5 and Guzzle that comes together with the laravel installer.
I am trying to make GET request (error occur with other HTTP requests as well) but don't seem work.
This code does not work:
public function callback(Request $request)
{
$code = $request->code;
$client = new Client(['exceptions' => false]);
try {
$response = $client->request('GET', 'http://localhost/api/tests');
// $response = $http->request('POST', Config::get('app.url') . '/oauth/token', [
// 'form_params' => [
// 'grant_type' => 'authorization_code',
// 'client_id' => Config::get('oauth_client.client_id'),
// 'client_secret' => Config::get('oauth_client.client_secret'),
// 'redirect_uri' => Config::get('oauth_client.redirect_uri'),
// 'code' => $code,
// ],
// ]);
// return json_decode((string) $response->getBody(), true);
} catch (\Exception $e) {
dd($e);
}
dd($response->getBody());
return;
}
But this code below is work very well
public function callback(Request $request)
{
$code = $request->code;
$client = new Client(['exceptions' => false]);
try {
$response = $client->request('GET', 'https://www.google.co.id');
// $response = $http->request('POST', Config::get('app.url') . '/oauth/token', [
// 'form_params' => [
// 'grant_type' => 'authorization_code',
// 'client_id' => Config::get('oauth_client.client_id'),
// 'client_secret' => Config::get('oauth_client.client_secret'),
// 'redirect_uri' => Config::get('oauth_client.redirect_uri'),
// 'code' => $code,
// ],
// ]);
// return json_decode((string) $response->getBody(), true);
} catch (\Exception $e) {
dd($e);
}
dd($response->getBody());
return;
}
I'm not understand why my Guzzle able to request to google.com but unable to connect to my own localhost server (to all ports).
Any help will greatly appreciate.
Thanks,
回答1:
The reason why it does not work is that php artisan serve
uses the PHP built-in web server and this is single threaded. So if you run your application it cannot make another request (your Guzzle request) until it finishes the initial request. That is why it hangs (as mentioned here).
One solution is (as you pointed out) to use a real webserver that is multi-threaded.
But if you still want to use php artisan serve
instead of a web server like Nginx, there is an easy solution (that I already posted in another issue):
You could run another web server instance with another port and configure your application to use this base_uri
when connecting to your API:
php artisan serve \\ defaults to port 8000
\\ in another console
php artisan serve --port=8001
$client->request('GET', 'http://localhost:8001/api/tests')
回答2:
Using Apache2 and Virtual Host for my development server solved the problem, this approach more realistic than making any changes on my Guzzle parameters.
So my conclusion is Better to use real web server (Nginx, Apache) rather than artisan serve.
回答3:
set 'CURLOPT_SSL_VERIFYPEER'
option to false
public function callback(Request $request)
{
$code = $request->code;
$client = new Client(['exceptions' => false, 'CURLOPT_SSL_VERIFYPEER' => false]);
try {
$response = $client->request('GET', 'http://localhost/api/tests');
// $response = $http->request('POST', Config::get('app.url') . '/oauth/token', [
// 'form_params' => [
// 'grant_type' => 'authorization_code',
// 'client_id' => Config::get('oauth_client.client_id'),
// 'client_secret' => Config::get('oauth_client.client_secret'),
// 'redirect_uri' => Config::get('oauth_client.redirect_uri'),
// 'code' => $code,
// ],
// ]);
// return json_decode((string) $response->getBody(), true);
} catch (\Exception $e) {
dd($e);
}
dd($response->getBody());
return;
}
来源:https://stackoverflow.com/questions/48841018/guzzle-cannot-make-get-request-to-the-localhost-port-80-8000-8080-etc