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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15493769/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:21:04  来源:igfitidea点击:

Is there a way to tell curl to not use cache

phpcurl

提问by Edgar

I am trying to find out file size of an url:

我试图找出一个 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;

The output is 43331. I think its too low because i have google cached. Can this be true? Also i tested google on some other site that calculates size of url, and it was twice as big.

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

回答by Olaf Dietsche

You can use CURLOPT_FRESH_CONNECTfor this. From curl_setopt

您可以CURLOPT_FRESH_CONNECT为此使用。从curl_setopt

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

CURLOPT_FRESH_CONNECTTRUE 强制使用新连接而不是缓存连接。

curl_setopt($curl1, CURLOPT_FRESH_CONNECT, TRUE);

回答by Subodh Ghulaxe

curl_setopt($curl1, CURLOPT_FRESH_CONNECT, 1); // don't use a cached version of the url

curl_setopt($curl1, CURLOPT_FRESH_CONNECT, 1); // 不要使用缓存版本的 url

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

check example here

在此处检查示例

you can set header

你可以设置标题

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

this link may be helpful to you http://www.php.net/manual/en/function.curl-setopt.php#96903

此链接可能对您有所帮助http://www.php.net/manual/en/function.curl-setopt.php#96903

回答by Mike Walkowiak

The best way to avoid caching is applying the time or any other random element to the url, like this:
$url .= '?ts=' . time();

避免缓存的最佳方法是将时间或任何其他随机元素应用于 url,如下所示:
$url .= '?ts=' . time();

so for example instead of having
http://example.com/content.php
you would have
http://example.com/content.php?ts=1212434353

所以例如,而不是让
http://example.com/content.php
你有
http://example.com/content.php?ts=1212434353

回答by Dead Man

You can tell CURLto use fresh data by setting CURLOPT_FRESH_CONNECTto TRUE

您可以CURL通过设置CURLOPT_FRESH_CONNECT为使用新数据TRUE

You can read more about CURLfunction here :

您可以CURL在此处阅读有关功能的更多信息:

http://php.net/manual/en/function.curl-setopt.php

http://php.net/manual/en/function.curl-setopt.php

回答by Tony Stark

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

使用CURLOPT_FRESH_CONNECT- TRUE 强制使用新连接而不是缓存连接。

Example:

例子:

<?php
    function check_url($url) {
        $c = curl_init();
        curl_setopt($c, CURLOPT_URL, $url);
        curl_setopt($c, CURLOPT_HEADER, 1); // get the header
        curl_setopt($c, CURLOPT_NOBODY, 1); // and *only* get the header
        curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); // get the response as a string from curl_exec(), rather than echoing it
        curl_setopt($c, CURLOPT_FRESH_CONNECT, 1); // don't use a cached version of the url
        if (!curl_exec($c)) { return false; }

        $httpcode = curl_getinfo($c, CURLINFO_HTTP_CODE);
        return ($httpcode < 400);
    }
?>

for more details about curl check out http://php.net/manual/en/function.curl-setopt.php

有关 curl 的更多详细信息,请查看http://php.net/manual/en/function.curl-setopt.php

may this help you.

可能这对你有帮助。