gpt4 book ai didi

php - 有没有办法告诉 curl 不使用缓存

转载 作者:IT王子 更新时间:2023-10-29 01:23:57 29 4
gpt4 key购买 nike

我正在尝试找出 url 的文件大小:

$url1 = 'www.google.com';
$curl1 = curl_init();
curl_setopt($curl1, CURLOPT_URL, $url1);
curl_setopt($curl1, CURLOPT_RETURNTRANSFER, TRUE);
curl_exec($curl1);
$file_size = curl_getinfo($curl1, CURLINFO_SIZE_DOWNLOAD );
$file_size_kb = $file_size / 1000;
echo $file_size_kb;

输出为 43331。我认为它太低了,因为我有谷歌缓存。这是真的吗?我还在其他一些计算 url 大小的网站上测试了 google,它是原来的两倍大。

最佳答案

您可以为此使用 CURLOPT_FRESH_CONNECT。来自 curl_setopt

CURLOPT_FRESH_CONNECT TRUE to force the use of a new connection instead of a cached one.

curl_setopt($curl1, CURLOPT_FRESH_CONNECT, TRUE);

根据RFC 7234 - Hypertext Transfer Protocol (HTTP/1.1): Caching5.2. Cache-Control

The "Cache-Control" header field is used to specify directives forcaches along the request/response chain.

5.2.1. Request Cache-Control Directives定义了几个指令来控制响应缓存的使用。其中之一是

5.2.1.4. no-cache

The "no-cache" request directive indicates that a cache MUST NOT usea stored response to satisfy the request without successfulvalidation on the origin server.

所以设置一个合适的标题

curl_setopt($curl1, CURLOPT_HTTPHEADER, array("Cache-Control: no-cache"));

应确保返回有效且最新的响应。我了解,如果服务器上的验证允许这样做,这仍可能导致缓存响应。


但是,5.2.2.1. must-revalidateResponse Cache-Control Directive由服务器与对请求的响应一起给出

[...]The must-revalidate directive ought to be used by servers if and onlyif failure to validate a request on the representation could resultin incorrect operation, such as a silently unexecuted financialtransaction.

关于php - 有没有办法告诉 curl 不使用缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15493769/

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