kriswallsmith/Buzz について色々

PHPのHTTPクライアントライブラリ kriswallsmith/Buzz について色々調べてみます。

検証コード

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Stopwatch\Stopwatch;

class TestBuzzBrowserCommand extends ContainerAwareCommand
{
    protected function configure()
    {
        $this->setName('sample:test-buzz-browser');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $stopwatch = new Stopwatch();
        $stopwatch->start('curl');

        $browser = $this->getContainer()->get('buzz.browser');

        echo 'Client.Timeout = ' . $browser->getClient()->getTimeout() . PHP_EOL;
        $browser->getClient()->setTimeout(10);
        echo 'Client.Timeout = ' . $browser->getClient()->getTimeout() . PHP_EOL;

        try {

            $response = $browser->get('http://localhost/...');
            print "content is ...\n" . $response->getContent();

        } catch (\Exception $e) {

            $event = $stopwatch->stop('curl');

            echo 'caught ' . get_class($e) . PHP_EOL;

            echo <<<EOT

Duration = {$event->getDuration()} msec
----------------------------------------
Code    : {$e->getCode()}
Message : {$e->getMessage()}
EOT;
        }
    }
}

実行結果・その1

$ php app/console sample:test-buzz-browser
Client.Timeout = 5
Client.Timeout = 10
caught Buzz\Exception\ClientException

Duration = 10051 msec
----------------------------------------
Code    : 28
Message : Operation timed out after 10016 milliseconds with 0 out of -1 bytes received

実行結果・その2

  • 接続エラーは、Code=7(CURLE_COULDNT_CONNECT)
  • メッセージ'No error'はバグかしら?
$ php app/console sample:test-buzz-browser
Client.Timeout = 5
Client.Timeout = 10
caught Buzz\Exception\ClientException

Duration = 3032 msec
----------------------------------------
Code    : 7
Message : Failed connect to localhost:12345; No error

実行結果・その3

名前解決できない場合は、Code=6(CURLE_COULDNT_RESOLVE_HOST)

$ php app/console sample:test-buzz-browser
Client.Timeout = 5
Client.Timeout = 10
caught Buzz\Exception\ClientException

Duration = 2285 msec
----------------------------------------
Code    : 6
Message : Could not resolve host: XXXXXXXXXX