使用Localhost时GuzzleHttp挂起 [英] GuzzleHttp Hangs When Using Localhost
本文介绍了使用Localhost时GuzzleHttp挂起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是一个简单的代码片段,但这只是挂起并且没有响应。
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();
任何指针都值得赞赏。当我使用用户名和密码在github api上尝试上述操作时,确实收到200条响应和很多信息。
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.
推荐答案
我遇到了同样的问题。我通过定义 base_uri
来解决此问题。
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');
这篇关于使用Localhost时GuzzleHttp挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文