<span style="font-family:Courier New;font-size:14px;"><?php
define("TOKEN","weixin");
$weixinObj = new Wechat();
$weixinObj->valid();
class Wechat{
public function valid(){
$echoStr = $_GET['echostr'];
//如果是第一次接入
if($this->checkSignature() && $echoStr ){
echo $echoStr;
exit;
}else{
$this->responseMsg();
}
}
//校验方法
private function checkSignature(){
$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;
}
}
/* 普通文本消息
<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
<CreateTime>1348831860</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[this is a test]]></Content>
</xml>
*/
public function responseMsg(){
//获取微信服务器POST请求中的数据
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
if( !empty($postStr) ){
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$fromUser = $postObj->FromUserName;
$toUser = $postObj->ToUserName;
$keyword = trim($postObj->Content);
$time = time();
$template = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
if( strtolower($postObj->MsgType)!='text' ){
$msgType = "text";
$content = "我只接受文本消息";
}else{
$msgType = "text";
if( !empty($keyword) ){
$content = "您发送的消息是:".$postObj->Content;
}else{
$content = "请输入关键字";//消息为空
}
}
$info = sprintf($template, $fromUser, $toUser, $time, $msgType, $content);
echo $info;
}else{
echo "";
exit;
}
}
}</span>
<span style="font-family:Courier New;font-size:14px;"><?php
define("TOKEN","weixin");
$weixinObj = new Wechat();
$weixinObj->valid();
class Wechat{
public function valid(){
$echoStr = $_GET['echostr'];
//如果是第一次接入
if($this->checkSignature() && $echoStr ){
echo $echoStr;
exit;
}else{
$this->responseMsg();
}
}
//校验方法
private function checkSignature(){
$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;
}
}
/* 接收图片消息格式
<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
<CreateTime>1348831860</CreateTime>
<MsgType><![CDATA[image]]></MsgType>
<PicUrl><![CDATA[this is a url]]></PicUrl>
<MediaId><![CDATA[media_id]]></MediaId>
<MsgId>1234567890123456</MsgId>
</xml>
*/
public function responseMsg(){
//获取微信服务器POST请求中的数据
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
if( !empty($postStr) ){
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$fromUser = $postObj->FromUserName;
$toUser = $postObj->ToUserName;
$time = time();
$msgType= $postObj->MsgType;
$picUrl = $postObj->PicUrl;
$mediaId = $postObj->MediaId;
$template = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
if( strtolower($msgType)!='image' ){
$msgType = "text";
$content = "我只接受图片消息";
}else{
$msgType = "text";
if( !empty( $picUrl ) ){
$content = "图片链接为:".$picUrl."\n";
$content .= "媒体id:".$mediaId;
}else{
$content = "请发送图片";//消息为空
}
}
$info = sprintf($template, $fromUser, $toUser, $time, $msgType, $content);
echo $info;
}else{
echo "";
exit;
}
}
}</span>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有