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

标签 php curl

我正在尝试找出 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 for caches 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 use a stored response to satisfy the request without successful validation 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 only if failure to validate a request on the representation could result in incorrect operation, such as a silently unexecuted financial transaction.

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

相关文章:

php - 在 Wamp 服务器上为 Z​​end 应用程序设置 VirtualHost

php - 创建一个 Cron Job 来删除 mysql 数据库中早于今天的行

curl - "TLS wrong version number"与 OpenSSL 1.1.1

PHP REST 客户端 API 调用

php - 从 PHP 重定向流式传输的 Android MediaPlayer 不工作!

javascript - AJAX xhr.responseText 返回整个 html

http - 有什么方法可以检查 POST url 是否存在?

java - Spring OAuth2 : InsufficientAuthenticationException

macos - 自制程序:“ curl :(60)SSL证书:无法获取本地发行者证书”

php - 自定义分类法未在 Post Gutenberg 编辑器中显示