本文实例为大家分享了php微信接口类,供大家参考,具体内容如下
<?php
/**
* wechat php test
*/
//define your token
//定义token秘钥
define("token", "weixin");
//实例化微信对象
$wechatobj = new wechatcallbackapitest();
//验证成功后注释valid方法
//$wechatobj->valid();
//开启自动回复功能
$wechatobj->responsemsg();
//定义类文件
class wechatcallbackapitest
{
//实现valid验证方法:实现对接微信公众平台
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
//接受用户端发送过来的xml数据
$poststr = $globals["http_raw_post_data"];
//extract post data
//判断xml数据是否为空
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);
//通过simplexml进行xml解析
$postobj = simplexml_load_string($poststr, 'simplexmlelement', libxml_nocdata);
//接受微信的手机端
$fromusername = $postobj->fromusername;
//微信公众平台
$tousername = $postobj->tousername;
//接受用户发送的关键词
$keyword = trim($postobj->content);
//1.接受用户消息类型
$msgtype = $postobj -> msgtype;
//时间戳
$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($msgtype == 'text'){
if(!empty( $keyword ))
{
/*这是一个实例
//如果发送文本信息
$msgtype = "text";
//回复内容
if($keyword == "李楠"){
$contentstr = "叫我干嘛";
}else{
$contentstr = "叫我干嘛";
}
//格式化xml模板,参数与上面的模板是一一对应的.fromusername和头username是相反的,只写带%s的
$resultstr = sprintf($texttpl, $fromusername, $tousername, $time, $msgtype, $contentstr);
//将xml信息返回给客户端
echo $resultstr;
*/
if($keyword == "?" || $keyword == "?"){
$msgtype = "text";
$contentstr = "1.特种服务号码n2.通讯服务号码";
$resultstr = sprintf($texttpl,$fromusername,$tousername,$time,$msgtype,$contentstr);
echo $resultstr;
}elseif($keyword == 1){
$msgtype = "text";
$contentstr = "1.匪警:110n2.火警:119n3.急救:120";
$resultstr = sprintf($texttpl,$fromusername,$tousername,$time,$msgtype,$contentstr);
echo $resultstr;
}elseif($keyword == 2){
$msgtype = "text";
$contentstr = "1.中国移动:10086n2.中国联通:10010";
$resultstr = sprintf($texttpl,$fromusername,$tousername,$time,$msgtype,$contentstr);
echo $resultstr;
}
}else{
echo "不能不说话";
}
}
////////////////////////////////////////////////////////////////////////////////////
//接受图片信息
if($msgtype == "image"){
//如果发送文本信息
$msgtype = "text";
//回复内容
$contentstr = "你发送的是图片文件";
//格式化字符串
$resultstr = sprintf($texttpl, $fromusername, $tousername, $time, $msgtype, $contentstr);
//将xml信息返回给客户端
echo $resultstr;
}
////////////////////////////////////////////////////////////////////////////////////
if($msgtype == "voice"){
//如果发送文本信息
$msgtype = "text";
//回复内容
$contentstr = "你发送的是语音文件";
//格式化字符串
$resultstr = sprintf($texttpl, $fromusername, $tousername, $time, $msgtype, $contentstr);
//将xml信息返回给客户端
echo $resultstr;
}
////////////////////////////////////////////////////////////////////////////////////
if($msgtype == "video"){
//如果发送文本信息
$msgtype = "text";
//回复内容
$contentstr = "你发送的是视频文件";
//格式化字符串
$resultstr = sprintf($texttpl, $fromusername, $tousername, $time, $msgtype, $contentstr);
//将xml信息返回给客户端
echo $resultstr;
}
////////////////////////////////////////////////////////////////////////////////////
if($msgtype == "shortvideo"){
//如果发送文本信息
$msgtype = "text";
//回复内容
$contentstr = "你发送的是小视频文件";
//格式化字符串
$resultstr = sprintf($texttpl, $fromusername, $tousername, $time, $msgtype, $contentstr);
//将xml信息返回给客户端
echo $resultstr;
}
////////////////////////////////////////////////////////////////////////////////////
if($msgtype == "location"){
//如果发送文本信息
$msgtype = "text";
//回复内容
$contentstr = "你发送的是地理位置文件";
//格式化字符串
$resultstr = sprintf($texttpl, $fromusername, $tousername, $time, $msgtype, $contentstr);
//将xml信息返回给客户端
echo $resultstr;
}
////////////////////////////////////////////////////////////////////////////////////
if($msgtype == "link"){
//如果发送文本信息
$msgtype = "text";
//回复内容
$contentstr = "你发送的是连接文件";
//格式化字符串
$resultstr = sprintf($texttpl, $fromusername, $tousername, $time, $msgtype, $contentstr);
//将xml信息返回给客户端
echo $resultstr;
}
////////////////////////////////////////////////////////////////////////////////////
/*
//判断用户发送关键词是否为空
if(!empty( $keyword ))
{
//如果发送文本信息
$msgtype = "text";
//回复内容
$contentstr = "大家好,我是hero";
//格式化字符串
$resultstr = sprintf($texttpl, $fromusername, $tousername, $time, $msgtype, $contentstr);
//将xml信息返回给客户端
echo $resultstr;
}else{
echo "input something...";
}
*/
}else {
echo "";
exit;
}
}
private function checksignature()
{
// you must define token by yourself
//判断是否定义了token,如果没有就抛出一个异常
if (!defined("token")) {
throw new exception('token is not defined!');
}
$signature = $_get["signature"];//接受微信加密签名
$timestamp = $_get["timestamp"];//接受时间戳
$nonce = $_get["nonce"];//接受随机数
$token = token;//把token常量赋值给$token
//把相关参数组装成数组
$tmparr = array($token, $timestamp, $nonce);
// use sort_string rule
//排序
sort($tmparr, sort_string);
//把排序后的数组转换成字符串
$tmpstr = implode( $tmparr );
//通过哈希算法加密
$tmpstr = sha1( $tmpstr );
//与加密签名进行对比
if( $tmpstr == $signature ){
//相同返回true
return true;
}else{
//不同返回false
return false;
}
}
}
?>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持硕编程。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!