首页 新闻 会员 周边

如何将json_decode出来的api数据按需输出(json转html)

0
[待解决问题]

比如用php进行api调用,http://apistore.baidu.com/apiworks/servicedetail/888.html,

 <?php
$ch = curl_init();
    $url = 'http://apis.baidu.com/tngou/info/news?id=0&classify=0&rows=20';
    $header = array(
        'apikey: 您自己的apikey',
    );
    // 添加apikey到header
    curl_setopt($ch, CURLOPT_HTTPHEADER  , $header);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    // 执行HTTP请求
    curl_setopt($ch , CURLOPT_URL , $url);
    $res = curl_exec($ch);

    var_dump(json_decode($res));
?>

 得到的结果是

array(13) { ["count"]=> int(4336) ["description"]=> string(296) "兰州自来水苯超标污染事件发生后," ["fcount"]=> int(0) ["id"]=> int(10) ["img"]=> string(49) "/info/150729/edd3b5b20b4a513b24bb2b480347903d.jpg" ["infoclass"]=> int(6) ["keywords"]=> string(35) "诉讼 立案 居民 兰州 法院 " ["message"]=> string(8896) "兰州自来水苯超标污染事件发生后..." ["rcount"]=> int(0) ["status"]=> bool(true) ["time"]=> int(1438141088000) ["title"]=> string(57) "兰州五居民要求水厂赔偿损失、精神抚慰金" ["url"]=> string(33) "http://www.tngou.net/info/show/10" }

 

现在只想在页面输出title、keyword、description怎么操作

ytkah的主页 ytkah | 菜鸟二级 | 园豆:220
提问于:2016-06-02 16:26
< >
分享
所有回答(1)
0
1 <?php
2     include_once("db.php");
3     header("Content-type: text/html; charset=utf-8");
4     $db=new DB();
5     $sql="select title,keyword,description from table名";
6     $res=$db->get($sql);
7     $data=json_encode($res);//封装成json数据
8     echo $data;
9  ?>

你在查询时标明title,keyword,description,封装成json数据然后在前台读取

范儿君 | 园豆:228 (菜鸟二级) | 2016-09-27 14:42
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册