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

来源:互联网 发布:房屋效果图设计软件 编辑:程序博客网 时间:2024/05/19 16:48

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

<?phpdefine("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;}}?>


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 立邦乳胶漆墙面脏了怎么办 橱柜门黑色边颜色花了怎么办 地板上有真实漆怎么办 吸了泡泡球的气怎么办 办公室上班坐着太累怎么办 金雀盆景生虫怎么办 租的房子墙脏怎么办 租住个厕所对着卧室怎么办 农村的房子没有房产证怎么办 墙面漆颜色深了怎么办 浴室门对卧室门怎么办 卧室40多个平方太大怎么办 卧室门洞太窄了怎么办 刷漆的墙面脏了怎么办 卧室颜色刷重了怎么办 感觉房子买小了怎么办 淋浴房一边是窗怎么办 1岁宝宝让狗咬了怎么办 2岁宝宝让狗咬了怎么办 带4个小孩只买两张儿童票怎么办 墙面补漆颜色有色差怎么办 墙壁刷的乳胶漆颜色不满意怎么办 门被水泡的鼓了怎么办 定制衣柜做大了怎么办 找平的地面高了怎么办 卫生间推拉门锁坏了怎么办 阳台推拉门框安装斜了怎么办? 电子门钥匙坏了怎么办 门的保险锁坏了怎么办 小区楼宇门坏了怎么办 门的锁坏了怎么办 汽车门把锁坏了怎么办 厨柜门上的板板掉了怎么办 衣柜门滑轮坏了怎么办 厨房推拉门推动时有声音是怎么办? 塑料推拉门声音大怎么办不好推 推拉门锁扣坏了怎么办 衣柜移门拉不动怎么办?如何保养 擦黑色桌面有层白灰怎么办 宝宝睡觉不盖被子怎么办 孩子盖被子就哭怎么办