/weixin-wall
├── wechat.php 連接微信公眾平臺 將消息存入數據庫
├── wall.php 微信墻
└── message.php 新增消息XML
微信公眾平臺與服務器通信 將用戶發送的消息存入MySQL數據庫
12345678910111213141516171819202122232425262728293031323334353637383940414243 | <?php/* 微信公眾平臺API*/ error_reporting(0);// 關閉錯誤報告 $db = mysqli_connect('localhost', '用戶名', '密碼', '數據庫名') or exit('Unable to connect to MySQL.');mysqli_query($db, 'SET NAMES UTF8');// 連接數據庫 if (!$postStr=$GLOBALS['HTTP_RAW_POST_DATA']) exit;// 無POST內容 退出 $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); //解析XML $msgType = $postObj->MsgType; // 獲取消息類型 if ($msgType!='text') exit;// 非文本消息 退出 $openID = antiInjection($postObj->FromUserName); // 獲取發送者openID$content = antiInjection($postObj->Content); // 獲取文本消息內容 record($openID, $content); // 將消息寫入MySQL數據庫 function record($openID, $content) { $recordQuery = "INSERT INTO message (openID, content) VALUES ('$openID', '$content');"; mysqli_query($GLOBALS['db'], $recordQuery);}function antiInjection($value) { // 反SQL注入 global $db; if (get_magic_quotes_gpc()) $value = stripslashes($value); $value = trim($value); $value = mysqli_real_escape_string($db, $value); return $value;}?> |
微信墻主體 訪問時加載已經存在的消息
當接收到新消息時,無需刷新,通過AJAX jQuery即可實現消息滾動。
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 | <?php$db = mysqli_connect('localhost', '用戶名', '密碼', '數據庫名') or exit('Unable to connect to MySQL.');mysqli_query($db, 'SET NAMES UTF8');?><!doctype html><html><head><meta charset="utf-8" /><title>微信墻</title><style>#msgBox div { padding: 19px; margin-bottom: 20px; background: #f5f5f5; border: 1px solid #e3e3e3; -webkit-border-radius: 4px; -moz-border-radius: 4px; border-radius: 4px; -webkit-box-shadow: inset 0 1px 1px rgba(0,0,0,0.05); -moz-box-shadow: inset 0 1px 1px rgba(0,0,0,0.05); box-shadow: inset 0 1px 1px rgba(0,0,0,0.05);}</style></head><body><div id="msgBox"><?php$wxQuery = 'SELECT * FROM message ORDER BY recordID DESC LIMIT 20';$wxResult = mysqli_query($db, $wxQuery);while ($wxRow=mysqli_fetch_assoc($wxResult)) { $lastID or $lastID = $wxRow['recordID']; $content = $wxRow['content']; echo '<div>',$content,"</div>/n";}$lastID = (int)$lastID;?></div> <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.3.min.js"></script><script>var lastID = <?php echo $lastID; ?>;function getMessages() { $.ajax({ url : '/message.php?lastID=' + lastID + '&v=' + (new Date()/1), success : function(xml){ $(xml).find('message').each(function() { if ($(this).attr('recordID') > lastID) { var message = $(this).text(); message = '<div>' + message + '</div>'; $(message).prependTo('#msgBox').hide().slideDown('slow'); lastID = $(this).attr('recordID'); } }); } }); window.setTimeout(getMessages, 5000);}getMessages();</script></body></html> |
頁面加載完畢后再接收到的新消息 通過XML傳送
123456789101112131415161718 | <?php$db = mysqli_connect('localhost', '用戶名', '密碼', '數據庫名') or exit('Unable to connect to MySQL.');mysqli_query($db, 'SET NAMES UTF8');header('Content-type: text/xml');$lastID = (int) $_GET['lastID'];echo '<?xml version="1.0" encoding="UTF-8"?>',"/n";?><feitsui><?php$wxQuery = "SELECT * FROM message WHERE recordID > $lastID ORDER BY recordID LIMIT 20";$wxResult = mysqli_query($db, $wxQuery);while ($wxRow=mysqli_fetch_assoc($wxResult)) { $recordID = $wxRow['recordID']; $content = $wxRow['content']; echo '<message recordID="',$recordID,'">',$content,"</message>/n";}?></feitsui> |
新聞熱點
疑難解答