Guzzle 访问本地网页速度很慢
Guzzle is very slow to access local webpage
我刚开始 Guzzle
使用 PHPUnit
对 API 进行验收测试。 API 驻留在我的本地机器上,但 Guzzle
的响应时间仍然是 ~5 秒!当我使用浏览器时,它会加载网站 "instantly",让我相信这是 Guzzle
或 cURL
.
的问题
这是我的测试单元:
class MyTest extends \PHPUnit_Framework_TestCase {
/** @var GuzzleHttp\Client */
private $http;
protected function setUp() {
$this->http = new GuzzleHttp\Client(['base_uri' => 'http://test-site.local']);
}
protected function tearDown() {
$this->http = null;
}
public function testGet() {
$response = $this->http->request('GET', 'users');
$this->assertEquals(200, $response->getStatusCode());
$contentType = $response->getHeaders()["Content-Type"][0];
$this->assertStringStartsWith("application/json", $contentType);
}
}
什么会导致响应时间过长?
问题似乎与使用 .local
as the top level domain for my local test URL. Apparently, .local
is used by Bonjour services on Mac OSx, somehow causing interference with cURL (but not with browsers oddly enough). There is a similar question on the Superuser StackExchange.
有关
通过编辑我的 hosts
文件和虚拟主机配置以使用 .dev
而不是 .local
解决了这个问题:
protected function setUp() {
$this->http = new GuzzleHttp\Client(['base_uri' => 'http://test-site.dev']);
}
我刚开始 Guzzle
使用 PHPUnit
对 API 进行验收测试。 API 驻留在我的本地机器上,但 Guzzle
的响应时间仍然是 ~5 秒!当我使用浏览器时,它会加载网站 "instantly",让我相信这是 Guzzle
或 cURL
.
这是我的测试单元:
class MyTest extends \PHPUnit_Framework_TestCase {
/** @var GuzzleHttp\Client */
private $http;
protected function setUp() {
$this->http = new GuzzleHttp\Client(['base_uri' => 'http://test-site.local']);
}
protected function tearDown() {
$this->http = null;
}
public function testGet() {
$response = $this->http->request('GET', 'users');
$this->assertEquals(200, $response->getStatusCode());
$contentType = $response->getHeaders()["Content-Type"][0];
$this->assertStringStartsWith("application/json", $contentType);
}
}
什么会导致响应时间过长?
问题似乎与使用 .local
as the top level domain for my local test URL. Apparently, .local
is used by Bonjour services on Mac OSx, somehow causing interference with cURL (but not with browsers oddly enough). There is a similar question on the Superuser StackExchange.
通过编辑我的 hosts
文件和虚拟主机配置以使用 .dev
而不是 .local
解决了这个问题:
protected function setUp() {
$this->http = new GuzzleHttp\Client(['base_uri' => 'http://test-site.dev']);
}