发表时间: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,
将视频设置为Android手机开机动画的教程
PHP中的(++i)前缀自增 和 (i++)后缀自增
最简单的asp登陆界面代码 asp登陆界面源代码详细介绍
常用dos命令及语法
PHP中include和require区别之我见
ACE健康下载v1.1.5 最新版
19.61MB |生活服务
星座进货宝app最新版(改名四季星座网)下载v3.8.15 安卓手机版
41.79MB |商务办公
苏宁推客官方版下载v9.8.40 安卓版
57.19MB |生活服务
德业储能app下载v2.6.2 安卓版
71.53MB |商务办公
苏宁微店卖家版app(苏宁推客)下载v9.8.40 安卓最新版
57.19MB |生活服务
机友邦工程机械网官方版app下载v4.0.4 安卓版
88.56MB |系统工具
苏宁微店客户端(改名苏宁推客)下载v9.8.40 安卓版
57.19MB |生活服务
优腿商家端app下载v1.23.5 安卓版
34.13MB |系统工具
2014-09-05
2022-03-20
2022-03-21
2022-03-24
2014-09-05
2014-09-05
2015-07-05
2014-09-05
2014-09-05
2022-03-21