PHP微信公众平台开发(二) 公众平台示例代码分
【PHP微信公众平台开发系列】
01.配置微信接口
02.公众平台示例代码分析
本文地址:http://www.phpchina.com/archives/view-43358-1.html
本系列由PHPChina特邀作者@David_Tang供稿,转载请注明作者信息与本文地址。
一、摘要
微信公众平台提供了一个简单的php示例代码,在做进一步开发之前,我们有必要将其详细了解一下。
二、获取代码
微信官网:http://mp.weixin.qq.com/mpres/htmledition/res/wx_sample.zip
三、分析代码
完整代码如下:
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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
|
3.1 整体分析
原始示例代码大致分为四个部分:
- 定义TOKEN
- 声明一个类 wechatCallbackapiTest
- 创建类wechatCallbackapiTest 的一个实例对象 $wechatObj
- 调用类的 valid() 方法。
3.2 详细分析
3.2.1 定义TOKEN
define("TOKEN", "weixin");
define 是用来给常量赋值的函数,这句话的意思是赋予“TOKEN”这个常量值为“weixin”。
TOKEN 是用来进行交互安全认证的,开发者可以随意定义,要和公众平台里设置的一样。
3.2.2 声明一个类
class wechatCallbackapiTest{
}
声明一个类 wechatCallbackapiTest,该类中包含有三个方法(函数)。
a. public function valid()
用于申请 成为开发者 时向微信发送验证信息。
b. public function responseMsg()
处理并回复用户发送过来的消息,也是用的最多的一个函数,几乎所有的功能都在这里实现。
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 |
|
echo $resultStr; //把回复的消息输出
c. private function checkSignature()
开发者通过检验signature对请求进行校验(下面有校验方式)。若确认此次GET请求来自微信服务器,请求原样返回echostr参数内容,则接入生效,否则接入失败。
signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。
1 2 3 4 |
|
3.2.3 创建实例对象
$wechatObj = new wechatCallbackapiTest();
3.2.4 调用类方法验证
$wechatObj->valid();
调用类的valid()方法执行接口验证,接口设置成功后将其注释掉。
四、总结
以上是对微信官方示例代码的一个分析,有解释不对的地方,还请高手指出。另外,该代码只是官方给出的简单示例代码,如果要做复杂的开发,还是要求开发者按照严谨的开发模式改写该段代码,会在后续教程中娓娓道来。
五、参考
微信官方公众平台API文档:http://mp.weixin.qq.com/wiki/index.php

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

AI Hentai Generator
Generate AI Hentai for free.

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 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

DeepSeek: A powerful AI image generation tool! DeepSeek itself is not an image generation tool, but its powerful core technology provides underlying support for many AI painting tools. Want to know how to use DeepSeek to generate images indirectly? Please continue reading! Generate images with DeepSeek-based AI tools: The following steps will guide you to use these tools: Launch the AI Painting Tool: Search and open a DeepSeek-based AI Painting Tool (for example, search "Simple AI"). Select the drawing mode: select "AI Drawing" or similar function, and select the image type according to your needs, such as "Anime Avatar", "Landscape"

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Gate.io, a leading cryptocurrency trading platform founded in 2013, provides Chinese users with a complete official Chinese website. The website provides a wide range of services, including spot trading, futures trading and lending, and provides special features such as Chinese interface, rich resources and community support.

Gateio Exchange app download channels for old versions, covering official, third-party application markets, forum communities and other channels. It also provides download precautions to help you easily obtain old versions and solve the problems of discomfort in using new versions or device compatibility.

Gate.io (Sesame Open Door) is the world's leading cryptocurrency trading platform. This article provides a complete tutorial on spot trading of Gate.io. The tutorial covers steps such as account registration and login, KYC certification, fiat currency and digital currency recharge, trading pair selection, limit/market transaction orders, and orders and transaction records viewing, helping you quickly get started on the Gate.io platform for cryptocurrency trading. Whether a beginner or a veteran, you can benefit from this tutorial and easily master the Gate.io trading skills.
