Check some selected articles in WeChat with relatively high number of clicks.
Don’t forget to apply for apikey (you can get it by logging in to your Baidu account). The function to be completed is:
1. When the user replies to "article", the public account should return the number of the article category (for example, 9, technology).
2. If the user replies wz9, 1, Tencent, it can return to the articles with the keyword "Tencent" in the science and technology articles, and display the first page (wz9, 2, Tencent can return to the second page, each page The number of articles returned can be customized, here I put 7 articles back).
Detailed steps:
1. Reply to "article" and return the ids of all article categories. The following code is part of the responseMsg method. If you don’t understand it or are new to WeChat development, you can refer to my article: http://www.php.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. At this time, the $resultStr of echo is the classification of all articles. Users can choose their favorite category to view articles based on the category ID. For example, by replying to wz19, 1, basketball, you can view articles about basketball classified as sports.
The specific calling interface and code to implement the function are as follows:
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(); }
Don’t forget to fill in your apikey when $header = array('apikey: ');, otherwise the interface will refuse to return your request .
The above is the entire content of this article. I hope it will be helpful to everyone’s study. I also hope that everyone will support Script House.