微信订阅号测试自动回复开发
微信公众平台测试号 自动回复开发、自动语音识别!!首先百度搜索微信公众号开发者文档点击登录,用微信扫码进入界面 示例文件下载位置:编辑:首先配置wx_sample.php 文件中配置成功后开始设置接口这里举例了 :订阅实践、自动回复、语音识别效果展示:代码实现:wx_sample.php 文件/
微信公众平台测试号 自动回复开发、自动语音识别!!
首先百度搜索微信公众号开发者文档
点击登录,用微信扫码进入界面
示例文件下载位置:
编辑:首先配置
wx_sample.php 文件中
配置成功后开始设置接口这里举例了 :订阅实践、自动回复、语音识别
效果展示:
代码实现:
wx_sample.php 文件
<?php
/**
* wechat php test
*/
//define your token
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
//配置方法
//$wechatObj->valid();
//具体接口操作方法
$wechatObj->responseMsg();
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"];//php:input
//写入日志 在同级目录下建立php_log.txt
//chmod 777php_log.txt(赋权) chown wwwphp_log.txt(修改主)
error_log(var_export($postStr,1),3,'php_log.txt');
//日志图片
//extract post data
if (!empty($postStr)){
/* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,
the best way is to check the validity of xml by yourself */
libxml_disable_entity_loader(true);
$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($postObj->Event=="subscribe")
{
$msgType = "text";
$contentStr = "欢迎订阅勇哥专题";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}
//语音识别
if($postObj->MsgType=="voice"){
$msgType = "text";
$contentStr = trim($postObj->Recognition,"。");
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}
//自动回复
if(!empty( $keyword ))
{
$msgType = "text";
$contentStr = "小朋友你好!";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}else{
echo "Input something...";
}
}else {
echo "";
exit;
}
}
private function checkSignature()
{
// you must define TOKEN by yourself
if (!defined("TOKEN")) {
throw new Exception('TOKEN is not defined!');
}
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
// use SORT_STRING rule
sort($tmpArr, SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
}
?>
此时就实现了功能,微信还有很多功能,这里因为实践只举例了一点!!
效果展示:
图文回复实例:
$textTpl1="<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[news]]></MsgType> <ArticleCount>2</ArticleCount> <Articles> <item> <Title><![CDATA[%s]]></Title> <Description><![CDATA[%s]]></Description> <PicUrl><![CDATA[%s]]></PicUrl> <Url><![CDATA[%s]]></Url> </item> <item> <Title><![CDATA[%s]]></Title> <Description><![CDATA[%s]]></Description> <PicUrl><![CDATA[%s]]></PicUrl> <Url><![CDATA[%s]]></Url> </item> </Articles> </xml>";
$arr=array( array( 'Title'=>'山塘街', 'Description'=>'山塘街东起阊门渡僧桥,西至苏州名胜虎丘山的望山桥,长约七里,所以苏州俗语说“七里山塘到虎丘”...', 'PicUrl'=>'http://114.115.211.150/weixin/1.png', 'url'=>'http://114.115.211.150/weixin/1.png' ), array( 'Title'=>'美丽接', 'Description'=>'山塘街东起阊门渡僧桥,西至苏州名胜虎丘山的望山桥,长约七里,所以苏州俗语说“七里山塘到虎丘”...', 'PicUrl'=>'http://114.115.211.150/weixin/1.png', 'url'=>'http://114.115.211.150/weixin/1.png' ) ); $resultStr = sprintf($textTpl1, $fromUsername, $toUsername, $time, $arr[0]['Title'], $arr[0]['Description'], $arr[0]['PicUrl'], $arr[0]['url'], $arr[1]['Title'], $arr[1]['Description'], $arr[1]['PicUrl'], $arr[1]['url']); // $resultStr = sprintf($textTpl1, $fromUsername, $toUsername, $time, $arr); echo $resultStr; } 音乐回复: 效果示例 代码:if($keyword=="海勇"){ $textTpl2="<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[music]]></MsgType> <Music> <Title><![CDATA[%s]]></Title> <Description><![CDATA[%s]]></Description> <MusicUrl><![CDATA[%s]]></MusicUrl> <HQMusicUrl><![CDATA[%s]]></HQMusicUrl> </Music> </xml>"; $title="当你老了"; $description="吉他弹唱"; $musicurl="http://114.115.211.150/weixin/1001.mp3"; $HQmusicurl="http://114.115.211.150/weixin/1001.mp3"; $resultStr = sprintf($textTpl2, $fromUsername, $toUsername, $time, $title, $description, $musicurl, $HQmusicurl); echo $resultStr; } eg:查询商品功能 效果实例:if($postObj->MsgType=="text"){ $keyword=$postObj->Content; //$this->textReplay("欢迎光临",$postObj); //开始查询商品名称 $goodsObj=new IModel("goods"); $goods_list=$goodsObj->query("name like '%".$keyword."%'"); if($goods_list){ $str=""; foreach($goods_list as $k=>$v){ $str.="商品名称:".$v['name']."\n商品价格:".$v['sell_price']."\n"; } $this->textReplay($str,$postObj); }else{ $this->textReplay("未查询到商品信息",$postObj); }
更多推荐
所有评论(0)