gpt4 book ai didi

PHP cURL - 限制执行时间

转载 作者:可可西里 更新时间:2023-11-01 00:31:22 25 4
gpt4 key购买 nike

我们正在使用网络服务,但有时它们没有响应,响应时间太长。

如果超过 1 秒,如何停止 cURL?

我试过了:

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,1);
curl_setopt($ch, CURLOPT_TIMEOUT, 1);

我还尝试在我的服务器和网络服务之间创建一个“临时页面”:我的服务器调用一个临时页面:

set_time_limit(1);
ini_set('max_execution_time', 1);

这个临时页面用 curl 调用了 web 服务本身,但仍然没有。如果我的网络服务执行时间为 10 秒,我将不得不等待 10 秒。

有什么想法吗?

最佳答案

有一个最好的解决方案。

Setting Curl's Timeout in PHP

参见文档:http://www.php.net/manual/en/function.curl-setopt.php

CURLOPT_CONNECTTIMEOUT - The number of seconds to wait while trying to connect. Use 0 to wait indefinitely.
CURLOPT_TIMEOUT - The maximum number of seconds to allow cURL functions to execute.
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,0);
curl_setopt($ch, CURLOPT_TIMEOUT, 400); //timeout in seconds

另外不要忘记增加 php 脚本自身的执行时间:

set_time_limit(0); // to infinity for example


关于PHP cURL - 限制执行时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29206651/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com