【问题标题】:Guzzle is very slow to access local webpageGuzzle 访问本地网页很慢
【发布时间】:2017-02-05 20:51:20
【问题描述】:

我刚刚开始使用Guzzle 使用PHPUnit 对 API 进行验收测试。 API 驻留在我的本地机器上,但Guzzle 的响应时间仍然是~5 秒!当我使用浏览器时,它会“立即”加载网站,让我相信这是GuzzlecURL 的问题。

这是我的测试单元:

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);
    }
}

什么可能导致响应时间过长?

【问题讨论】:

    标签: curl phpunit localhost guzzle


    【解决方案1】:

    似乎问题与使用.local 作为我的本地测试 URL 的顶级域有关。显然,.local 被 Mac OSx 上的 Bonjour 服务使用,不知何故会干扰 cURL(但奇怪的是不会干扰浏览器)。有一个类似的question on the Superuser StackExchange

    通过编辑我的hosts 文件和虚拟主机配置以使用.dev 而不是.local 解决了这个问题:

    protected function setUp() {
        $this->http = new GuzzleHttp\Client(['base_uri' => 'http://test-site.dev']);
    }
    

    【讨论】:

      猜你喜欢
      • 2013-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多