[英]cURL error 28: Operation timed out after 2000 milliseconds with 7276200 out of 23000995 bytes received
I'm using Guzzle in my Laravel project.我在我的 Laravel 项目中使用 Guzzle。 I had a memory crash when I make a request to an API that return a huge payload.
当我向返回巨大负载的 API 发出请求时,我发生了 memory 崩溃。
I have this on the top of my CURL.php
class. I have get() that I use Guzzle.我把这个放在我的
CURL.php
class 的顶部。我有 get(),我使用 Guzzle。
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Client;
use GuzzleHttp\FORCE_IP_RESOLVE;
use GuzzleHttp\DECODE_CONTENT;
use GuzzleHttp\CONNECT_TIMEOUT;
use GuzzleHttp\READ_TIMEOUT;
use GuzzleHttp\TIMEOUT;
class CURL {
public static function get($url) {
$client = new Client();
$options = [
'http_errors' => true,
'force_ip_resolve' => 'v4',
'connect_timeout' => 2,
'read_timeout' => 2,
'timeout' => 2,
];
$result = $client->request('GET',$url,$options);
$result = (string) $result->getBody();
$result = json_decode($result, true);
return $result;
}
...
}
When I call it like this in my application, it request a large payload (30000)当我在我的应用程序中这样调用它时,它会请求一个大的有效负载 (30000)
$url = 'http://site/api/account/30000';
$response = CURL::get($url)['data'];
I kept getting this error我一直收到这个错误
cURL error 28: Operation timed out after 2000 milliseconds with 7276200 out of 23000995 bytes received (see http://curl.haxx.se/libcurl/c/libcurl-errors.html )
cURL 错误 28:操作在 2000 毫秒后超时,收到 23000995 个字节中的 7276200 个(请参阅http://curl.haxx.se/libcurl/c/libcurl-errors.html )
How do I avoid this?我该如何避免这种情况?
Should I increase these settings?我应该增加这些设置吗?
'connect_timeout' => 2,
'read_timeout' => 2,
'timeout' => 2,
Yes, you need to increase read_timeout
and timeout
. 是的,您需要增加
read_timeout
和timeout
。 The error is clear, you don't have enough time to get the response (the server is slow, network or something else, doesn't matter). 错误很明显,您没有足够的时间来获取响应(服务器很慢,网络或其他原因都没有关系)。
If it's possible, increasing the timeouts is the easiest way. 如果可能的话,增加超时是最简单的方法。
If the server supports pagination, it's a better way to request the data part by part. 如果服务器支持分页,则这是一种更好地部分请求数据的更好方法。
Also you can use async queries in Guzzle and send something to your end user while you are waiting for the response from the API. 您还可以在Guzzle中使用异步查询,并在等待来自API的响应时向最终用户发送一些内容。
simply add this in your composer, worked for me !只需将其添加到您的作曲家中,对我有用!
"require": {
"ext-curl": "*"
},
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.