PHP微信开发之模板消息回复,php模板消息回复_PHP教程
PHP微信开发之模板消息回复,php模板消息回复
微信公众平台上,有个模板消息回复接口:可以用来实现回复特定的消息-----比如用户下了订单之后,回复给用户您的订单正在处理中...比如下面这张图:
用户进行了一些操作之后,服务号能够返回特定的消息----这就是模板消息。
要实现模板消息的回复,你的公众号必须是经过微信认证的,如果是个人的订阅号,暂时不支持微信认证。不过也不影响做测试-----你可以使用测试公众号:
使用测试号的时候,接口信息的配置就不讲了,可以参考我之前的博客。
使用维码登录测试号之后,需要用你的微信号去关注这个测试号,不然看不到模板消息的回复。
我在做测试的时候,用了chrome浏览器的一个插件:postman。该插件可以指定发送GET,POST等各种HTTP请求,功能强大,在测试接口时很有用。
1、发送GET请求获取测试号的access_token:https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=你的测试号appID&secret=你的测试号appsecret
2、设置所属行业:
发送一个POST请求,并带上参数:
请求:https://api.weixin.qq.com/cgi-bin/template/api_set_industry?access_token=你刚才得到的access_token
参数:
{ "industry_id1":"1", "industry_id2":"2" }
参数里的1和2表示所属的行业,具体的行业可以参考文档:http://mp.weixin.qq.com/wiki/12/bd383158b0f8435c07b8b6bc7cdbac9c.html
具体发送post请求大家可以使用curl发送,应该不难。我使用postman来发送。发送完成之后返回成功的json消息,然后就可以进行下一步
3、获得模板ID:这一块你可以自定义,也可以使用微信内置的一些。
比如使用自定义的模板ID:在你的测试号里有个“新增测试模板”
提交之后,就可以看到自己模板ID出现了:
然后另一种生成模板ID的方法是发送post请求(可以curl发送或者用postman发送)
请求:https://api.weixin.qq.com/cgi-bin/template/api_add_template?access_token=ACCESS_TOKEN
参数:
{ "template_id_short":"TM00015" }
其中的TM00015是微信内置的模板的ID,你可以在https://mp.weixin.qq.com的图形化界面里找到(须微信认证)
4、获得模板ID之后,刷新你的测试号页面,可以看到出现了:
5、用你得到的template_id_short,再发送一个post请求就可以得到一个msgid:
请求:https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=你的access_token
发送的数据:
{ "touser":"测试号的关注者的openId", "template_id":"你刚才得到的模板ID", "url":"关注者点击你的模板消息时跳转的链接", "data":{ "first": { "value":"恭喜你购买成功!", "color":"#173177" }, "orderMoneySum":{ "value":"666", "color":"#173177" }, "orderProductName": { "value":"男士正装", "color":"#173177" }, "Remark":{ "value":"欢迎再次购买!", "color":"#173177" } } }
color表示你的模板里字段的颜色
官方的文档里没有对上面的参数作说明,我在上面用中文写出了每个字段的意思,其中需要注意的是,touser不是关注者在微信客户端的“微信号”,而是如下图里的微信号
把参数用post方式发送之后,就会得到一个msgid:
得到这个成功响应的时候,同时,你的测试公众号应该也返回了一个模板消息给你(关注者)
本文只是介绍怎样做出一个模板消息,如果想用到项目里,还请自行翻阅微信开发者文档,结合微支付,事件推送等高级功能一起来做。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持帮客之家。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...
