PHP前端开发

php微信公众平台开发回复功能实例代码

百变鹏仔 2天前 #前端问答
文章标签 实例

这篇文章主要介绍了php微信公众平台开发的第四篇,微信回复功能开发,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

一、简介

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

二、思路分析

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

立即学习“PHP免费学习笔记(深入)”;

关键代码如下:


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

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

三、完整代码


<?php /** * wechat php test *///define your tokendefine("TOKEN", "zhuojin");$wechatObj = new wechatCallbackapiTest();$wechatObj->responseMsg();//$wechatObj-&gt;valid();class wechatCallbackapiTest{  /*public function valid()  {    $echoStr = $_GET["echostr"];    //valid signature , option    if($this-&gt;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-&gt;MsgType);        switch($RX_TYPE)        {          case "text":            $resultStr = $this-&gt;handleText($postObj);            break;          case "event":            $resultStr = $this-&gt;handleEvent($postObj);            break;          default:            $resultStr = "Unknow msg type: ".$RX_TYPE;            break;        }        echo $resultStr;    }else {      echo "";      exit;    }  }  public function handleText($postObj)  {    $fromUsername = $postObj-&gt;FromUserName;    $toUsername = $postObj-&gt;ToUserName;    $keyword = trim($postObj-&gt;Content);    $time = time();    $textTpl = "<xml>          <tousername></tousername>          <fromusername></fromusername>          <createtime>%s</createtime>          <msgtype></msgtype>          <content></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-&gt;Event)    {      case "subscribe":        $contentStr = "感谢您关注【卓锦苏州】".""."微信号:zhuojinsz".""."卓越锦绣,名城苏州,我们为您提供苏州本地生活指南,苏州相关信息查询,做最好的苏州微信平台。".""."目前平台功能如下:".""."【1】 查天气,如输入:苏州天气".""."【2】 查公交,如输入:苏州公交178".""."【3】 翻译,如输入:翻译I love you".""."【4】 苏州信息查询,如输入:苏州观前街".""."更多内容,敬请期待...";        break;      default :        $contentStr = "Unknow Event: ".$object-&gt;Event;        break;    }    $resultStr = $this-&gt;responseText($object, $contentStr);    return $resultStr;  }    public function responseText($object, $content, $flag=0)  {    $textTpl = "<xml>          <tousername></tousername>          <fromusername></fromusername>          <createtime>%s</createtime>          <msgtype></msgtype>          <content></content>          <funcflag>%d</funcflag>          </xml>";    $resultStr = sprintf($textTpl, $object-&gt;FromUserName, $object-&gt;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;    }  }}?&gt;

四、测试