Table of Contents
PHP WeChat development query WeChat selected articles,
Home php教程 php手册 PHP WeChat development to query WeChat selected articles,

PHP WeChat development to query WeChat selected articles,

Jul 06, 2016 pm 02:24 PM
php WeChat Inquire

PHP WeChat development query WeChat selected articles,

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. 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.bkjia.com/article/87252.htm

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

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');

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

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 code to implement the function are as follows:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

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&#63;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 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 Bangke Home.

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 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

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)

Explain the match expression (PHP 8 ) and how it differs from switch. Explain the match expression (PHP 8 ) and how it differs from switch. Apr 06, 2025 am 12:03 AM

In PHP8, match expressions are a new control structure that returns different results based on the value of the expression. 1) It is similar to a switch statement, but returns a value instead of an execution statement block. 2) The match expression is strictly compared (===), which improves security. 3) It avoids possible break omissions in switch statements and enhances the simplicity and readability of the code.

What is Cross-Site Request Forgery (CSRF) and how do you implement CSRF protection in PHP? What is Cross-Site Request Forgery (CSRF) and how do you implement CSRF protection in PHP? Apr 07, 2025 am 12:02 AM

In PHP, you can effectively prevent CSRF attacks by using unpredictable tokens. Specific methods include: 1. Generate and embed CSRF tokens in the form; 2. Verify the validity of the token when processing the request.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

The difference between H5 and mini-programs and APPs The difference between H5 and mini-programs and APPs Apr 06, 2025 am 10:42 AM

H5. The main difference between mini programs and APP is: technical architecture: H5 is based on web technology, and mini programs and APP are independent applications. Experience and functions: H5 is light and easy to use, with limited functions; mini programs are lightweight and have good interactiveness; APPs are powerful and have smooth experience. Compatibility: H5 is cross-platform compatible, applets and APPs are restricted by the platform. Development cost: H5 has low development cost, medium mini programs, and highest APP. Applicable scenarios: H5 is suitable for information display, applets are suitable for lightweight applications, and APPs are suitable for complex functions.

Explain strict types (declare(strict_types=1);) in PHP. Explain strict types (declare(strict_types=1);) in PHP. Apr 07, 2025 am 12:05 AM

Strict types in PHP are enabled by adding declare(strict_types=1); at the top of the file. 1) It forces type checking of function parameters and return values ​​to prevent implicit type conversion. 2) Using strict types can improve the reliability and predictability of the code, reduce bugs, and improve maintainability and readability.

How can you prevent a class from being extended or a method from being overridden in PHP? (final keyword) How can you prevent a class from being extended or a method from being overridden in PHP? (final keyword) Apr 08, 2025 am 12:03 AM

In PHP, the final keyword is used to prevent classes from being inherited and methods being overwritten. 1) When marking the class as final, the class cannot be inherited. 2) When marking the method as final, the method cannot be rewritten by the subclass. Using final keywords ensures the stability and security of your code.

The Future of PHP: Adaptations and Innovations The Future of PHP: Adaptations and Innovations Apr 11, 2025 am 12:01 AM

The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

See all articles