希望写一段php代码可以快速读取json接口的数据其中一个api地址
http://api.crossref.org/journals/2314-6133/works?rows=0
数据如下:
{"status":"ok","message-type":"work-list","message-version":"1.0.0","message":{"query":{"search-terms":null,"start-index":0},"items-per-page":0,"items":[],"total-results":10747,"facets":{}}}我只需要其中 total-results":10747 这个数据因为一个页面需要读取十多条这样的json数据不知道如何快速高效的把这个数据读出来jason接口不是本站的,需要远程读取,不知道如何快速高效的读取?
http://api.crossref.org/journals/2314-6133/works?rows=0
数据如下:
{"status":"ok","message-type":"work-list","message-version":"1.0.0","message":{"query":{"search-terms":null,"start-index":0},"items-per-page":0,"items":[],"total-results":10747,"facets":{}}}我只需要其中 total-results":10747 这个数据因为一个页面需要读取十多条这样的json数据不知道如何快速高效的把这个数据读出来jason接口不是本站的,需要远程读取,不知道如何快速高效的读取?
解决方案 »
- 初用smarty显示文章标题列表,遇到一个问题。
- 共享一款软件,discuz!论坛助手 免费的 快速提高论坛人气、实时有效监听论坛内容、批量马甲顶贴的工具,是论坛运营过程中不可或缺的利器。
- php服务器上传大文件
- 请教mysql中的字符串查询问题?
- 急求:php扩展中在对zend_compile_string做HOOK在windows下无效?
- 在不同的HTML源码中提取特定的内容
- 关于会员身份验证,是不是只有 session 和cookies 这两种,这两种方法在什么场核,什么情况下该用谁?
- 怎样得到今日零点的时间戳?
- 希望在本地用php调用存放在web上的access数据库里的数据,该如何连接?
- Zend studio12.0.1是不是不能提示javascript代码啊?
- php注册、登陆
- http://www.jinrishoufa.com/ 这个网站wordpress做的吗
$s = file_get_contents($url);
$a = json_decode($s, 1);
echo $a['message']['total-results'];
10747
$url = 'http://api.crossref.org/journals/2314-6133/works?rows=0';
$json = file_get_contents($url);
$data = json_decode($json, true);
echo $data['message']['total-results'];
?>
求教如何设置缓存,这个接口响应比较慢。
<?php
$url = 'http://api.crossref.org/journals/2314-6133/works?rows=0';// cache文件
$cache_file = 'mydata.cache';// cache超时时间
$cache_expire = 5;// 读取cache内容
$json = get_cache($cache_file);// cache不存在,重新读取api获取数据,并写入cache
if(!$json){
echo 'request API<br>';
$json = file_get_contents($url);
set_cache($cache_file, $json, $cache_expire);
}else{
echo 'request cache<br>';
}$data = json_decode($json, true);
echo $data['message']['total-results'];function get_cache($cache_file){
if(file_exists($cache_file)){
$cache = file_get_contents($cache_file);
$cache_data = json_decode($cache, true);
if(time()<$cache_data['expire']){
return $cache_data['data'];
}
}
return '';
}function set_cache($cache_file, $data, $expire){
$cache_data = json_encode(array(
'data' => $data,
'expire' => time()+$expire
));
file_put_contents($cache_file, $cache_data, true);
}?>