得到微信消息并存入数据库
来源:互联网 发布:房屋效果图设计软件 编辑:程序博客网 时间: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
- 得到微信消息并存入数据库
- 上传excel并存入数据库
- 从Excel读取数据并存入数据库
- 如何读取csv文件并存入数据库?
- MVC4.0 上传Excel并存入数据库
- php读取xml文件,并存入数据库
- 爬取电影天堂并存入数据库
- jsp上传文件,并存入数据库
- python 分析 nginx 日志 ip 并存入sqlite 数据库
- 字符串转化为日期字段并存入数据库
- 使用python访问文件,并存入数据库中
- java使用POI 读取excel2010文件 并存入oracle数据库
- java切割图片并存入sqlite数据库中
- 读取特定文件夹下内容并存入数据库
- php读取服务器(CentOS 6.5)状态并存入数据库
- C#实现文件与二进制互转并存入数据库
- 【详解】从b站抓取信息并存入数据库
- android之访问网络获取网页数据并存入数据库
- 走近麻省理工学院的神秘科技:大数据分析自动化
- org.gradle.api.publication.maven.internal.DefaultMavenFactory错误
- 解决邮件javax.mail发送邮件的问题unable to find valid certification path to requested target
- Android ViewPager的FragmentPagerAdapter中Fragment不重新加载数据,并且使用的是其他缓存的Framgnet的问题
- SAP系统资产盘盈盘亏的处理
- 得到微信消息并存入数据库
- hdu 2579 Dating with girls(2) (BFS)
- DNS 原理入门
- flash builder eclipse不在本地使用
- SAP反记帐和红字冲销
- WebViewClient与WebChromeClient的区别
- Android中shape的使用
- 非文本内容的 MIME 输出(总结)
- ios NSDate获取星期几