wordpress

WordPress 调用韩寒「ONE · 一个」API

2016-1-23 1744字 30,674


如果你是一个文艺青年肯定不会错过韩大少的一个,韩大少说one is all,每天只为你准备一张图画,一篇文字和一个问答。和我之前介绍的nextday有点相似,但是提供的内容更多,如果把每天的图片和名言放在自己的网站上瞬间提升逼格啊有木有。

APIs

图文接口

url: http://211.152.49.184:7001/OneForWeb/one/getHpinfo
para: strDate = yyyy-mm-dd

内容接口

url: http://211.152.49.184:7001/OneForWeb/one/getOneContentInfo
para: strDate = yyyy-mm-dd

问题接口

url: http://211.152.49.184:7001/OneForWeb/one/getOneQuestionInfo
para: strDate = yyyy-mm-dd

调用方法

下面的代码添加到 functions.php 中,由于使用wordpress的缓存函数所以只能在

wordprfunction the_one(){
 $json = get_one_json();
 $content = $json['hpEntity'];
 $image = $content['strOriginalImgUrl'];
 $author = $content['strAuthor'];
 $words = $content['strContent'];
 $output = '<div id="one--feed">';
 $output .= '<img src="'.$image.'">';//如果不显示图片则删掉这行代码
 $output .= '<div class="oneContent">'.$words.'</div></div>';
 return $output;
}

function get_one_json(){
 if (get_transient('onecache')) {
 $content = get_transient('onecache');
 }else{
 delete_transient('onecache');
 $data = date("Y-m-d",time());
 $getjson = 'http://211.152.49.184:7001/OneForWeb/one/getHpinfo?strDate=' . $data;
 $content = file_get_contents($getjson);
 set_transient('onecache', $content, 60*60*24);
 }
 return json_decode($content,true);
} return json_decode($content,true);
}

在你想使用的地方使用 <?php echo the_one(); ?> 调用即可。

参考CSS样式

#one--feed{width:220px;font-size:12px;border-bottom:2px #ee4d50 solid}
#one--feed .oneContent{background-color:#f2f2f2;padding:5px 10px}
版权声明:Shire 发表于 2016-1-23
转载请注明: WordPress 调用韩寒「ONE · 一个」API | Shire

评论

  1. 流水     Windows 10 /    Google Chrome

    大神,没有成功,能帮我弄一下吗

    北京市北京市 回复

该文章已经关闭评论