Table of Contents
1. Message acceptance:
Accept picture messages: " >Accept picture messages:
The template is as follows:
The documents are as follows:
1.5:接受地理位置:
1.6:接受链接消息:
2.消息的回复:
2.1回复文本消息:
2.2:回复图片的消息:
2.3回复语音的消息:
2.4:回复视频的消息:" >2.4:回复视频的消息:
2.5:回复图文消息:
Home Backend Development PHP Tutorial WeChat public account development complete tutorial 2

WeChat public account development complete tutorial 2

Apr 17, 2018 am 09:54 AM
whole develop Tutorial

This article introduces the second complete tutorial on WeChat public account development. It has certain reference value. Now I share it with everyone. Friends in need can refer to it

because of work needs , in the past two years, there have been many projects on WeChat public accounts and mini programs. That’s why I plan to write a comprehensive production tutorial. Of course, the best tutorial is the documentation of the WeChat work platform. I'm just going to talk about the production process in my work here. I host the source code of all related articles on my own github. Welcome to follow: Address Click to open the link. Let's start our tutorial.

In the first section above, I talked about turning on the developer mode and simply obtaining the access_token and caching it, as well as simple testing. We will not do this for the time being. You need to use the parameters of this

access_token. Most of the time, we will first talk about that part of our test code and improve it: (I will introduce technologies such as Baidu Maps and Turing Robots), because the company develops We use this technology when we are working, so I will tell you about it: Start:

1. Message acceptance:

When receiving a message, WeChat will send the message according to the user's content. Divide. There are text messages, picture messages, language messages, videos, connections and other messages respectively. When a user interacts with a message on the public platform, each message will request a customized URL address. During the request process, various parameters are transmitted using the xml format. Accepting a message means obtaining relevant data from the WeChat request process.

Variable substitution used in the code uses PHP functions

sprintf

##Everyone You can check it out. Its function is nothing more than to allow variables to be replaced in sequence.

1.1. Accept text messages:

The format is as above: the code is as follows : Text template


#// Text template

$textTpl = "<xml>
                            <ToUserName><![CDATA[%s]]></ToUserName>
                            <FromUserName><![CDATA[%s]]></FromUserName>
                            <CreateTime>%s</CreateTime>
                            <MsgType><![CDATA[%s]]></MsgType>
                            <Content><![CDATA[%s]]></Content>
                            <FuncFlag>0</FuncFlag>
                            </xml>";
Copy after login

You have already seen the test results in the first section, and I will not demonstrate again

1.2

Accept picture messages:


code show as below:



// Image template

$picTpl="<xml>
                    <ToUserName><![CDATA[%s]]></ToUserName>
                    <FromUserName><![CDATA[%s]]></FromUserName>
                    <CreateTime>%s</CreateTime>
                    <MsgType><![CDATA[%s]]></MsgType>
                    <Image>
                    <MediaId><![CDATA[%s]]></MediaId>
                    </Image>
                </xml>";
Copy after login

The code is as follows:


The test result is:



1.3: Receptive Language Message:

The template is as follows:


The code is as follows:

}elseif($msgType=="voice"){
$contentStr ="语音消息MediaId为:".$postObj->MediaId.&#39;具体内容为:&#39;.$postObj->Recognition;
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time,&#39;text&#39;, $contentStr);
echo $resultStr;
}
Copy after login

The result is as follows: In the above code we used one:


Attached is a picture of my test results:



1.4 Accept video messages:

The documents are as follows:


代码如下:



elseif($msgType=="video"){
$contentStr ="视频消息MediaId为:".$postObj->MediaId;
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time,&#39;text&#39;, $contentStr);
echo $resultStr;
}
Copy after login

1.5:接受地理位置:

代码:



elseif($MsgType == &#39;location&#39;){
            $contentStr = "经度为:".$postObj->Location_Y.&#39;维度&#39;.$postObj->Location_X.&#39;具体地址为:&#39;.$postObj->Label;
    $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, &#39;text&#39;, $contentStr);
    file_put_contents(&#39;2&#39;, $resultStr);
    echo $resultStr;
        }
Copy after login

结果如下:


1.6:接受链接消息:

文档如下:


代码如下:



elseif($MsgType == &#39;link&#39;){
            $contentStr = &#39;消息的标题为&#39;.$postObj->Title;
    $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, &#39;text&#39;, $contentStr);
    file_put_contents(&#39;2&#39;, $resultStr);
    echo $resultStr;
        }
Copy after login

结果如下:


上述几种情况都是使用的text模板接受的消息。设置微信回复的内容。接下来我们对回复内容的格式进行丰富:

2.消息的回复:

2.1回复文本消息:

格式如下:

代码如下:


2.2:回复图片的消息:

代码如下:



结果如下:


2.3回复语音的消息:

模板:

$voiceTpl="<xml>
            <ToUserName><![CDATA[%s]]></ToUserName>
            <FromUserName><![CDATA[%s]]></FromUserName>
            <CreateTime>%s</CreateTime>
            <MsgType><![CDATA[%s]]></MsgType>
            <Voice>
                <MediaId><![CDATA[%s]]></MediaId>
            </Voice>
            </xml>";
Copy after login

回复代码:


elseif($keyword == &#39;语音&#39;){
                // 关于此MediaId需要从素材库中获得,没有可以临时使用消息返回的媒体ID
                $MediaId = &#39;3XlXZ4-r2OTNYTFAkcmpWv4QjWtwg_15B4PytQJVwOAwHpOfc38mGZTSDkDXx9po&#39;;
                $resultStr = sprintf($voiceTpl, $fromUsername, $toUsername, $time, &#39;voice&#39;,$MediaId);
                echo $resultStr;
            }
Copy after login

结果如下:


2.4:回复视频的消息:

模板如下:


在线调试接口上传视频素材:



视频模板:


$VideoTpl="<xml>
            <ToUserName><![CDATA[%s]]></ToUserName>
            <FromUserName><![CDATA[%s]]></FromUserName>
            <CreateTime>%s</CreateTime>
            <MsgType><![CDATA[%s]]></MsgType>
            <Video>
                <MediaId><![CDATA[%s]]></MediaId>
                <Title><![CDATA[%s]]></Title>
                <Description><![CDATA[%s]]></Description>
            </Video>
            </xml>";
Copy after login

回复代码如下:



elseif($keyword=="视频"){
//关于此MediaId需要从素材库中获得,没有可以使用临时消息返回的媒体id
$MediaId="xxMyAoPbUt1u3q5Z95xrhafNzyvL3Tg08E-9Ub2m6db_Elj4XAJHr2pUOqLhREyB";
$Title = $Description ="视频还是好看的";
$resultStr = sprintf($VideoTpl, $fromUsername, $toUsername, $time, &#39;video&#39;, $MediaId, $Title,$Description);
echo $resultStr;
}
Copy after login

结果如下:


2.5:回复图文消息:

文档信息:

图文模板:



// 图文模板

$newsTpc="<xml>
                    <ToUserName><![CDATA[%s]]></ToUserName>
                    <FromUserName><![CDATA[%s]]></FromUserName>
                    <CreateTime>%s</CreateTime>
                    <MsgType><![CDATA[%s]]></MsgType>
                    <ArticleCount>%d</ArticleCount>
                    <Articles>%s</Articles>
                    </xml>";
Copy after login

回复代码:



elseif($keyword=="图文"){
$data = array(
array(&#39;Title&#39;=>&#39;图文消息&#39;,&#39;Description&#39;=>&#39;效果好像还不错啊&#39;,&#39;PicUrl&#39;=>&#39;http://mmbiz.qpic.cn/mmbiz_jpg/E3TENE8JsTAqus3ic5qEtt4wl14ibBu4UaobarzTVOP18Awt83hkZM0aI9XStapN4xay6JI4lfm0H7QnKSfxQyVA/0&#39;,&#39;Url&#39;=>&#39;http://xiaomi.com&#39;)
);
file_put_contents(&#39;2&#39;,$data[0][&#39;Title&#39;]);
for ($i=0; $i <count($data); $i++) {
$Articles .="<item>
                                <Title><![CDATA[{$data[$i][&#39;Title&#39;]}]]></Title> 
                                <Description><![CDATA[{$data[$i][&#39;Description&#39;]}]]></Description>
                                <PicUrl><![CDATA[{$data[$i][&#39;PicUrl&#39;]}]]></PicUrl>
                                <Url><![CDATA[{$data[$i][&#39;Url&#39;]}]]></Url>
                               </item>";
}
$count = count($data);
$resultStr = sprintf($newsTpc, $fromUsername, $toUsername, $time, &#39;news&#39;,$count,$Articles);
echo $resultStr;
}
Copy after login

结果如下:



到此为止我们对所有的接受和回复的代码进行的书写和演示,所有的源码我会放在我的github上面,大家可以下载和关注,这一节到此为止,下一节开始使用我们的access_token开始我们的自定义菜单。

相关推荐:

微信公众号开发完整教程一


The above is the detailed content of WeChat public account development complete tutorial 2. 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 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)

Hot Topics

Java Tutorial
1658
14
PHP Tutorial
1257
29
C# Tutorial
1231
24
Tutorial on how to use Dewu Tutorial on how to use Dewu Mar 21, 2024 pm 01:40 PM

Dewu APP is currently a very popular brand shopping software, but most users do not know how to use the functions in Dewu APP. The most detailed usage tutorial guide is compiled below. Next is the Dewuduo that the editor brings to users. A summary of function usage tutorials. Interested users can come and take a look! Tutorial on how to use Dewu [2024-03-20] How to use Dewu installment purchase [2024-03-20] How to obtain Dewu coupons [2024-03-20] How to find Dewu manual customer service [2024-03-20] How to check the pickup code of Dewu [2024-03-20] Where to find Dewu purchase [2024-03-20] How to open Dewu VIP [2024-03-20] How to apply for return or exchange of Dewu

Four recommended AI-assisted programming tools Four recommended AI-assisted programming tools Apr 22, 2024 pm 05:34 PM

This AI-assisted programming tool has unearthed a large number of useful AI-assisted programming tools in this stage of rapid AI development. AI-assisted programming tools can improve development efficiency, improve code quality, and reduce bug rates. They are important assistants in the modern software development process. Today Dayao will share with you 4 AI-assisted programming tools (and all support C# language). I hope it will be helpful to everyone. https://github.com/YSGStudyHards/DotNetGuide1.GitHubCopilotGitHubCopilot is an AI coding assistant that helps you write code faster and with less effort, so you can focus more on problem solving and collaboration. Git

In summer, you must try shooting a rainbow In summer, you must try shooting a rainbow Jul 21, 2024 pm 05:16 PM

After rain in summer, you can often see a beautiful and magical special weather scene - rainbow. This is also a rare scene that can be encountered in photography, and it is very photogenic. There are several conditions for a rainbow to appear: first, there are enough water droplets in the air, and second, the sun shines at a low angle. Therefore, it is easiest to see a rainbow in the afternoon after the rain has cleared up. However, the formation of a rainbow is greatly affected by weather, light and other conditions, so it generally only lasts for a short period of time, and the best viewing and shooting time is even shorter. So when you encounter a rainbow, how can you properly record it and photograph it with quality? 1. Look for rainbows. In addition to the conditions mentioned above, rainbows usually appear in the direction of sunlight, that is, if the sun shines from west to east, rainbows are more likely to appear in the east.

What software is photoshopcs5? -photoshopcs5 usage tutorial What software is photoshopcs5? -photoshopcs5 usage tutorial Mar 19, 2024 am 09:04 AM

PhotoshopCS is the abbreviation of Photoshop Creative Suite. It is a software produced by Adobe and is widely used in graphic design and image processing. As a novice learning PS, let me explain to you today what software photoshopcs5 is and how to use photoshopcs5. 1. What software is photoshop cs5? Adobe Photoshop CS5 Extended is ideal for professionals in film, video and multimedia fields, graphic and web designers who use 3D and animation, and professionals in engineering and scientific fields. Render a 3D image and merge it into a 2D composite image. Edit videos easily

Which AI programmer is the best? Explore the potential of Devin, Tongyi Lingma and SWE-agent Which AI programmer is the best? Explore the potential of Devin, Tongyi Lingma and SWE-agent Apr 07, 2024 am 09:10 AM

On March 3, 2022, less than a month after the birth of the world's first AI programmer Devin, the NLP team of Princeton University developed an open source AI programmer SWE-agent. It leverages the GPT-4 model to automatically resolve issues in GitHub repositories. SWE-agent's performance on the SWE-bench test set is similar to Devin, taking an average of 93 seconds and solving 12.29% of the problems. By interacting with a dedicated terminal, SWE-agent can open and search file contents, use automatic syntax checking, edit specific lines, and write and execute tests. (Note: The above content is a slight adjustment of the original content, but the key information in the original text is retained and does not exceed the specified word limit.) SWE-A

Learn how to develop mobile applications using Go language Learn how to develop mobile applications using Go language Mar 28, 2024 pm 10:00 PM

Go language development mobile application tutorial As the mobile application market continues to boom, more and more developers are beginning to explore how to use Go language to develop mobile applications. As a simple and efficient programming language, Go language has also shown strong potential in mobile application development. This article will introduce in detail how to use Go language to develop mobile applications, and attach specific code examples to help readers get started quickly and start developing their own mobile applications. 1. Preparation Before starting, we need to prepare the development environment and tools. head

Tutorial on how to turn off the payment sound on WeChat Tutorial on how to turn off the payment sound on WeChat Mar 26, 2024 am 08:30 AM

1. First open WeChat. 2. Click [+] in the upper right corner. 3. Click the QR code to collect payment. 4. Click the three small dots in the upper right corner. 5. Click to close the voice reminder for payment arrival.

PHP Tutorial: How to convert int type to string PHP Tutorial: How to convert int type to string Mar 27, 2024 pm 06:03 PM

PHP Tutorial: How to Convert Int Type to String In PHP, converting integer data to string is a common operation. This tutorial will introduce how to use PHP's built-in functions to convert the int type to a string, while providing specific code examples. Use cast: In PHP, you can use cast to convert integer data into a string. This method is very simple. You only need to add (string) before the integer data to convert it into a string. Below is a simple sample code

See all articles