Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel server hangs whenever I try to request localhost:8000/any using guzzle

If I make any request to http://localhost:8000 or http://127.0.0.1:8000 it hangs on status pending. (Exactly as here https://github.com/guzzle/guzzle/issues/1857)

I was told that it isn't related to guzzle and that I should better ask about it here.

I stumbled upon this problem while following laravel.com/docs/5.4/passport

This is the code that hangs:

$response = $http->post('http://your-app.com/oauth/token', [
    'form_params' => [
        'grant_type' => 'authorization_code',
        'client_id' => 'client-id',
        'client_secret' => 'client-secret',
        'redirect_uri' => 'http://example.com/callback',
        'code' => $request->code,
    ],
]);

I tried making GET and POST request to working API routes (tested with postman) and it still hangs when calling the same routes using guzzle.

So is there a way to make requests to my own API while using php artisan serve?

like image 462
Carl Kroeger Ihl Avatar asked Jul 03 '17 07:07

Carl Kroeger Ihl


People also ask

How to run Laravel application on another port in Laravel?

First one is run your Laravel server on other port with the bellow command. Second way is to kill service which runs on the port 8000 and then run your Laravel application on port 8000. For that first run bellow command, it will display servies which runs on the port.

What is local host in Laravel?

In Laravel you have to explain that your URL will hit controller@function. What is local host 8080? Short answer: “this machine, port 8080”. Slightly longer short answer: an application, typically a web application, that is running on the user’s own computer, utilizing UDP or typically TCP port 8080.

Why does PHP artisan serve not work with guzzle?

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

Why does my guzzle application hang when I run it?

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.


1 Answers

Carl has a great solution to this. 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' => 'authorization_code',
        'client_id' => 'client-id',
        'client_secret' => 'client-secret',
        'redirect_uri' => 'http://example.com/callback',
        'code' => $request->code,
    ],
]);

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

like image 167
busytraining Avatar answered Sep 19 '22 01:09

busytraining