查询微信里的一些精选的,点击量比较大的文章。
别忘记申请apikey(登录百度账号即可获取),要完成的功能是:
1、用户回复"文章",公众号要返回文章分类的编号(比如9、科技)。
2、用户回复wz9,1,腾讯 则能返回科技类文章中,关键词为“腾讯”的文章,并且显示第一页(wz9,2,腾讯则可以返回第二页,每一页返回的文章数量可以自定义,此处我放回7篇)。
详细步骤:
1、回复“文章”,返回所有文章分类的id。下面的代码是responsemsg方法里的一部分,觉得看得不明白的或者第一次接触微信开发的,可以参考我的文章:http://www.jb51.net/article/87252.htm
if(!empty($poststr)){
//解析post来的xml为一个对象$postobj
$postobj = simplexml_load_string($poststr, 'simplexmlelement', libxml_nocdata);
$fromusername = $postobj->fromusername; //请求消息的用户
$tousername = $postobj->tousername; //"我"的公众号id
$keyword = trim($postobj->content); //用户发送的消息内容
$time = time(); //时间戳
$msgtype = 'text'; //消息类型:文本
$texttpl = "<xml>
<tousername><![cdata[%s]]></tousername>
<fromusername><![cdata[%s]]></fromusername>
<createtime>%s</createtime>
<msgtype><![cdata[%s]]></msgtype>
<content><![cdata[%s]]></content>
</xml>";
$which = mb_substr($keyword, 0, 2, 'utf-8');
elseif($which == "文章"){
$ch = curl_init();
$url = 'http://apis.baidu.com/showapi_open_bus/weixin/weixin_article_type';
$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);
$res = json_decode($res, true); //获取文章分类name和id
foreach($res['showapi_res_body']['typelist'] as $v){
$article[] = $v['id'] . "、" . $v['name'];
}
sort($article, sort_numeric);
foreach($article as $v){
$contentstr .= $v . "n";
}
$resultstr = sprintf($texttpl, $fromusername, $tousername, $time, $msgtype, $contentstr);
echo $resultstr;
exit();
}
2、此时echo的$resultstr就是所有文章的分类了。用户根据分类的id,可以选择自己喜欢的分类查看文章,比如回复wz19,1,篮球可以查看分类为体育的关于篮球的文章。
具体的调用接口和实现功能的代码如下:
elseif($which == "wz"){
list($art_id, $page_id, $keyname) = split(',', $keyword);
$art_id = str_replace('wz', '', $art_id);
$ch = curl_init();
$url = 'http://apis.baidu.com/showapi_open_bus/weixin/weixin_article_list?typeid=' . $art_id . '&page=' . $page_id . '&key=' . urlencode($keyname);
$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);
$res = json_decode($res, true);
foreach($res['showapi_res_body']['pagebean']['contentlist'] as $k=>$v){
if($k <= 6){
$arts[] = $v;
}else{
break;
}
}
$items = "";
foreach($arts as $v){
$items .= "<item>
<title><![cdata[" . $v['title'] . "]]></title>
<description><![cdata[" . $v['title'] . "]]></description>
<picurl><![cdata[" . $v["contentimg"] . "]]></picurl>
<url><![cdata[" . $v['url'] . "]]></url>
</item>";
}
$texttpl = "<xml>
<tousername><![cdata[%s]]></tousername>
<fromusername><![cdata[%s]]></fromusername>
<createtime>%s</createtime>
<msgtype><![cdata[news]]></msgtype>
<articlecount>7</articlecount>
<articles>" . $items . "
</articles>
</xml> ";
$resultstr = sprintf($texttpl, $fromusername, $tousername, $time);
echo $resultstr;
exit();
}
别忘了$header = array('apikey: ');的时候填写自己的apikey,否则接口会拒绝返回你的请求。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持硕编程。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!