我的 php 项目正在使用 reddit JSON api 来获取当前页面提交的标题。
现在,每次加载页面时我都会运行一些代码,但我遇到了一些问题,即使没有真正的 API 限制。
我想以某种方式在本地存储提交的标题。你能推荐最好的方法吗?该站点在 appfog 上运行。你会推荐什么?
这是我当前的代码:
<?php
/* settings */
$url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$reddit_url = 'http://www.reddit.com/api/info.{format}?url='.$url;
$format = 'json'; //use XML if you'd like...JSON FTW!
$title = '';
/* action */
$content = get_url(str_replace('{format}',$format,$reddit_url)); //again, can be xml or json
if($content) {
if($format == 'json') {
$json = json_decode($content,true);
foreach($json['data']['children'] as $child) { // we want all children for this example
$title= $child['data']['title'];
}
}
}
/* output */
/* utility function: go get it! */
function get_url($url) {
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,1);
$content = curl_exec($ch);
curl_close($ch);
return $content;
}
?>
谢谢!
最佳答案
介绍
这是你的代码的修改版本
$url = "http://stackoverflow.com/";
$loader = new Loader();
$loader->parse($url);
printf("<h4>New List : %d</h4>", count($loader));
printf("<ul>");
foreach ( $loader as $content ) {
printf("<li>%s</li>", $content['title']);
}
printf("</ul>");
输出
新列表:7
- 来自 Joel Spolsky 和 Jeff Atwood 的新播客。
- 示例代码/Pyhton 的好网站
- stackoverflow.com 显然拥有互联网历史上最好的 Web 代码,reddit 最好开始复制它。
- 一个类似于 reddit、使用 OpenID 的程序员网站
- 很棒的开发者网站。让熟悉的人回答您的问题。
- Stack Overflow 已公开发布
- Stack Overflow,一个编程问答网站。 & Reddit 可以从他们的界面中学到很多东西!
问题
我在这里看到了一些你想实现的事情
- 我想以某种方式在本地存储提交的标题
- 现在我正在每次加载页面时运行一些代码
据我了解,您需要的是一个简单的数据缓存副本,这样您就不必一直加载 url。
简单的解决方案
您可以使用的简单缓存系统是 memcache ..
示例 A
$url = "http://stackoverflow.com/";
// Start cache
$m = new Memcache();
$m->addserver("localhost");
$cache = $m->get(sha1($url));
if ($cache) {
// Use cache copy
$loader = $cache;
printf("<h2>Cache List: %d</h2>", count($loader));
} else {
// Start a new Loader
$loader = new Loader();
$loader->parse($url);
printf("<h2>New List : %d</h2>", count($loader));
$m->set(sha1($url), $loader);
}
// Oupput all listing
printf("<ul>");
foreach ( $loader as $content ) {
printf("<li>%s</li>", $content['title']);
}
printf("</ul>");
示例 B
您可以使用 Last Modification Date
作为缓存键,这样只有当文档被修改时您才会保存新副本
$headers = get_headers(sprintf("http://www.reddit.com/api/info.json?url=%s",$url), true);
$time = strtotime($headers['Date']); // get last modification date
$cache = $m->get($time);
if ($cache) {
$loader = $cache;
}
由于您的类实现了 JsonSerializable
,您可以对结果进行 json 编码,并将其存储在数据库中,例如 MongoDB 或 MySQL
$data = json_encode($loader);
// Save to DB
使用的类
class Loader implements IteratorAggregate, Countable, JsonSerializable {
private $request = "http://www.reddit.com/api/info.json?url=%s";
private $data = array();
private $total;
function parse($url) {
$content = json_decode($this->getContent(sprintf($this->request, $url)), true);
$this->data = array_map(function ($v) {
return $v['data'];
}, $content['data']['children']);
$this->total = count($this->data);
}
public function getIterator() {
return new ArrayIterator($this->data);
}
public function count() {
return $this->total;
}
public function getType() {
return $this->type;
}
public function jsonSerialize() {
return $this->data;
}
function getContent($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);
$content = curl_exec($ch);
curl_close($ch);
return $content;
}
}
关于php - 如何存储来自 JSON api 的 moSTLy 静态数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13773987/