GuzzleHttp Hangs When Using Localhost

2020-08-18 08:24发布

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.

6条回答
Luminary・发光体
2楼-- · 2020-08-18 08:43

Setting connect_timeout to false worked for me on local development (Laravel), it just takes very long for the request to go through!

$client = new Client([
    'connect_timeout' => false,
    'timeout'         => 30.0, // set higher if timeout still happens
]);
查看更多
forever°为你锁心
3楼-- · 2020-08-18 08:47

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 everything on server or a local virtual host.

查看更多
迷人小祖宗
4楼-- · 2020-08-18 08:49

Finally resolved it. Guzzle (or CURL to be specific) is denying the requests if you're running from non-standard ports.

Also, this appears to be random, sometime it works, sometime it doesn't. I moved to port 80 and Voila everything worked.

查看更多
在下西门庆
5楼-- · 2020-08-18 08:49

This issue fixed in my end. Try this -> Configure your Laravel files into IIS or XAMPP server and then try to run via your local IP address like (192.168.X.X).

查看更多
爱情/是我丢掉的垃圾
6楼-- · 2020-08-18 08:52

If the script at http://localhost:8000/BlogApiV1/BlogApi/blogs works well, my bet it that the X-API-KEY is not being sent.

If you look at the docs (http://docs.guzzlephp.org/en/latest/request-options.html#headers) it seems that you malformed the options array.

It should be

 $headers = ['headers' => ['X-API-KEY' => '123456']];
查看更多
一夜七次
7楼-- · 2020-08-18 09:02

I was having the same issue. I got around it by defining base_uri as below.

$client = new \GuzzleHttp\Client([
    'base_uri' => 'http://localhost:8000',
    'defaults' => [
        'exceptions' => false
    ]
]);

$response = $client->get('/api/user/1');
查看更多
登录 后发表回答