Home Backend Development PHP Tutorial PHP WeChat development to obtain WeChat selected articles

PHP WeChat development to obtain WeChat selected articles

Dec 29, 2017 pm 06:38 PM
php article Featured

This article mainly introduces in detail the simple implementation of PHP WeChat development to query the relevant information of WeChat selected articles. Interested friends can refer to it. I hope to be helpful.

Query some selected articles in WeChat with relatively large 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. The user replies to "article", and the official account must return the number of the article category (For example, 9. Technology).

2. If the user replies wz9, 1, Tencent, the article with the keyword "Tencent" in the science and technology articles can be returned, and the first page will be displayed (wz9, 2, Tencent can return to the second page, The number of articles returned on each page can be customized, here I return 7 articles).

Detailed steps:

1. Reply to "article" and return the ids of all article categories. The following code is part of the responseMsg method

  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>";
Copy after login

$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();


   }
Copy after login

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 function implementation code 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();
   }
Copy after login

Don’t forget to fill in $header = array('apikey: '); Your own apikey, otherwise the interface will refuse to return your request.

Related recommendations:

Summary of common error messages in WeChat public account development and configuration

WeChat public account implements user management function

TP access to WeChat public account payment details

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

Statement of this Website
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 Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles