我正在尝试找出 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): Caching和 5.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-revalidate是 Response 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/