php接收微信消息,PHP-微信公众平台开发-接收用户输入消息类型并响应

// 微信公众账号接收到用户的消息类型判断

//

define("TOKEN", "weixin");$wechatObj = newwechatCallbackapiTest();if (!isset($_GET['echostr'])) {$wechatObj->responseMsg();

}else{$wechatObj->valid();

}classwechatCallbackapiTest

{public functionvalid()

{$echoStr = $_GET["echostr"];if($this->checkSignature()){echo $echoStr;exit;

}

}private functioncheckSignature()

{$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;

}

}public functionresponseMsg()

{$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];if (!empty($postStr)){$postObj = simplexml_load_string($postStr, 'SimpleXMLElement',LIBXML_NOCDATA);$RX_TYPE = trim($postObj->MsgType);//用户发送的消息类型判断

switch ($RX_TYPE)

{case "text": //文本消息

$result = $this->receiveText($postObj);break;case "image": //图片消息

$result = $this->receiveImage($postObj);break;case "voice": //语音消息

$result = $this->receiveVoice($postObj);break;case "video": //视频消息

$result = $this->receiveVideo($postObj);break;case "location"://位置消息

$result = $this->receiveLocation($postObj);break;case "link": //链接消息

$result = $this->receiveLink($postObj);break;default:

$result = "unknow msg type: ".$RX_TYPE;break;

}echo $result;

}else{echo "";exit;

}

}/** 接收文本消息*/

private function receiveText($object)

{$content = "你发送的是文本,内容为:".$object->Content;$result = $this->transmitText($object, $content);return $result;

}/** 接收图片消息*/

private function receiveImage($object)

{$content = "你发送的是图片,地址为:".$object->PicUrl;$result = $this->transmitText($object, $content);return $result;

}/** 接收语音消息*/

private function receiveVoice($object)

{$content = "你发送的是语音,媒体ID为:".$object->MediaId;$result = $this->transmitText($object, $content);return $result;

}/** 接收视频消息*/

private function receiveVideo($object)

{$content = "你发送的是视频,媒体ID为:".$object->MediaId;$result = $this->transmitText($object, $content);return $result;

}/** 接收位置消息*/

private function receiveLocation($object)

{$content = "你发送的是位置,纬度为:".$object->Location_X.";经度为:".$object->Location_Y.";缩放级别为:".$object->Scale.";位置为:".$object->Label;$result = $this->transmitText($object, $content);return $result;

}/** 接收链接消息*/

private function receiveLink($object)

{$content = "你发送的是链接,标题为:".$object->Title.";内容为:".$object->Description.";链接地址为:".$object->Url;$result = $this->transmitText($object, $content);return $result;

}/** 回复文本消息*/

private function transmitText($object, $content)

{$textTpl = "

%s

";$result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content);return $result;

}

}?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值