├── README.md
├── db.sql
├── functions.php
├── weixin-new.php
└── weixin.php
/README.md:
--------------------------------------------------------------------------------
1 | #微信公众平台自助服务
2 |
3 |
4 | 如果你了解数据库,并有自己的服务器,那么这个程序可以帮助你快速的创建你的开发版的微信公众服务信息,免得在微信的后台一步步添加信息。
5 | 目前支持回复的信息类型为:
6 | ***
7 | 1.文本
8 | 2.图文类,仅仅一条
9 | ***
10 |
11 | ###Clone 或下载项目源码。
12 |
13 | 1. 打开 /functions.php 文件, 修改里面的数据库配置信息和欢迎词
14 | 
15 |
16 | 2. 进入微信公众平台,高级功能,开启开发模式,并设置接口配置信息。其中 URL 为 {yoursite}/weixin.php 的实际位置, Token 为上一步设置的 Token 。
17 |
18 | 3. 验证后,把weixin.php删除或者改名字,把weixin-new.php修改为 wexin.php.
19 |
20 | 4. 导入数据库文件db.sql 添加数据,您可以参考里面已经有的数据进行修改。
21 | ***
22 | [1] equls 类型为用户输入的内容和关键词完全匹配
23 | [2] startwith类型为用户输入的内容以此内容开头,并可以自行定义方法到title中。
24 | ***
25 |
26 |
27 | ###向你的微信公众号发送消息并测试吧!
28 |
29 | 大家可以关注我们进行测试:
30 |
31 | 
32 |
33 | 我是白领
34 |
35 |
--------------------------------------------------------------------------------
/db.sql:
--------------------------------------------------------------------------------
1 | # Sequel Pro dump
2 | # Version 2210
3 | # http://code.google.com/p/sequel-pro
4 | #
5 | # Host: 127.0.0.1 (MySQL 5.1.63-lucky9805)
6 | # Database: db
7 | # Generation Time: 2013-05-17 02:21:16 +0000
8 | # ************************************************************
9 |
10 | /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
11 | /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
12 | /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
13 | /*!40101 SET NAMES utf8 */;
14 | /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
15 | /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
16 | /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
17 | /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
18 |
19 |
20 | # Dump of table articles
21 | # ------------------------------------------------------------
22 |
23 | DROP TABLE IF EXISTS `articles`;
24 |
25 | CREATE TABLE `articles` (
26 | `id` int(11) NOT NULL AUTO_INCREMENT,
27 | `title` varchar(100) DEFAULT NULL,
28 | `description` text,
29 | `picurl` varchar(200) DEFAULT NULL,
30 | `url` varchar(200) DEFAULT NULL,
31 | `type` enum('text','news','music') DEFAULT 'text',
32 | `keyword` varchar(200) DEFAULT NULL,
33 | `keyword_type` enum('startwith','equals') DEFAULT 'equals',
34 | PRIMARY KEY (`id`)
35 | ) ENGINE=MyISAM AUTO_INCREMENT=19 DEFAULT CHARSET=utf8;
36 |
37 | LOCK TABLES `articles` WRITE;
38 | /*!40000 ALTER TABLE `articles` DISABLE KEYS */;
39 | INSERT INTO `articles` (`id`,`title`,`description`,`picurl`,`url`,`type`,`keyword`,`keyword_type`)
40 | VALUES
41 | (1,'新闻','欢迎大家访问我们的微信产品\n我是白领\n幸福\n微信公众自住建设\n\n做一个有态度的淘宝客和有态度的微信公众平台系统,是我们致力打造的最好的服务。\n','picture','','news','0000','equals'),
42 | (2,'帮助','您可以输入以下信息来获取我们的帮助:\n1 找美女\n2 找帅哥\n3 找服务\n4 找笑话\n0000 获取新闻\nhelp 获取帮助\n#gxxm 您的姓名 可以用来更新您的姓名','','','text','?','equals'),
43 | (3,'show','您已经更新了姓名','','','text','#gxxm','startwith'),
44 | (4,'show','您可以输入以下信息来获取我们的帮助:\n1 找美女\n2 找帅哥\n3 找服务\n4 找笑话\n0000 获取新闻\nhelp 获取帮助\n#gxxm 您的姓名 可以用来更新您的姓名',NULL,NULL,'text','help','equals');
45 |
46 | /*!40000 ALTER TABLE `articles` ENABLE KEYS */;
47 | UNLOCK TABLES;
48 |
49 |
50 | # Dump of table messages
51 | # ------------------------------------------------------------
52 |
53 | DROP TABLE IF EXISTS `messages`;
54 |
55 | CREATE TABLE `messages` (
56 | `id` int(11) NOT NULL AUTO_INCREMENT,
57 | `uid` varchar(32) DEFAULT NULL,
58 | `content` text,
59 | `time_created` int(11) DEFAULT NULL,
60 | PRIMARY KEY (`id`)
61 | ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
62 |
63 |
64 |
65 | # Dump of table sj
66 | # ------------------------------------------------------------
67 |
68 | DROP TABLE IF EXISTS `sj`;
69 |
70 | CREATE TABLE `sj` (
71 | `id` int(11) NOT NULL AUTO_INCREMENT,
72 | `username` varchar(50) DEFAULT NULL,
73 | `wxid` varchar(50) DEFAULT NULL,
74 | `x` varchar(50) DEFAULT NULL,
75 | `y` varchar(50) DEFAULT NULL,
76 | `status` int(11) unsigned DEFAULT '1',
77 | `sf` int(11) DEFAULT '0',
78 | `dq` int(11) DEFAULT NULL,
79 | `telphone` varchar(13) DEFAULT NULL,
80 | `dz` varchar(200) DEFAULT NULL,
81 | `last_update` int(11) DEFAULT '0',
82 | `cphm` varchar(11) DEFAULT NULL,
83 | `bz` varchar(500) DEFAULT NULL,
84 | `sq` int(11) DEFAULT '0',
85 | `createtime` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
86 | PRIMARY KEY (`id`)
87 | ) ENGINE=MyISAM AUTO_INCREMENT=21 DEFAULT CHARSET=utf8;
88 |
89 |
90 |
91 |
92 |
93 |
94 | /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
95 | /*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
96 | /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
97 | /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
98 | /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
99 | /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
100 | /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
101 |
--------------------------------------------------------------------------------
/functions.php:
--------------------------------------------------------------------------------
1 |
22 |
23 |
24 | %s
25 |
26 |
27 | 0
28 | ";
29 | $conn = mysql_connect($_hostname, $_username, $_password);
30 | mysql_select_db($_dbname, $conn);
31 | mysql_query("set names utf-8");// connect to db
32 |
33 | //自定义的方法都可以写到这个里面来
34 | function show($keyword, $key, $fromUsername, $toUsername)
35 | {
36 |
37 | $textTpl = "
38 |
39 |
40 | %s
41 |
42 |
43 | 0
44 | ";
45 | $time = time();
46 | $msgType = "text";
47 | $contentStr = "您输入的是,这是一个自定义的方法:".$keyword;
48 | $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
49 | echo $resultStr;
50 | mysql_query("insert into messages (uid,content) values('{$fromUsername}','{$keyword}')");
51 | }
52 |
53 | function yourfunction($keyword, $key, $fromUsername, $toUsername)
54 | {
55 | //..........
56 | }
57 | ?>
58 |
--------------------------------------------------------------------------------
/weixin-new.php:
--------------------------------------------------------------------------------
1 | MsgType;
13 |
14 | if($mType =='text')
15 | {
16 | $fromUsername = $postObj->FromUserName;
17 | $toUsername = $postObj->ToUserName;
18 | $keyword = trim($postObj->Content);
19 | $time = time();
20 | if(!empty( $keyword ))
21 | {
22 | /*
23 | 先去数据库中查找是否有匹配的关键词
24 | 再查是否有以此关键词为开头的关键词
25 | */
26 | $sql="select keyword,title,description,type,picurl,url from articles where keyword='{$keyword}' and keyword_type='equals'";
27 | $result=mysql_query($sql);
28 | $num = mysql_num_rows($result);
29 | if($num<1){
30 | $sql="select keyword,title from articles where keyword_type='startwith'";
31 | $result=mysql_query($sql);
32 |
33 | $starwith = false;
34 | $starwith_str = "";
35 | $starwith_func = "";
36 | while ($row=mysql_fetch_row($result))
37 | {
38 | $_keyword = $row[0];
39 | $len = strlen($_keyword);
40 | if( substr($keyword,0,$len) == $_keyword )
41 | {
42 | $starwith_str = $_keyword;
43 | $starwith_func = $row[1];
44 | $starwith = true;
45 | break;
46 | }
47 | }
48 |
49 | if( $starwith )
50 | {
51 | $starwith_func($keyword, $starwith_str, $fromUsername, $toUsername);
52 | }
53 | else
54 | {
55 | $contentStr = $NO_RESPONSE;
56 |
57 | $msgType = "text";
58 | $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
59 | echo $resultStr;
60 | }
61 |
62 | }else
63 | {
64 | //有匹配的数据
65 | $row = mysql_fetch_row($result);
66 | $title = $row[1];
67 | $type = $row[3];
68 | $description = $row[2];
69 | $time = time();
70 | $picurl = $row[4];
71 | $url = $row[5];
72 | $url = str_replace("[[wxid]]", $fromUsername, $url); // 替换微信的ID
73 | if( "text" == $type )
74 | {
75 | $contentStr = $description;
76 | $contentStr = str_replace("[[wxid]]", $fromUsername, $contentStr); // 替换微信的ID
77 | $msgType = "text";
78 | $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
79 | echo $resultStr;
80 | }
81 | if( "news" == $type )
82 | {
83 | $newsTpl = "
84 |
85 |
86 | %s
87 |
88 | 1
89 |
90 | -
91 |
92 |
93 |
94 |
95 |
96 |
97 | 1
98 | ";
99 | $msgType1 = "news";
100 | $resultStr = sprintf($newsTpl, $fromUsername, $toUsername, $time, $msgType1, $title,$description,$picurl,$url);
101 | echo $resultStr;
102 | }
103 | mysql_query("insert into messages (uid,content) values('{$fromUsername}','{$keyword}')");
104 | }
105 | }
106 |
107 |
108 | }elseif($mType =='event')
109 | {
110 | $fromUsername = $postObj->FromUserName;
111 | $toUsername = $postObj->ToUserName;
112 | $location_x = $postObj->Location_X;
113 | $location_y = $postObj->Location_Y;
114 | $scale = $postObj->Scale;
115 | $label = $postObj->Label;
116 | $time = time();
117 |
118 | $contentStr =$WELCOME_MSG;
119 | $msgType = "text";
120 | $sql1="select * from sj where wxid='{$fromUsername}'";
121 | $result1=mysql_query($sql1);
122 | $num1 = mysql_num_rows($result1);
123 | if($num1<1)
124 | mysql_query("insert into sj (wxid) values('{$fromUsername}')");
125 |
126 | $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
127 | echo $resultStr;
128 | }
129 | elseif($mType =='location')
130 | {
131 | $fromUsername = $postObj->FromUserName;
132 | $toUsername = $postObj->ToUserName;
133 | $location_x = $postObj->Location_X;
134 | $location_y = $postObj->Location_Y;
135 | $scale = $postObj->Scale;
136 | $label = $postObj->Label;
137 | $time = time();
138 | $contentStr = "您更新了您的位置信息";
139 | $msgType = "text";
140 | $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
141 | mysql_query("update sj set x='{$location_x}',y='{$location_y}',dz='{$label}',last_update='{$time}' where wxid='{$fromUsername}'");
142 | echo $resultStr;
143 | }
144 |
145 | }else {
146 | echo "";
147 | exit;
148 | }
149 |
150 |
151 | ?>
152 |
--------------------------------------------------------------------------------
/weixin.php:
--------------------------------------------------------------------------------
1 | valid();
12 |
13 | class wechatCallbackapiTest
14 | {
15 | public function valid()
16 | {
17 | $echoStr = $_GET["echostr"];
18 |
19 | //valid signature , option
20 | if($this->checkSignature()){
21 | echo $echoStr;
22 | exit;
23 | }
24 | }
25 |
26 | public function responseMsg()
27 | {
28 | //get post data, May be due to the different environments
29 | $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
30 |
31 | //extract post data
32 | if (!empty($postStr)){
33 |
34 | $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
35 | $fromUsername = $postObj->FromUserName;
36 | $toUsername = $postObj->ToUserName;
37 | $keyword = trim($postObj->Content);
38 | $time = time();
39 | $textTpl = "
40 |
41 |
42 | %s
43 |
44 |
45 | 0
46 | ";
47 | if(!empty( $keyword ))
48 | {
49 | $msgType = "text";
50 | $contentStr = "Welcome to wechat world!";
51 | $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
52 | echo $resultStr;
53 | }else{
54 | echo "Input something...";
55 | }
56 |
57 | }else {
58 | echo "";
59 | exit;
60 | }
61 | }
62 |
63 | private function checkSignature()
64 | {
65 | $signature = $_GET["signature"];
66 | $timestamp = $_GET["timestamp"];
67 | $nonce = $_GET["nonce"];
68 |
69 | $token = TOKEN;
70 | $tmpArr = array($token, $timestamp, $nonce);
71 | sort($tmpArr);
72 | $tmpStr = implode( $tmpArr );
73 | $tmpStr = sha1( $tmpStr );
74 |
75 | if( $tmpStr == $signature ){
76 | return true;
77 | }else{
78 | return false;
79 | }
80 | }
81 | }
82 |
83 | ?>
--------------------------------------------------------------------------------