我正在使用Guzzle打开url-s列表并获取标题.一些网址花了太长时间才响应,无法打开,我想忽略它们.在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";
?>
Run Code Online (Sandbox Code Playgroud)
这是一个示例结果.如你所见,花了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 …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用ClientBuildersymfony 6 来使用 Elasticsearch。我收到以下错误
The HTTP client Symfony\Component\HttpClient\Psr18Client is not supported for custom options
Run Code Online (Sandbox Code Playgroud)
这是我尝试过的代码。我在 laravel 中也有同样有效的代码行。
$hosts = ['https://localhost:9200'];
$client = ClientBuilder::create()
->setHosts($hosts)
->setSSLVerification(false)
->setBasicAuthentication('elastic', 'password')
->build();
Run Code Online (Sandbox Code Playgroud)