得到微信消息并存入数据库

整个代码中不能有  echo输出  否则你的公众号会出现  无法提供服务的现象,当出现这种情况时 肯定是你的代码出现了问题   只要修改成功后此现象就回消失。

<?php
define("TOKEN", "dropet");

include_once "../encode/wxBizMsgCrypt.php";
$wechatObj = new wechatCallbackapiTest();
if (isset($_GET['echostr'])) {
	$wechatObj -> valid();
} else {
	$wechatObj -> responseMsg();
}

class wechatCallbackapiTest {

	public function valid() {
		$echoStr = $_GET["echostr"];
		if ($this -> checkSignature()) {
			///echo $echoStr;
			exit ;
		}
	}

	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;
		}
	}

	public function responseMsg() {
		$encodingAesKey = "...";
		$token = "dropet";
		$timeStamp = $_GET['timestamp'];
		$nonce = $_GET['nonce'];
		$appId = "...";
		$appSecret = "...";
		$msg_sign = $_GET['msg_signature'];
		$pc = new WXBizMsgCrypt($token, $encodingAesKey, $appId);
		$postData = $GLOBALS["HTTP_RAW_POST_DATA"];
		$postStr = simplexml_load_string($postData, "SimpleXMLElement", LIBXML_NOCDATA);
		$encrypt = $postStr -> Encrypt;
		$format = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%s]]></Encrypt></xml>";
		$from_xml = sprintf($format, $encrypt);
		$msg = '';
		$errCode = $pc -> decryptMsg($msg_sign, $timeStamp, $nonce, $from_xml, $msg);

		if ($errCode == 0) {
			// print("解密后: " . $msg . "\n");
			$counter_file = 'E5.txt ';
			$fopen = fopen($counter_file, 'wb');
			fputs($fopen, $msg . "encryptMsg2222");
			fclose($fopen);
		} else {
			print($errCode . "\n");
		}

		$postStr = $msg;
		if (!empty($postStr)) {

			global $msgid;
			global $tousername;
			global $fromusername;
			global $creattime;
			global $msgtype;
			global $content;
			global $picurl;
			global $mediaid;
			global $format;
			global $recognition;
			global $thumbmediaId;
			global $location_x;
			global $location_y;
			global $scale;
			global $label;
			global $title;
			global $description;
			global $url;

			$msgid = null;
			$tousername = null;
			$fromusername = null;
			$creattime = null;
			$msgtype = null;
			$content = null;
			$picurl = null;
			$mediaid = null;
			$format = null;
			$recognition = null;
			$thumbmediaId = null;
			$location_x = null;
			$location_y = null;
			$scale = null;
			$label = null;
			$title = null;
			$description = null;
			$url = null;

			$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
			$RX_TYPE = trim($postObj -> MsgType);
			$counter_file = 'bb.txt ';
			//文件名及路径,在当前目录下新建aa.txt文件
			$fopen = fopen($counter_file, 'wb');
			//新建文件命令
			fputs($fopen, $postStr);
			//向文件中写入内容;
			fclose($fopen);

			$msgid = $postObj -> MsgId;
			$tousername = $postObj -> ToUserName;
			$fromusername = $postObj -> FromUserName;
			$creattime = $timeStamp;
			$msgtype = $RX_TYPE;

			switch ($RX_TYPE) {
				case "text" :
					$content = $postObj -> Content;
					$resultStr = $this -> receiveText($postObj);
					break;
				case "image" :
					$picurl = $postObj -> PicUrl;
					$resultStr = $this -> receiveImage($postObj);
					break;
				case "location" :
					$location_x = $postObj -> Location_X;
					$location_y = $postObj -> Location_Y;
					$scale = $postObj -> Scale;
					$label = $postObj -> Label;
					$resultStr = $this -> receiveLocation($postObj);
					break;
				case "voice" :
					$mediaid = $postObj -> MediaId;
					$format = $postObj -> Format;
					$recognition = $postObj -> Recognition;
					$resultStr = $this -> receiveVoice($postObj);
					break;
				case "video" :
					$mediaid = $postObj -> MediaId;
					$thumbmediaId = $postObj -> ThumbMediaId;
					$resultStr = $this -> receiveVideo($postObj);
					break;
				case "link" :
					$title = $postObj -> Title;
					$description = $postObj -> Description;
					$url = $postObj -> Url;
					$resultStr = $this -> receiveLink($postObj);
					break;
				case "event" :
					$resultStr = $this -> receiveEvent($postObj);
					break;
				case "shortvideo" :
					$mediaid = $postObj -> MediaId;
					$thumbmediaId = $postObj -> ThumbMediaId;
					$resultStr = $this -> receiveShortVideo($postObj);
					break;
				default :
					$resultStr = "unknow msg type: " . $RX_TYPE;
					break;
			}
			$mysqli = new mysqli('主机', '用户名', '密码', '数据库');
			$mysqli -> query("SET NAMES utf8");
			if ($mysqli -> connect_errno) {
				$counter_file = 'af.txt ';
				//文件名及路径,在当前目录下新建aa.txt文件
				$fopen = fopen($counter_file, 'wb');
				//新建文件命令
				fputs($fopen, "con_err");
				//向文件中写入内容;
				fclose($fopen);
				exit();
			}

			$query = "insert into message values (null,'" . $msgid . "','" . $tousername . "','" . $fromusername . "','" . $creattime . "','" . $msgtype . "','" . $content . "','" . $picurl . "','" . $mediaid . "','" . $format . "','" . $recognition . "','" . $thumbmediaId . "','" . $location_x . "','" . $location_y . "','" . $scale . "','" . $label . "','" . $title . "','" . $description . "','" . $url . "')";
			$result2 = $mysqli -> query($query);
			if ($result2) {
				$tt = 'sucess';
			} else {
				$tt = 'fail';
			}
			$mysqli -> close();
			$counter_file = 'at.txt ';
			//文件名及路径,在当前目录下新建aa.txt文件
			$fopen = fopen($counter_file, 'wb');
			//新建文件命令
			fputs($fopen, $query);
			//向文件中写入内容;
			fclose($fopen);

			//echo $resultStr;
			$counter_file = 'aa.txt ';
			//文件名及路径,在当前目录下新建aa.txt文件
			$fopen = fopen($counter_file, 'wb');
			//新建文件命令
			fputs($fopen, $resultStr);
			//向文件中写入内容;
			fclose($fopen);
		} else {
			//  echo "";
			exit ;
		}
	}

	private function receiveText($object) {
		$funcFlag = 0;
		$contentStr = $object -> Content;
		$resultStr = $this -> transmitText($object, $contentStr, $funcFlag);
		return $resultStr;
	}

	private function receiveImage($object) {
		$funcFlag = 0;
		$contentStr = "你发送的是图片,地址为:" . $object -> PicUrl;
		$resultStr = $this -> transmitText($object, $contentStr, $funcFlag);
		return $resultStr;
	}

	private function receiveLocation($object) {
		$funcFlag = 0;
		$contentStr = "你发送的是位置,纬度为:" . $object -> Location_X . ";经度为:" . $object -> Location_Y . ";缩放级别为:" . $object -> Scale . ";位置为:" . $object -> Label;
		$resultStr = $this -> transmitText($object, $contentStr, $funcFlag);
		return $resultStr;
	}

	private function receiveVoice($object) {
		$funcFlag = 0;
		$contentStr = "你发送的是语音,媒体ID为:" . $object -> MediaId;
		$resultStr = $this -> transmitText($object, $contentStr, $funcFlag);
		return $resultStr;
	}

	private function receiveVideo($object) {
		$funcFlag = 0;
		$contentStr = "你发送的是视频,媒体ID为:" . $object -> MediaId;
		$resultStr = $this -> transmitText($object, $contentStr, $funcFlag);
		return $resultStr;
	}

	private function receiveShortVideo($object) {
		$funcFlag = 0;
		$contentStr = "你发送的是小视频,媒体ID为:" . $object -> MediaId;
		$resultStr = $this -> transmitText($object, $contentStr, $funcFlag);
		return $resultStr;
	}

	private function receiveLink($object) {
		$funcFlag = 0;
		$contentStr = "你发送的是链接,标题为:" . $object -> Title . ";内容为:" . $object -> Description . ";链接地址为:" . $object -> Url;
		$resultStr = $this -> transmitText($object, $contentStr, $funcFlag);
		return $resultStr;
	}

	private function receiveEvent($object) {
		$contentStr = "";
		switch ($object->Event) {
			case "subscribe" :
				$contentStr = "欢迎关注dropet";
				break;
			case "unsubscribe" :
				$contentStr = "";
				break;
			case "CLICK" :
				switch ($object->EventKey) {
					default :
						$contentStr = "你点击了: " . $object -> EventKey;
						break;
				}
				break;
			default :
				$contentStr = "receive a new event: " . $object -> Event;
				break;
		}
		$resultStr = $this -> transmitText($object, $contentStr);
		return $resultStr;
	}

	private function transmitText($object, $content, $flag = 0) {
		$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>%d</FuncFlag>
</xml>";
		$resultStr = sprintf($textTpl, $object -> FromUserName, $object -> ToUserName, time(), $object -> MsgType, $content, $flag);
		return $resultStr;
	}

}
?>


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值