Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GuzzleHttp Hangs When Using Localhost

Here is a simple code snipplet but this just hangs and unresponsive.

    $httpClient = new GuzzleHttp\Client(); // version 6.x

    $headers = ['X-API-KEY' => '123456'];

    $request = $httpClient->request('GET', 'http://localhost:8000/BlogApiV1/BlogApi/blogs/', $headers);
    $response = $client->send($request, ['timeout' => 2]);

    echo $request->getStatusCode();
    echo $request->getHeader('content-type');
    echo $request->getBody();
    die();

Any pointers much appreciated. When I tried above with the github api using my username and password, I do get a 200 response and a lot of info.

like image 897
spreaderman Avatar asked Apr 29 '16 21:04

spreaderman


1 Answers

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:

$request = $httpClient->request('GET', 'http://localhost:8001/BlogApiV1/BlogApi/blogs/', $headers);

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

like image 95
busytraining Avatar answered Sep 19 '22 22:09

busytraining