我使用gae php作为使用“file_get_contents”的简单代理时遇到了一些麻烦
当我第一次加载文件时,我获得了最新版本。 但是,如果我更改文件的内容,我不会立即获得最新版本。
$result = file_get_contents('http://example.com/'.$url);
我发现的临时解决方案是在查询字符串的末尾添加一个随机变量,这样我每次都可以得到一个新版本的文件:
$result = file_get_contents('http://example.com/'.$url.'?r=' . rand(0, 9999));
但是这个技巧对于带参数的api调用不起作用。
我尝试在gae的php.ini中禁用APC缓存(使用apc.enabled =“0”),我在我的脚本中使用了clearstatcache();
,但都没有工作。
有什么想法吗?
感谢。
答案 0 :(得分:3)
如appengine documentation中所述,http流包装器使用urlfetch
。由于seen in another question urlfetch
提供公共/共享缓存,因此不允许单个应用清除它。对于您自己的服务,您可以根据需要设置HTTP缓存标头以减少或取消缓存。
此外,您还可以添加HTTP请求标头,指示允许返回的数据的最长期限。邮件列表线程中给出的python示例是:
result = urlfetch.fetch(url, headers = {'Cache-Control' : 'max-age=300'})
每php.net file_get_contents http header example和HTTP header documentation修改后的示例如下:
<?php
$opts = [
'http' => [
'method' => 'GET',
'header' => "Cache-Control: max-age=60\r\n",
],
];
$context = stream_context_create($opts);
$file = file_get_contents('http://www.example.com/', false, $context);
?>