每60分钟解码并缓存一次json
我如何在 php 网页上执行此操作?
我想获取并解码 json 字符串并将结果显示为我的页面上的 html,但是,我不希望它热链接回源。
如果我可以将解码后的字符串写入服务器上的txt文件(例如weather.txt)并保留html格式并执行此操作,以便页面不会获取json脚本,直到自上次获取以来经过60分钟为止60 分钟内打开页面以及查看weather.txt 的次数。
我能想出的只是一个简单的热链接脚本,我尝试过的其他一切都失败了。
$file = file_get_contents('http://sample.com/weather');
$out = (json_decode($file));
回声 $out->mainText;
将不胜感激任何帮助。
How do I do this on a php webpage?
I want to get and decode a json string and display the results as html on my page, however, I don't want it hotlinking back to the source.
If I could write the decoded string to a txt file say weather.txt on the server and keep the html formatting and do it so that the page won't fetch the json script until 60 minutes has passed since the last time it was fetched regardless of how many times the page is opened during that 60 minute period and the weather.txt is viewed.
All I can come up with is a simple script that hotlinks, everything else I have tried simply failed.
$file = file_get_contents('http://sample.com/weather');
$out = (json_decode($file));
echo $out->mainText;
Will appreciate any help with this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用 filemtime() 每分钟重新制作一次weather.txt,如果没有,则发送现有文件。
You could use filemtime() to re-make weather.txt every minutes and if not, send the existing file.
您可以使用 UNIX cronjob,它每小时用代码输出的内容覆盖weather.txt的内容
You can use an UNIX cronjob which overwrites the content of weather.txt every hour with what your code outputs
您还可以将数据存储在 2 列表中,1 列是数据,1 列是 dts。
SO....
将 SQL 语句替换为您选择的 RDBMS
You could also store the data in a 2 column table, 1 is data, one is dts.
SO....
replace the SQL statements with your RDBMS of choice
尝试我的答案 Twitter api 每小时仅允许 150 次 json 调用,并且 Twitter 会删除不需要的 cookie。我该如何防止这种情况?
有一个很好的缓存模块。设置为 5 分钟,但很容易修改为 60 分钟。
Try my answer at Twitter api only allows 150 json calls per hour and twitter drops unwanted cookies. How do I prevent this?
There is a nice caching module. It's set for 5 minutes, but it's easy to modify for 60 minutes.