Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

php - Guzzle Cannot make GET request to the localhost (port: 80, 8000, 8080, etc )

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,

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...