Guzzle を使用して URL のリストを開き、ヘッダーを取得しています。一部の URL は応答に時間がかかりすぎて開くことができず、それらを無視したいと考えています。Guzzle が例外をスローするまでに最大 20 秒以上かかります。これを変更して、接続時間を 2 秒に制限したいと考えています。私はこのコードを持っていますが、まだ時間がかかります:
<?php
include 'vendor/autoload.php';
$start = new \DateTime("now");
$start = $start->format("d.m.Y H:i:s");
echo $start."\n";
$client = new Guzzle\Http\Client();
Guzzle\Http\StaticClient::mount();
try {
$request = $client->get('http://takestoolongexample', [], ['connect_timeout' => 2, 'timeout' => 3, 'debug' => true]);
$response = $request->send();
var_dump($response->getStatusCode());
} catch (Exception $e) {
echo "\n".$e->getMessage()."\n";
}
$end = new \DateTime("now");
$end = $end->format("d.m.Y H:i:s");
echo "\n".$end."\n";
?>
これが結果の例です。ご覧のとおり、13 秒かかりました。
$ php test.php
30.12.2013 22:00:07
* getaddrinfo(3) failed for takestoolongexample:80
* Couldn't resolve host 'takestoolongexample'
* Closing connection 0
[curl] 6: Couldn't resolve host 'http://takestoolongexample' http://takestoolongexample
30.12.2013 22:00:20
(http://takestoolongexample
実際の URL でしたが、ここで変更しました)