php微信公众平台开发(四)回复功能开发

2022-10-22,,,,

一、简介

微信公众平台可以根据用户发送的信息进行判断,然后给出对应的回复,具有良好的交互性。下文将模拟简单的回复功能,根据这个案例,开发者也可以基本理解微信交互的原理,进行更深层次的开发。

二、思路分析

用户发送过来的文本信息,我们可以提取关键字,通过简单的 if...elseif...else... 实现。

关键代码如下:

if($keyword=="你好"){
  $contentstr = "hello";
}elseif($keyword=="苏州"){
  $contentstr = "上有天堂,下有苏杭";
}else{
  $contentstr = "感谢您关注【卓锦苏州】 微信号:zhuojinsz";
}

如果用户发送"你好",则回复"hello",如果用户发送"苏州",则回复"上有天堂,下有苏杭",其他信息,则回复你的欢迎词。

三、完整代码

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

//define your token
define("token", "zhuojin");
$wechatobj = new wechatcallbackapitest();
$wechatobj->responsemsg();
//$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);
        $rx_type = trim($postobj->msgtype);

        switch($rx_type)
        {
          case "text":
            $resultstr = $this->handletext($postobj);
            break;
          case "event":
            $resultstr = $this->handleevent($postobj);
            break;
          default:
            $resultstr = "unknow msg type: ".$rx_type;
            break;
        }
        echo $resultstr;
    }else {
      echo "";
      exit;
    }
  }

  public function handletext($postobj)
  {
    $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";

      if($keyword=="你好"){
        $contentstr = "hello";
      }elseif($keyword=="苏州"){
        $contentstr = "上有天堂,下有苏杭";
      }else{
        $contentstr = "感谢您关注【卓锦苏州】 微信号:zhuojinsz";
      }
      $resultstr = sprintf($texttpl, $fromusername, $tousername, $time, $msgtype, $contentstr);
      echo $resultstr;
    }else{
      echo "input something...";
    }
  }

  public function handleevent($object)
  {
    $contentstr = "";
    switch ($object->event)
    {
      case "subscribe":
        $contentstr = "感谢您关注【卓锦苏州】"."\n"."微信号:zhuojinsz"."\n"."卓越锦绣,名城苏州,我们为您提供苏州本地生活指南,苏州相关信息查询,做最好的苏州微信平台。"."\n"."目前平台功能如下:"."\n"."【1】 查天气,如输入:苏州天气"."\n"."【2】 查公交,如输入:苏州公交178"."\n"."【3】 翻译,如输入:翻译i love you"."\n"."【4】 苏州信息查询,如输入:苏州观前街"."\n"."更多内容,敬请期待...";
        break;
      default :
        $contentstr = "unknow event: ".$object->event;
        break;
    }
    $resultstr = $this->responsetext($object, $contentstr);
    return $resultstr;
  }
  
  public function responsetext($object, $content, $flag=0)
  {
    $texttpl = "<xml>
          <tousername><![cdata[%s]]></tousername>
          <fromusername><![cdata[%s]]></fromusername>
          <createtime>%s</createtime>
          <msgtype><![cdata[text]]></msgtype>
          <content><![cdata[%s]]></content>
          <funcflag>%d</funcflag>
          </xml>";
    $resultstr = sprintf($texttpl, $object->fromusername, $object->tousername, time(), $content, $flag);
    return $resultstr;
  }


  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;
    }
  }
}

?>

四、测试

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

《php微信公众平台开发(四)回复功能开发.doc》

下载本文的Word格式文档,以方便收藏与打印。