发表时间:2015-05-27来源:网络
好久就想实现这个功能了,可是一直没心情,好在收到网易实习的offer,于是心情大好,实现了这个功能来和大家共享。大家可以先看效果吧:只要关注微信公众帐号say_magic,

然后打开网址http://www.saymagic.cn/weixin/wall.php,在公众号里回复:上墙+您要说的话,您就会发现您说的话会同步到上面的网址上。
整个流程大概是这样:
公众号的后台接收到消息并将消息存入数据库,而前台呢,则使用js的setTimeout函数进行循环的使用ajax向后台get数据来获取数据库的最新数据,当明白整个原理后,就显得很简单,接下来看一下主要的代码:
wall.php(微信墙页面 )
微信墙 query($wxQuery);while ($wxRow=mysql_fetch_row($wxResult)) { $lastID or $lastID = $wxRow[0];//0代表数据库中的id,这个要和你自己数据库相对应 $content = $wxRow[4];//4也是一样的 echo '<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.3.min.js"></script><script>var lastID = ;function getMessages() { $.ajax({ url: "message.php?lastID=" + lastID + "&v=" + (new Date()/1), dataType: "json", error: function(){ alert('Error loading JSON document'); }, success: function(data){//如果调用php成功 $.each(data,function(i,n){ message = '',$content,"/n";}$lastID = (int)$lastID;?>' + n + ''; $(message).prependTo('#msgBox').hide().slideDown('slow'); lastID = i; }); } }); window.setTimeout(getMessages, 5000);}getMessages();</script>
db_host = $db_host; $this->db_user = $db_user; $this->db_pwd = $db_pwd; $this->db_database = $db_database; $this->conn = $conn; $this->coding = $coding; $this->connect(); } /*数据库连接*/ public function connect() { if ($this->conn == "pconn") { //永久链接 $this->conn = mysql_pconnect($this->db_host, $this->db_user, $this->db_pwd); } else { //即使链接 $this->conn = mysql_connect($this->db_host, $this->db_user, $this->db_pwd); } if (!mysql_select_db($this->db_database, $this->conn)) { if ($this->show_error) { $this->show_error("数据库不可用:", $this->db_database); } } //mysql_query("SET NAMES $this->coding"); } /*数据库执行语句,可执行查询添加修改删除等任何sql语句*/ public function query($sql) { if ($sql == "") { $this->show_error("SQL语句错误:", "SQL查询语句为空"); } $this->sql = $sql; $result = mysql_query($this->sql, $this->conn); if (!$result) { //调试中使用,sql语句出错时会自动打印出来 if ($this->show_error) { $this->show_error("错误SQL语句:", $this->sql); } } else { $this->result = $result; } return $this->result; } }?>
不断从数据库获取最新数据的文件message.php.
1000) { die('possible deep recursion attack'); } foreach ($array as $key => $value) { if (is_array($value)) { arrayRecursive($array[$key], $function, $apply_to_keys_also); } else { $array[$key] = $function($value); } if ($apply_to_keys_also && is_string($key)) { $new_key = $function($key); if ($new_key != $key) { $array[$new_key] = $array[$key]; unset($array[$key]); } } } $recursive_counter--;}function JSON($array) { arrayRecursive($array, 'urlencode', true); $json = json_encode($array); return urldecode($json);}$lastID = (int) $_GET['lastID'];include_once("sql.php");$backValue=array();$wxQuery = "SELECT * FROM wx_note WHERE id > ".$lastID." ORDER BY id LIMIT 3";$wxResult = $mysql->query($wxQuery);while ($wxRow=mysql_fetch_row($wxResult)) { $recordID = $wxRow[0]; $content = $wxRow[4]; //$xml=$content; $backValue[$recordID ] = $content;}echo JSON($backValue);?>
当然,这里说的只是后端的部分,在处理微信消息上,你还需要把收到的消息存入数据库,这应该没什么难度,就不放代码了。这里一定要记得和刚才操作的方法中的数据格式相对应,这是最容易出错的了。好的,如果有什么疑问的话,欢迎留言。
CI框架连接数据库配置操作以及多数据库操作
asp 简单读取数据表并列出来 ASP如何快速从数据库读取大量数据
C语言关键字及其解释介绍 C语言32个关键字详解
C语言中sizeof是什么意思 c语言里sizeof怎样用法详解
PHP中的魔术方法 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep,
PHP中的(++i)前缀自增 和 (i++)后缀自增
将视频设置为Android手机开机动画的教程
最简单的asp登陆界面代码 asp登陆界面源代码详细介绍
常用dos命令及语法
PHP中include和require区别之我见
甜蜜守护手表版客户端下载v1.3.4 安卓版
62.66MB |生活服务
视力筛查软件(改名智能筛查)下载vv3.5.62 安卓版
57.63MB |生活服务
云高高尔夫官方版下载v9.16 安卓版
74.52MB |生活服务
京东优悠保软件下载v3.9.70 安卓版
53.16MB |生活服务
天猫校园超市下载v4.5.4 安卓版
116.06MB |生活服务
驾考家园2025新版本下载v6.83 安卓版
166.97M |学习教育
e网充手机版下载v1.2.6
40.63MB |生活服务
专利代理师考试聚题库app下载v2.0.7 安卓版
77.19MB |学习教育
2014-09-05
2022-03-20
2022-03-21
2022-03-24
2014-09-05
2014-09-05
2015-07-05
2014-09-05
2022-03-21
2014-09-05