Home  >  Article  >  Backend Development  >  javascript - How to use Baidu’s API? Please give me the answer

javascript - How to use Baidu’s API? Please give me the answer

WBOY
WBOYOriginal
2016-10-10 11:56:01840browse

api store’s WeChat popular selection api

<code>接口地址 :http://apis.baidu.com/txapi/weixin/wxhot
请求方法 :GET
请求参数(header) :
参数名    类型    必填    参数位置    描述    默认值
apikey
string    是    header    
API密钥
您自己的apikey
请求参数(urlParam) :
参数名    类型    必填    参数位置    描述    默认值
num
number    是    urlParam    
文章返回数量
10
rand
number    否    urlParam    
是否随机,1表示随机
1
word
string    否    urlParam    
检索关键词
盗墓笔记
page
number    否    urlParam    
翻页,输出数量跟随num参数
1
src
string    否    urlParam    
指定文章来源
人民日报

请求示例 :
curl示例php示例python示例java示例C#示例ObjectC示例Swift示例

1
<?php
2
    $ch = curl_init();
3
    $url = 'http://apis.baidu.com/txapi/weixin/wxhot?num=10&rand=1&word=%E7%9B%97%E5%A2%93%E7%AC%94%E8%AE%B0&page=1&src=%E4%BA%BA%E6%B0%91%E6%97%A5%E6%8A%A5';
4
    $header = array(
5
        'apikey: 您自己的apikey',
6
    );
7
    // 添加apikey到header
8
    curl_setopt($ch, CURLOPT_HTTPHEADER  , $header);
9
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
10
    // 执行HTTP请求
11
    curl_setopt($ch , CURLOPT_URL , $url);
12
    $res = curl_exec($ch);
13
​
14
    var_dump(json_decode($res));
15
?></code>
<code>JSON返回示例 :
{
    "code": 200,   //返回状态码
    "msg": "success",  //返回状态消息
    "newslist": [
        {
            "ctime": "2016-03-31",  //文章发布时间
            "title": "奇虎360宣布通过私有化决议",  //文字标题
            "description": "互联网头条",  //来源及描述
            "picUrl": "http://t1.qpic.cn/mblogpic/f01a972dbcc1060fd456/2000", //文章插图,若空为默认
            "url": "http://mp.weixin.qq.com/s?__biz=MjM5OTMyODA2MA==&idx=1&mid=402594468&sn=5cd644536b472a283cc1d3f5124a0cab"
        },  //文章URL
        {
            "ctime": "2016-03-31",
            "title": "小本生意做什么挣钱十七大小本生意推荐",
            "description": "创业最前线",
            "picUrl": "http://zxpic.gtimg.com/infonew/0/wechat_pics_-4225297.jpg/640",
            "url": "http://mp.weixin.qq.com/s?__biz=MzA3NjgzNDUwMQ==&idx=2&mid=401864059&sn=cfa082e38ba38c7e673b1ce0a075faee"
        },
        {
            "ctime": "2016-03-31",
            "title": "当穷游从线上社区向线下旅游服务提供者转身,创业不如就忘了“大平台”这件事吧",
            "description": "中国旅讯",
            "picUrl": "http://zxpic.gtimg.com/infonew/0/wechat_pics_-4218551.jpg/640",
            "url": "http://mp.weixin.qq.com/s?__biz=MzA5MzQyMzQyMw==&idx=3&mid=402488438&sn=88f2d7e415e6d18dafccaa8da549edb6"
        },
        {
            "ctime": "2016-03-31",
            "title": "等我们老了,就去经营这样一家小店",
            "description": "慢时间",
            "picUrl": "http://zxpic.gtimg.com/infonew/0/wechat_pics_-4225311.jpg/640",
            "url": "http://mp.weixin.qq.com/s?__biz=MjM5NTIxNTU0MA==&idx=1&mid=405728106&sn=b8ac94b83aa92eea54acf75eff89c657"
        },
        {
            "ctime": "2016-03-31",
            "title": "曝光失业女子利用微信月均收入过万,原因居然是这个?",
            "description": "那些路过心上的句子",
            "picUrl": "http://zxpic.gtimg.com/infonew/0/wechat_pics_-4116605.jpg/640",
            "url": "http://mp.weixin.qq.com/s?__biz=MjM5MTg3OTAzNg==&idx=1&mid=402804635&sn=172aa8f7bf0f8078ade644f46722c561"
        },
        {
            "ctime": "2016-03-31",
            "title": "什麼人生活在一個單向的世界?",
            "description": "今天文学",
            "picUrl": "http://zxpic.gtimg.com/infonew/0/wechat_pics_-4082011.jpg/640",
            "url": "http://mp.weixin.qq.com/s?__biz=MjM5ODQxODMyMQ==&idx=1&mid=420440897&sn=cb4b009440b6a9d954bff2a622344e8d"
        },
        {
            "ctime": "2016-03-31",
            "title": "这些事,足够让我们相信爱情",
            "description": "女生心理学",
            "picUrl": "http://zxpic.gtimg.com/infonew/0/wechat_pics_-4225315.jpg/640",
            "url": "http://mp.weixin.qq.com/s?__biz=MzA3NjM5NDIyMw==&idx=1&mid=401870610&sn=1baf92ad1bb1b9f575027473754551f9"
        },
        {
            "ctime": "2016-03-31",
            "title": "少女们,为什么你们总爱翻白眼?",
            "description": "青年文摘",
            "picUrl": "http://zxpic.gtimg.com/infonew/0/wechat_pics_-4225320.jpg/640",
            "url": "http://mp.weixin.qq.com/s?__biz=MzA5ODEzMTIxOA==&idx=4&mid=405169046&sn=8ea51ec94fcd5d9bed6d1df0d0bf889f"
        },
        {
            "ctime": "2016-03-31",
            "title": "总说韩国艺人来中国捞金,先看完这些再说吧",
            "description": "RunningMan",
            "picUrl": "http://zxpic.gtimg.com/infonew/0/wechat_pics_-4225344.jpg/640",
            "url": "http://mp.weixin.qq.com/s?__biz=MzA3NDcwNzUxMw==&idx=4&mid=403101020&sn=5a9fa497df693b768637faf85f2b6449"
        },
        {
            "ctime": "2016-03-31",
            "title": "宋仲基宋慧乔频传绯闻宋仲基蛮享受的",
            "description": "韩国新网",
            "picUrl": "http://zxpic.gtimg.com/infonew/0/wechat_pics_-4225352.jpg/640",
            "url": "http://mp.weixin.qq.com/s?__biz=MjM5MDAzNjYwMA==&idx=4&mid=403617618&sn=3446bb057607d2daf2e3123f837fa24d"
        }
    ]
}</code>

How can I put the returned content and images on the web page? Please give me some answers, I'm a newbie

Reply content:

api store’s WeChat popular selection api

<code>接口地址 :http://apis.baidu.com/txapi/weixin/wxhot
请求方法 :GET
请求参数(header) :
参数名    类型    必填    参数位置    描述    默认值
apikey
string    是    header    
API密钥
您自己的apikey
请求参数(urlParam) :
参数名    类型    必填    参数位置    描述    默认值
num
number    是    urlParam    
文章返回数量
10
rand
number    否    urlParam    
是否随机,1表示随机
1
word
string    否    urlParam    
检索关键词
盗墓笔记
page
number    否    urlParam    
翻页,输出数量跟随num参数
1
src
string    否    urlParam    
指定文章来源
人民日报

请求示例 :
curl示例php示例python示例java示例C#示例ObjectC示例Swift示例

1
<?php
2
    $ch = curl_init();
3
    $url = 'http://apis.baidu.com/txapi/weixin/wxhot?num=10&rand=1&word=%E7%9B%97%E5%A2%93%E7%AC%94%E8%AE%B0&page=1&src=%E4%BA%BA%E6%B0%91%E6%97%A5%E6%8A%A5';
4
    $header = array(
5
        'apikey: 您自己的apikey',
6
    );
7
    // 添加apikey到header
8
    curl_setopt($ch, CURLOPT_HTTPHEADER  , $header);
9
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
10
    // 执行HTTP请求
11
    curl_setopt($ch , CURLOPT_URL , $url);
12
    $res = curl_exec($ch);
13
​
14
    var_dump(json_decode($res));
15
?></code>
<code>JSON返回示例 :
{
    "code": 200,   //返回状态码
    "msg": "success",  //返回状态消息
    "newslist": [
        {
            "ctime": "2016-03-31",  //文章发布时间
            "title": "奇虎360宣布通过私有化决议",  //文字标题
            "description": "互联网头条",  //来源及描述
            "picUrl": "http://t1.qpic.cn/mblogpic/f01a972dbcc1060fd456/2000", //文章插图,若空为默认
            "url": "http://mp.weixin.qq.com/s?__biz=MjM5OTMyODA2MA==&idx=1&mid=402594468&sn=5cd644536b472a283cc1d3f5124a0cab"
        },  //文章URL
        {
            "ctime": "2016-03-31",
            "title": "小本生意做什么挣钱十七大小本生意推荐",
            "description": "创业最前线",
            "picUrl": "http://zxpic.gtimg.com/infonew/0/wechat_pics_-4225297.jpg/640",
            "url": "http://mp.weixin.qq.com/s?__biz=MzA3NjgzNDUwMQ==&idx=2&mid=401864059&sn=cfa082e38ba38c7e673b1ce0a075faee"
        },
        {
            "ctime": "2016-03-31",
            "title": "当穷游从线上社区向线下旅游服务提供者转身,创业不如就忘了“大平台”这件事吧",
            "description": "中国旅讯",
            "picUrl": "http://zxpic.gtimg.com/infonew/0/wechat_pics_-4218551.jpg/640",
            "url": "http://mp.weixin.qq.com/s?__biz=MzA5MzQyMzQyMw==&idx=3&mid=402488438&sn=88f2d7e415e6d18dafccaa8da549edb6"
        },
        {
            "ctime": "2016-03-31",
            "title": "等我们老了,就去经营这样一家小店",
            "description": "慢时间",
            "picUrl": "http://zxpic.gtimg.com/infonew/0/wechat_pics_-4225311.jpg/640",
            "url": "http://mp.weixin.qq.com/s?__biz=MjM5NTIxNTU0MA==&idx=1&mid=405728106&sn=b8ac94b83aa92eea54acf75eff89c657"
        },
        {
            "ctime": "2016-03-31",
            "title": "曝光失业女子利用微信月均收入过万,原因居然是这个?",
            "description": "那些路过心上的句子",
            "picUrl": "http://zxpic.gtimg.com/infonew/0/wechat_pics_-4116605.jpg/640",
            "url": "http://mp.weixin.qq.com/s?__biz=MjM5MTg3OTAzNg==&idx=1&mid=402804635&sn=172aa8f7bf0f8078ade644f46722c561"
        },
        {
            "ctime": "2016-03-31",
            "title": "什麼人生活在一個單向的世界?",
            "description": "今天文学",
            "picUrl": "http://zxpic.gtimg.com/infonew/0/wechat_pics_-4082011.jpg/640",
            "url": "http://mp.weixin.qq.com/s?__biz=MjM5ODQxODMyMQ==&idx=1&mid=420440897&sn=cb4b009440b6a9d954bff2a622344e8d"
        },
        {
            "ctime": "2016-03-31",
            "title": "这些事,足够让我们相信爱情",
            "description": "女生心理学",
            "picUrl": "http://zxpic.gtimg.com/infonew/0/wechat_pics_-4225315.jpg/640",
            "url": "http://mp.weixin.qq.com/s?__biz=MzA3NjM5NDIyMw==&idx=1&mid=401870610&sn=1baf92ad1bb1b9f575027473754551f9"
        },
        {
            "ctime": "2016-03-31",
            "title": "少女们,为什么你们总爱翻白眼?",
            "description": "青年文摘",
            "picUrl": "http://zxpic.gtimg.com/infonew/0/wechat_pics_-4225320.jpg/640",
            "url": "http://mp.weixin.qq.com/s?__biz=MzA5ODEzMTIxOA==&idx=4&mid=405169046&sn=8ea51ec94fcd5d9bed6d1df0d0bf889f"
        },
        {
            "ctime": "2016-03-31",
            "title": "总说韩国艺人来中国捞金,先看完这些再说吧",
            "description": "RunningMan",
            "picUrl": "http://zxpic.gtimg.com/infonew/0/wechat_pics_-4225344.jpg/640",
            "url": "http://mp.weixin.qq.com/s?__biz=MzA3NDcwNzUxMw==&idx=4&mid=403101020&sn=5a9fa497df693b768637faf85f2b6449"
        },
        {
            "ctime": "2016-03-31",
            "title": "宋仲基宋慧乔频传绯闻宋仲基蛮享受的",
            "description": "韩国新网",
            "picUrl": "http://zxpic.gtimg.com/infonew/0/wechat_pics_-4225352.jpg/640",
            "url": "http://mp.weixin.qq.com/s?__biz=MjM5MDAzNjYwMA==&idx=4&mid=403617618&sn=3446bb057607d2daf2e3123f837fa24d"
        }
    ]
}</code>

How can I put the returned content and images on the web page? Please give me some answers, I'm a newbie

Just put it directly into the corresponding tag....

What you need to know:

  1. How to send requests in the backend language you know

  2. What is json and how to parse json using your backend language or JavaScript

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn