设为首页 收藏本站
查看: 794|回复: 0

[经验分享] 微信公共账号开发模式PHP接口实现

[复制链接]

尚未签到

发表于 2017-4-10 13:10:55 | 显示全部楼层 |阅读模式
<?php
define("TOKEN","你的TOKEN");
class wechatCallBackApi{
//public $signature;    //加密签名
//public $timestamp;    //时间戳
//public $nonce;        //随机数
//public $echostr;    //随机字符串,返回标志
public $fromUserName;    //发信人ID
public $toUserName;        //收信人ID(本公共号)
public $keyWords;        //文本信息
public $msgId;        //消息ID
public $welcomeMsg; //欢迎消息
public $msgType;    //消息类型
public $event;        //事件名
public $mediaId;    //语音消息媒体id,可以调用多媒体文件下载接口拉取该媒体
public $format;        //语音格式:amr
public $recognition;    //语音识别结果

public function __construct(){
$this->welcomeMsg="HELLO,欢迎订阅!";
//$this->main();    //*****************第一次验证的时候打开,随后可关闭***************
}
//首次TOKEN验证
public function main(){
$echoStr=$_GET['echostr'];
if($this->checkRes()){
echo $echoStr;
}else{
exit();
}
}
//验证权限
private function checkRes(){
$signature=$_GET['signature'];    //获取微信服务发送过来的参数
$timestamp=$_GET['timestamp'];
$nonce=$_GET['nonce'];
$token = TOKEN;
$tmpArr = array($token,$timestamp,$nonce);
sort($tmpArr);    //进行字典排序
$strWaitCheck = implode($tmpArr);    //进行字符串组合
$strWaitCheck = sha1($strWaitCheck);    //对字符串进行sha1加密
if($strWaitCheck==$signature){
return true;
}else{
return false;
}
}

public function getWhatWeChatServerPost(){
$huntPostContent = $GLOBALS["HTTP_RAW_POST_DATA"];    //获取微信服务post过来的内容
$newXmlObj =simplexml_load_string($huntPostContent,'SimpleXMLElement', LIBXML_NOCDATA);    //把post过的字符串载入对象
$this->fromUserName = $newXmlObj->FromUserName;    //向公共账号发信息的用户名
$this->toUserName = $newXmlObj->ToUserName;    //公共账号名
$this->keyWords = trim($newXmlObj->Content);    //消息内容
$this->msgId = trim($newXmlObj->MsgId);        //读取当前消息msgId
$this->msgType = trim($newXmlObj->MsgType);        //读取当前消息类型
$this->event = trim($newXmlObj->Event);        //读取当前事件名
$this->format = trim($newXmlObj->Format);    //语音格式:amr
$this->mediaId = trim($newXmlObj->MediaID);    //语音消息媒体id,可以调用多媒体文件下载接口拉取该媒体
$this->recognition = trim($newXmlObj->Recognition);    //语音识别结果,UTF8编码
}
//消息响应
public function responseMsg(){
$this->getWhatWeChatServerPost();    //执行接收微信服务器POST过来的东西
//当消息类型为text的时候处理
if(!empty($this->keyWords)&& $this->msgType == "text"){
$para = $this->keyWords;
switch($para){
case "1":
$tmp_item = $this->textPicMaxTmp(
"this is a test!!",
"各位大叔大妈别太大较真,这就一测试滴",
"http://www.baidu.com/img/bdlogo.gif",
"http://www.baidu.com/");
$re_global = $this->resTextPicMax($this->fromUserName,$this->toUserName,1,$tmp_item);
echo $re_global;
break;
case "2":
$contentResText = "Hello,buddy,you send me a \"2\"[玫瑰]";
$re_global = $this->resTextMsg($this->fromUserName,$this->toUserName,$contentResText);
echo $re_global;
break;
case "000":   
$contentResText = "你想返回的字符串";
$this->longStrHandleExtendfunc($contentResText);
break;                  
default:
//调用功能判断函数处理
$this->longStrHandle($para);
break;
}
}else if($this->msgType == "event" && !empty($this->event)){        //获取事件类型
if($this->event=='subscribe'){
$contentResText = $this->welcomeMsg;    //初次关注的时候推送欢迎消息
$re_global = $this->resTextMsg($this->fromUserName,$this->toUserName,$contentResText);
}else if($this->event == "unsubscribe"){///这个地方好像没有作用!!!
$contentResText = "我擦,你咋跑了";
$re_global = $this->resTextMsg($this->fromUserName,$this->toUserName,$contentResText);
}
}else if($this->msgType == "voice"){
if($this->recognition == ""){
$contentResText ="亲,你说的火星文么,听不懂,建议您还是文字查询吧..[呲牙]";//语音无返回结果的时候给用记返回一条错误
$re_global = $this->resTextMsg($this->fromUserName,$this->toUserName,$contentResText);
}else{
$contentResText =$this->recognition; //其它情况下返回欢迎语
$re_global = $this->resTextMsg($this->fromUserName,$this->toUserName,$contentResText);
}
}else{
//如果在text情况意外得到一条空消息的处理
echo "";
exit();
}
echo $re_global;    //返回值给微信服务器
}
//多次用到的几句话就定义了一个小方法
public function longStrHandleExtendfunc($conText){
$re_global = $this->resTextMsg($this->fromUserName,$this->toUserName,$conText);
echo $re_global;
}
//收到除特定字符外的其它字符的处理方法,这个要根据你需要的具体逻辑处理   
public function longStrHandle($waitHandleStr){
/*
your code
*/
}

//回复文本信息方法
public function resTextMsg($tu,$fu,$textContent){
//文本消息模板
$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>";
$msgType = "text";    //消息类型
$time = time();        //时间戳
$resServerStr = sprintf($textTpl,$tu,$fu,$time,$msgType,$textContent);    //替换模板里的内容
return $resServerStr;
}

//回复图文
public function resTextPicMax($tu,$fu,$itemNum,$totalItemStr){
//图片模板
$picTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<ArticleCount>%s</ArticleCount>
<Articles>"
.$totalItemStr.
"</Articles>
</xml>";
$msgType = "news";    //消息类型
$time = time();        //时间戳
$resServerStr = sprintf($picTpl,$this->fromUserName,$this->toUserName,$time,$msgType,$itemNum);    //替换模板里的内容
return $resServerStr;
}

//图文模板处理
public function textPicMaxTmp($title,$des,$picUrl,$jumpUrl){
//图文消息条目模板
$itemTpl="
<item>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<PicUrl><![CDATA[%s]]></PicUrl>
<Url><![CDATA[%s]]></Url>
</item>
";
$resItemStr = sprintf($itemTpl,$title,$des,$picUrl,$jumpUrl);    //替换模板里的内容
return $resItemStr;
}

//接下来就是你自己定义的各种方法

}

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-362986-1-1.html 上篇帖子: php模拟登录qq邮箱(curl命令详解) 下篇帖子: php里获取第一个中文首字母并排序
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表