本文实例讲述了php版微信公众平台接口开发之智能回复功能实现方法。分享给大家供大家参考,具体如下:
智能回复是根据用户输入的条件来反馈结果用用户了,这个小编以前有做过信整理了一些例子供各位参考,比较完整主要是介绍在开发端了。
微信自推出后,着实火了一把,而支付功能的推出,又把微信推到了一个无可比拟的高度,然后申请微信订阅号或者服务号的人也开始比肩接踵。下面我将给大家简单讲解下微信公众平台开发接口。
先去 微信公众平台 申请账号,然后按照提示一步步。在选择订阅号和服务号上,个人只能申请订阅号,而且局限于基础功能;而企业两者都可以申请。订阅号和服务号的区别在于:订阅号可以每天群发一条消息,而服务号一个月才能群发一条;订阅号需要微信认证才能自定义菜单(企业才能认证,认证300元一次),而服务号则一开始就有自定义菜单,但是也可以认证,认证后服务号直接升级高级功能。更多差异请百度...
我申请的是订阅号,因为是个人。只要传一张手捧身份证的人头照就可以了,虽然有点傻。然后等待信息登记审核(一天左右时间)。通过后直接进入 微信公众平台 ,点击功能进入高级功能,关闭编辑模式,开启开发模式,然后下载微信提供的demo,解压,就一个文件:wx_sample.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 (!emptyempty($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(!emptyempty( $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, sort_string);
$tmpstr = implode( $tmparr );
$tmpstr = sha1( $tmpstr );
if( $tmpstr == $signature ){
return true;
}else{
return false;
}
}
}
?>
其实就是认证,然后发送消息。将文件传到你的服务器上,我放在根目录下,然后修改开发模式下的url和token值。假设这里使用的url是http://www.jb51.net/wx_sample.php,token就是上面define的token,这个可以改的,只要两边保持一致,默认是weixin。然后点提交,就会提示你成功了。然后扫下你申请的号码,发个消息,你会发现没反应,这个时候我们需要小调整一下,关闭接口文档中调用认证的方法,开启调用处理回复信息的方法:
//$wechatobj->valid();
$wechatobj->responsemsg();
这个时候你再发个消息,你就会收到:welcome to wechat world!
是不是在关注了有些订阅号或者服务号之后,马上会收到一条消息。什么回复1,怎样怎样;回复2,怎样怎样之类的。
拿我自己的博客举例,我的关注语是:
感谢您关注andyyang个人博客微信小助手。
回复【1】返回两篇最新文章
回复【2】返回两篇人气文章
回复【3】返回两篇热评文章
回复【4】返回两篇最新技术文章
回复【5】返回两篇最新写作文章
回复其他返回搜索关键字的两篇文章
更多精彩内容,尽在:www.jb51.net。亲们,请多多支持哦,谢谢~
那这个怎么实现呢?直接上代码:
<?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"];
//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();
$msgtype = $postobj->msgtype; //add
$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($msgtype != 'event') {
if(!empty( $keyword ))
{
$msgtype = "text";
$contentstr = "welcome to wechat world!";
}else{
echo "input something...";
}
} else {
$msgtype = "text";
$contentstr = "感谢您关注andyyang个人博客微信小助手。rn".
"回复【1】返回两篇最新文章rn".
"回复【2】返回两篇人气文章rn".
"回复【3】返回两篇热评文章rn".
"回复【4】返回两篇最新技术文章rn".
"回复【5】返回两篇最新写作文章rn".
"回复其他返回搜索关键字的两篇文章rn".
"更多精彩内容,尽在:<a href='http://www.jb51.net'>www.jb51.net</a>。亲们,请多多支持哦,谢谢~";
;
}
$resultstr = sprintf($texttpl, $fromusername, $tousername, $time, $msgtype, $contentstr);
echo $resultstr;
}else {
echo "";
exit;
}
}
private function checksignature()
{
$signature = $_get["signature"];
$timestamp = $_get["timestamp"];
$nonce = $_get["nonce"];
$token = token;
$tmparr = array($token, $timestamp, $nonce);
sort($tmparr, sort_string); //这个在新的sdk中添加了第二个参数(compare items as strings)
$tmpstr = implode( $tmparr );
$tmpstr = sha1( $tmpstr );
if( $tmpstr == $signature ){
return true;
}else{
return false;
}
}
}
当然这里只是简单的实现下,在微信公众平台提供的sdk上做简单的修改,实际上msgtype类型很多,就算消息类型为event的,它里面也有subscribe、location等,而如果细化的话,就用event为subscribe来处理初次关注的事件,代码如下:
<?php
define("token", "weixin");
$wechatobj = new wechatcallbackapitest();
$wechatobj->weixin_run();
class wechatcallbackapitest {
private $fromusername;
private $tousername;
private $times;
private $keyword;
private $msgtype;
public function responsemsg() {
$poststr = $globals["http_raw_post_data"];
if (!emptyempty($poststr)) {
$postobj = simplexml_load_string($poststr, 'simplexmlelement', libxml_nocdata);
$this->fromusername = $postobj->fromusername;
$this->tousername = $postobj->tousername;
$this->keyword = trim($postobj->content);
$this->time = time();
$this->msgtype = $postobj->msgtype;
} else {
echo "pay attention to <a href='http://{$_server['http_host']}'>http://{$_server['http_host']}</a>,thanks!";
exit;
}
}
public function weixin_run() {
$this->responsemsg();
if($this->msgtype != 'event') { //attention
$data = $this->getdata();
$this->fun_xml("news", $data, count($data));
} else {
$data = $this->getweldata();
$this->fun_xml("text", $data, 1);
}
}
//type: text 文本类型, news 图文类型
//text,array(内容),array(id)
//news,array(array(标题,介绍,图片,超链接),...小于10条),条数
private function fun_xml($type, $value_arr, $count) {
$con="<xml>
<tousername><![cdata[{$this->fromusername}]]></tousername>
<fromusername><![cdata[{$this->tousername}]]></fromusername>
<createtime>{$this->times}</createtime>
<msgtype><![cdata[{$type}]]></msgtype>";
switch($type) {
case "text" :
$con.="<content><![cdata[$value_arr]]></content>";
break;
case "news" :
$con.="<articlecount>{$count}</articlecount>
<articles>";
foreach($value_arr as $key => $v) {
$con.="<item>
<title><![cdata[{$v[0]}]]></title>
<description><![cdata[{$v[1]}]]></description>
<picurl><![cdata[{$v[2]}]]></picurl>
<url><![cdata[{$v[3]}]]></url>
</item>";
}
$con.="</articles>";
break;
}
echo $con."</xml>";
}
private function getdata() {
//数据库通过关键字查询文章
//。。。。。。。。。。。。
//。。。。。。。。。。。。
//返回文章结果的数组
return $data;
}
private function getweldata() {
$data = "感谢您关注andyyang个人博客微信小助手。rn".
"回复【1】返回两篇最新文章rn".
"回复【2】返回两篇人气文章rn".
"回复【3】返回两篇热评文章rn".
"回复【4】返回两篇最新技术文章rn".
"回复【5】返回两篇最新写作文章rn".
"回复其他返回搜索关键字的两篇文章rn".
"更多精彩内容,尽在:<a href='http://www.jb51.net/'>www.jb51.net</a>。亲们,请多多支持哦,谢谢~";
;
return $data;
}
}
老实说很想弄个服务号玩玩,自定义菜单是没什么技术含量的,但是后面的微信支付之类,服务号仅有的服务,还是挺值得去尝试下的.
更多关于php相关内容感兴趣的读者可查看本站专题:《php微信开发技巧汇总》、《php编码与转码操作技巧汇总》、《php网络编程技巧总结》、《php基本语法入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家php程序设计有所帮助。
【说明】:
本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!