search
HomeWeChat AppletWeChat DevelopmentHow to query code analysis of selected articles in WeChat development

QuerySome selected ones in WeChat, click volume comparison Big article.
Don’t forget to apply for apikey (you can get it by logging in to your Baidu account). The functions to be completed are:

1. User reply" Article", the official account should return the number of the article category (for example, 9, technology).

2. If the user replies wz9, 1, Tencent, it can return articles with the keyword "Tencent" in technology articles, And display the first page (wz9, 2, Tencent can return to the second page, the number of articles returned on each page can be customized, here I put back 7 articles)

Detailed steps:

1. Reply to "article" and return the ids of all article categories.

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 = &#39;http://apis.baidu.com/showapi_open_bus/weixin/weixin_article_type&#39;;
    $header = array(&#39;apikey: 你自己的apikey&#39;);
 
    // 添加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[&#39;showapi_res_body&#39;][&#39;typeList&#39;] as $v){
     $article[] = $v[&#39;id&#39;] . "、" . $v[&#39;name&#39;];
    }
    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 category of all articles. Users can choose their favorite category to view articles based on the category id. For example, if you reply to wz19, 1, Basketball, you can view articles about basketball classified as sports. .
The specific code for calling the interface and implementing the function is as follows:

elseif($which == "wz"){
    list($art_id, $page_id, $keyname) = split(&#39;,&#39;, $keyword);
    $art_id = str_replace(&#39;wz&#39;, &#39;&#39;, $art_id);
 
    $ch = curl_init();
    $url = &#39;http://apis.baidu.com/showapi_open_bus/weixin/weixin_article_list?typeId=&#39; . $art_id . &#39;&page=&#39; . $page_id . &#39;&key=&#39; . urlencode($keyname);
 
    $header = array(&#39;apikey: 你自己的apikey&#39;);
 
    // 添加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[&#39;showapi_res_body&#39;][&#39;pagebean&#39;][&#39;contentlist&#39;] as $k=>$v){
     if($k <= 6){
      $arts[] = $v;
     }else{
      break;
     }
    }
 
    $items = "";
    foreach($arts as $v){
     $items .= "<item>
    <Title><![CDATA[" . $v[&#39;title&#39;] . "]]></Title>
    <Description><![CDATA[" . $v[&#39;title&#39;] . "]]></Description>
    <PicUrl><![CDATA[" . $v["contentImg"] . "]]></PicUrl>
    <Url><![CDATA[" . $v[&#39;url&#39;] . "]]></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 own apikey when $header = array('apikey: '); otherwise The interface will refuse to return your request

.

The above is the detailed content of How to query code analysis of selected articles in WeChat development. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor