


The WeChat public platform provides a simple PHP sample code. Before further development, it is necessary for us to understand it in detail.
WeChat official website: http://mp.weixin.qq.com/mpres/htmledition/res/wx_sample.zip
The complete code is as follows:
<?<span php </span><span /*</span><span * * wechat php test </span><span */</span> <span //</span><span define your token</span> <span define</span>("TOKEN", "weixin"<span ); </span><span $wechatObj</span> = <span new</span><span wechatCallbackapiTest(); </span><span $wechatObj</span>-><span valid(); </span><span class</span><span wechatCallbackapiTest { </span><span public</span> <span function</span><span valid() { </span><span $echoStr</span> = <span $_GET</span>["echostr"<span ]; </span><span //</span><span valid signature , option</span> <span if</span>(<span $this</span>-><span checkSignature()){ </span><span echo</span> <span $echoStr</span><span ; </span><span exit</span><span ; } } </span><span public</span> <span function</span><span responseMsg() { </span><span //</span><span get post data, May be due to the different environments</span> <span $postStr</span> = <span $GLOBALS</span>["HTTP_RAW_POST_DATA"<span ]; </span><span //</span><span extract post data</span> <span if</span> (!<span empty</span>(<span $postStr</span><span )){ </span><span $postObj</span> = <span simplexml_load_string</span>(<span $postStr</span>, 'SimpleXMLElement',<span LIBXML_NOCDATA); </span><span $fromUsername</span> = <span $postObj</span>-><span FromUserName; </span><span $toUsername</span> = <span $postObj</span>-><span ToUserName; </span><span $keyword</span> = <span trim</span>(<span $postObj</span>-><span Content); </span><span $time</span> = <span time</span><span (); </span><span $textTpl</span> = "<span <xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Content><![CDATA[%s]]></Content> <FuncFlag>0</FuncFlag> </xml></span>"<span ; </span><span if</span>(!<span empty</span>( <span $keyword</span><span )) { </span><span $msgType</span> = "text"<span ; </span><span $contentStr</span> = "Welcome to wechat world!"<span ; </span><span $resultStr</span> = <span sprintf</span>(<span $textTpl</span>, <span $fromUsername</span>, <span $toUsername</span>, <span $time</span>, <span $msgType</span>, <span $contentStr</span><span ); </span><span echo</span> <span $resultStr</span><span ; }</span><span else</span><span { </span><span echo</span> "Input something..."<span ; } }</span><span else</span><span { </span><span echo</span> ""<span ; </span><span exit</span><span ; } } </span><span private</span> <span function</span><span checkSignature() { </span><span $signature</span> = <span $_GET</span>["signature"<span ]; </span><span $timestamp</span> = <span $_GET</span>["timestamp"<span ]; </span><span $nonce</span> = <span $_GET</span>["nonce"<span ]; </span><span $token</span> =<span TOKEN; </span><span $tmpArr</span> = <span array</span>(<span $token</span>, <span $timestamp</span>, <span $nonce</span><span ); </span><span sort</span>(<span $tmpArr</span><span ); </span><span $tmpStr</span> = <span implode</span>( <span $tmpArr</span><span ); </span><span $tmpStr</span> = <span sha1</span>( <span $tmpStr</span><span ); </span><span if</span>( <span $tmpStr</span> == <span $signature</span><span ){ </span><span return</span> <span true</span><span ; }</span><span else</span><span { </span><span return</span> <span false</span><span ; } } } </span>?>
3.1 Overall analysis
The original sample code is roughly divided into four parts:
- Define TOKEN
- Declare a class wechatCallbackapiTest
- Create an instance object of class wechatCallbackapiTest $wechatObj
- Call the valid() method of the class.
3.2 Detailed analysis
3.2.1 Define TOKEN
3.2.2 Declare a class
<span <span><span><span <strong>responseMsg 函数详解:</strong></span><br /><br /></span></span><span>$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];</span><br />接收微信公众平台发送过来的用户消息,该消息数据结构为XML,不是php默认的识别数据类型,因此这里用了$GLOBALS['HTTP_RAW_POST_DATA']来接收,同时赋值给了$postStr<br /><br />if (!empty($postStr))<br />判断$postStr是否为空,如果不为空(接收到了数据),就继续执行下面的语句;如果为空,则跳转到与之相对应的else语句。<br /><br />$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);<br />使用simplexml_load_string() 函数将接收到的XML消息数据载入对象$postObj中。这个严谨的写法后面还得加个判断是否载入成功的条件语句,不过不写也没事。<br /><br />$fromUsername = $postObj->FromUserName;<br />将对象$postObj中的发送消息用户的OPENID赋值给$fromUsername变量<br /><br />$toUsername = $postObj->ToUserName;<br />将对象$postObj中的公众账号的ID赋值给$toUsername变量<br /><br />$keyword = trim($postObj->Content);<br />trim() 函数从字符串的两端删除空白字符和其他预定义字符,这里就可以得到用户输入的关键词<br /><br />$time = time();<br />time() 函数返回当前时间的 Unix 时间戳,即自从 Unix 纪元(格林威治时间 1970 年 1 月 1 日 00:00:00)到当前时间的秒数。<br /><br />$textTpl = "<xml><br /></span> <ToUserName><![CDATA[%s]]></ToUserName><br /> <FromUserName><![CDATA[%s]]></FromUserName><br /> <CreateTime>%s</CreateTime><br /> <MsgType><![CDATA[%s]]></MsgType><br /> <Content><![CDATA[%s]]></Content><br /> <FuncFlag>0</FuncFlag><br /> </xml>";<br />存放微信输出内容的模板<br /><br />if(!empty( $keyword ))<br />判断$keyword是否为空,不为空则继续执行下面的语句;如果为空,则跳转到与之相对应的else语句,即 echo "Input something...";<br /><br />$msgType = "text";<br />消息类型是文本类型<br /><br />$contentStr = "Welcome to wechat world!";<br />回复的消息内容<br /><br />$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);<br />使用sprintf() 函数将格式化的数据写入到变量中去;<br />$fromUsername, $toUsername, $time, $msgType, $contentStr 分别顺序替换模板里“%s”位置,也即是“$resultStr”这个变量最后实际为:
echo $resultStr; //Output the reply message
<span 加密/校验流程: 1. 将token、timestamp、nonce三个参数进行字典序排序 2. 将三个参数字符串拼接成一个字符串进行sha1加密 3. 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信</span>
3.2.4 Calling class method verification
$wechatObj->valid();
The above is an analysis of WeChat’s official sample code. If there are any incorrect explanations, please ask experts to point them out. In addition, this code is only a simple example code provided by the official. If complex development is required, developers are still required to rewrite this code according to a rigorous development model, which will be explained in subsequent tutorials.
WeChat official public platform API document: http://mp.weixin.qq.com/wiki/index.php
Please follow Zhuojin Suzhou WeChat public account, Zhuojin Suzhou is developed based on the SAE platform and is developed and tested for mainstream WeChat functions.
You can follow the Zhuojin Suzhou public account to conduct functional testing and obtain new application development.
1. Log in to the WeChat client, friends -> Add friends -> Search number -> zhuojinsz, find and follow.
2. Scan the QR code:
Zhuojin Suzhou Function list.
We Believe, Great People Share Knowledge...

微信文件的过期时间需要根据情况来判断:1、如果发送的文件没有打开过,则在72小时以后微信系统会自动清理掉,即过了三天文件就会过期;2、如果已经查看了微信文件,但是并没有下载(当然已经下载的文件也是一样的),那么文件是可以保留180天的,在这180天以内随时都可以去下载。

区别:1、拉黑后对话框从主页消失,但是聊天记录还在;删除后聊天记录全部消失不见了。2、拉黑后还能发给他,但是收不到他的消息;删除后不能发信息了。3、拉黑后双方都不可见彼此的朋友圈;删除对方以后,你看不到对方的朋友圈了,对方是否能看到你的,取决于设置(允许陌生人查看十张照片)与否,如果设置则可以看到朋友圈。

支持微信付款的购物平台有:1、京东,是中国的综合网络零售商;2、唯品会,是一家在线销售品牌折扣商品的互联网公司;3、拼多多,是社交新电商领导者,更懂消费者的购物平台;4、京喜,是京东旗下生活消费商城;5、蘑菇街,一个电子商务网站;6、聚美优品,是一家以销售化妆品为主的时尚购物网站;7、微店,是一个云推广电子商务平台;8、考拉海购,是一个跨境海淘业务为主的会员电商平台。

微信查看ip地址的方法:1、登录电脑版微信,右键点击屏幕下方的任务栏,点击“任务管理器”;2、弹出任务管理器时,点击左下角的“详细信息”;3、任务管理器进入“性能”选项,点击“打开资源监视器”;4、选择“网络”,勾选微信进程“Wechat.exe”;5、点击下面的“TCP连接”即可监视微信网络IP相关情况,发送消息得到回复就会显示他人的IP地址。

可以。未经过实名认证的微信号,可以绑定他人的银行卡,但在绑定过程中需要提供银行卡的开户人姓名、开户行地址、开户时预留的联系方式及银行卡支付密码;已通过实名认证的微信号,无法绑定他人银行卡,只能添加使用自己身份证办理的银行卡。

不是,一个身份证能绑定5个微信。按照微信当前规定,一个身份证可以实名认证5个微信号;如果已经实名认证了5个微信账号,但是还想要继续实名,就要把已经实名认证的一些不用的微信号清除以后,才可以再实名认证新的微信号。

财付通是微信,是腾讯公司旗下的第三方支付平台,其核心业务是协助在互联网上进行交易的双方完成支付和收款,其使用方式是:1、进行账户注册及登录;2、进行账户充值;3、根据需求设置快捷支付;4、通过打开微信支付或QQ钱包查询交易账单。

电脑微信打字打一个少一个是因为开启了改写状态,其解决办法:1、打开电脑微信;2、在微信聊天窗口输入对话文字内容;3、找到并按下键盘上的Insert键即可正常输入文字内容。


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

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Atom editor mac version download
The most popular open source editor

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.
