当前位置: 移动技术网 > IT编程>开发语言>PHP > php微信公众平台示例代码分析(二)

php微信公众平台示例代码分析(二)

2017年12月12日  | 移动技术网IT编程  | 我要评论

姚远如意令,凯登克洛斯,苔藓

一、摘要

微信公众平台提供了一个简单的php示例代码,在做进一步开发之前,我们有必要将其详细了解一下。

二、获取代码

微信官网:

三、分析代码

完整代码如下:

<?php
/**
 * wechat php test
 */

//define your token
define("token", "weixin");
$wechatobj = new wechatcallbackapitest();
$wechatobj->valid();

class wechatcallbackapitest
{
 public function valid()
 {
  $echostr = $_get["echostr"];

  //valid signature , option
  if($this->checksignature()){
   echo $echostr;
   exit;
  }
 }

 public function responsemsg()
 {
  //get post data, may be due to the different environments
  $poststr = $globals["http_raw_post_data"];

  //extract post data
  if (!empty($poststr)){
    
    $postobj = simplexml_load_string($poststr, 'simplexmlelement', libxml_nocdata);
    $fromusername = $postobj->fromusername;
    $tousername = $postobj->tousername;
    $keyword = trim($postobj->content);
    $time = time();
    $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>";    
    if(!empty( $keyword ))
    {
     $msgtype = "text";
     $contentstr = "welcome to wechat world!";
     $resultstr = sprintf($texttpl, $fromusername, $tousername, $time, $msgtype, $contentstr);
     echo $resultstr;
    }else{
     echo "input something...";
    }

  }else {
   echo "";
   exit;
  }
 }
  
 private function checksignature()
 {
  $signature = $_get["signature"];
  $timestamp = $_get["timestamp"];
  $nonce = $_get["nonce"]; 
    
  $token = token;
  $tmparr = array($token, $timestamp, $nonce);
  sort($tmparr);
  $tmpstr = implode( $tmparr );
  $tmpstr = sha1( $tmpstr );
  
  if( $tmpstr == $signature ){
   return true;
  }else{
   return false;
  }
 }
}

?>

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

处理并回复用户发送过来的消息,也是用的最多的一个函数,几乎所有的功能都在这里实现。

responsemsg 函数详解:

$poststr = $globals["http_raw_post_data"];
接收微信公众平台发送过来的用户消息,该消息数据结构为xml,不是php默认的识别数据类型,因此这里用了$globals['http_raw_post_data']来接收,同时赋值给了$poststr

if (!empty($poststr))
判断$poststr是否为空,如果不为空(接收到了数据),就继续执行下面的语句;如果为空,则跳转到与之相对应的else语句。

$postobj = simplexml_load_string($poststr, 'simplexmlelement', libxml_nocdata);
使用simplexml_load_string() 函数将接收到的xml消息数据载入对象$postobj中。这个严谨的写法后面还得加个判断是否载入成功的条件语句,不过不写也没事。

$fromusername = $postobj->fromusername;
将对象$postobj中的发送消息用户的openid赋值给$fromusername变量

$tousername = $postobj->tousername;
将对象$postobj中的公众账号的id赋值给$tousername变量

$keyword = trim($postobj->content);
trim() 函数从字符串的两端删除空白字符和其他预定义字符,这里就可以得到用户输入的关键词

$time = time();
time() 函数返回当前时间的 unix 时间戳,即自从 unix 纪元(格林威治时间 1970 年 1 月 1 日 00:00:00)到当前时间的秒数。

$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>";

存放微信输出内容的模板

if(!empty( $keyword ))

判断$keyword是否为空,不为空则继续执行下面的语句;如果为空,则跳转到与之相对应的else语句,即 echo "input something...";

$msgtype = "text";

消息类型是文本类型

$contentstr = "welcome to wechat world!";

回复的消息内容

$resultstr = sprintf($texttpl, $fromusername, $tousername, $time, $msgtype, $contentstr);

使用sprintf() 函数将格式化的数据写入到变量中去;
$fromusername, $tousername, $time, $msgtype, $contentstr 分别顺序替换模板里“%s”位置,也即是“$resultstr”这个变量最后实际为:

<xml>
<tousername><![cdata[$tousername]]></tousername>
<fromusername><![cdata[$fromusername]]></fromusername>
<createtime>$time</createtime>
<msgtype><![cdata[$msgtype]]></msgtype>
<content><![cdata[$contentstr]]></content>
<funcflag>0</funcflag>  //位0x0001被标志时,星标刚收到的消息。
</xml>

echo $resultstr;     //把回复的消息输出

c. private function checksignature()

开发者通过检验signature对请求进行校验(下面有校验方式)。若确认此次get请求来自微信服务器,请求原样返回echostr参数内容,则接入生效,否则接入失败。

signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。

加密/校验流程:
1. 将token、timestamp、nonce三个参数进行字典序排序
2. 将三个参数字符串拼接成一个字符串进行sha1加密
3. 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信
3.2.3 创建实例对象

$wechatobj = new wechatcallbackapitest();

3.2.4 调用类方法验证

$wechatobj->valid();

调用类的valid()方法执行接口验证,接口设置成功后将其注释掉。

四、总结

以上是对微信官方示例代码的一个分析,有解释不对的地方,还请高手指出。另外,该代码只是官方给出的简单示例代码,如果要做复杂的开发,还是要求开发者按照严谨的开发模式改写该段代码,会在后续教程中娓娓道来。

五、参考

微信官方公众平台api文档:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网