gpt4 book ai didi

php - 简单的 PHP/cURL 缓存

转载 作者:行者123 更新时间:2023-12-01 09:35:21 52 4
gpt4 key购买 nike

我正在通过 cURL 获取远程页面的内容,我想缓存该页面,以便下次该页面不请求 cURL,并从缓存中加载。

缓存应该在一周内过期。

最佳答案

一些伪代码。

make a temp folder 


before each request check if page exists(with name==hash of URL) in the temp folder,
if not,
fetch the page,
hash the URL,
save page in temp with the Hashed url file name.
else if exists
get the temp file content
return contents

更新:代码::

$curl_defaults = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => 0,
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 5.1; rv:2.0b11) Gecko/20100101 Firefox/4.0b11',
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_AUTOREFERER => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_CONNECTTIMEOUT => 5,
CURLOPT_TIMEOUT => 20,
CURLOPT_VERBOSE => 0,
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_SSL_VERIFYPEER => 0
);

$curl_headers = array();
$curl_headers[] = 'Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg';
$curl_headers[] = 'Connection: Keep-Alive';
$curl_headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8';

function get($url, $data = "") {

if (strlen($url) < 7) return;
//echo "\n<br> Sending GET :: $url\n<br>";
if (is_array($data)) $data = implode("&", $data);
if (strpos($url, "?") > 5) $url .= "&$data";
else
$url .= "?$data";

global $curl_defaults;

$rep = array(":", "/", ".", "?", "&", "+", "=");
$fn = dirname(__FILE__) . "/cache/" . md5(str_replace($rep, "", $url)) . ".txt";

if (file_exists($fn) && @filesize($fn)>1){ //Add a file time check Here
//echo "\n<br>From Cache FileSize :: " . filesize($fn);
return file_get_contents($fn);
}


$ch = curl_init();
curl_setopt_array($ch, $curl_defaults);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$html = curl_exec($ch);

@unlink($fn);
if(strlen($html) > 100)
write2file($fn, $html);

return $html;
}

您可能需要进行一些更改。

缓存目录:./cache/

关于php - 简单的 PHP/cURL 缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9113351/

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