Develope WeChat Official Account's Hello Message

Develope WeChat Official Account's Hello Message

Exchange blogroll: laker.me
Github:https://github.com/younglaker

Apply test account

Apply for Wechat testing account:

https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421137524

Application

Connect WeChat server with

Enter your testing account, and set your server address and the director wx for WeChat server. Set a token name what ever you like:

token name

Enter your Baota Panel, new a file wx in application/public/

Baota Panel

Add a new file index.php in wx

Function:

  • Connect to WeChat server
  • When someone subscribes, reply a hello message
  • When get user text messge, reply what he sent
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
/*
* Connect to WeChat server
*/
// Get signature nonce token timestamp echostr
$nonce = $_GET['nonce'];
$token = 'YOUR_TOKEN';
$timestamp = $_GET['timestamp'];
$echostr = $_GET['echostr'];
$signature = $_GET['signature'];
// Set it as array, and sort
$array = array();
$array = array($nonce, $timestamp, $token);
sort($array);
// Join them as a string, use sha1 to encrypt, and check with signature
$str = sha1( implode( $array ) );
//The first time weixin api
if( $str == $signature && $echostr ){
echo $echostr;
exit;
}
else{
/*
* When someone subscribes, reply a hello message
*/
// 1. Get the posted xml data from Weixin
$postArr = $GLOBALS['HTTP_RAW_POST_DATA'];
// 2. Deal with message type and set reply type and content
$postObj = simplexml_load_string( $postArr );
// Check if the data is subscription event
if( strtolower( $postObj->MsgType) == 'event'){
// If it'is subscribe event
if( strtolower($postObj->Event == 'subscribe') ){
// Reply text message
$toUser = $postObj->FromUserName;
$fromUser = $postObj->ToUserName;
$time = time();
$msgType = 'text';
$content = 'Hello, welcome~ '.$postObj->FromUserName.'-'.$postObj->ToUserName;;
$template = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
$info = sprintf($template, $toUser, $fromUser, $time, $msgType, $content);
echo $info;
}
}
/*
* When get user text messge, reply what he sent
*/
// Check if the data is text
if( strtolower( $postObj->MsgType) == 'text'){
// Get text message
$content = $postObj->Content;
// Reply text message
$toUser = $postObj->FromUserName;
$fromUser = $postObj->ToUserName;
$time = time();
$msgType = 'text';
$content = 'The content you sent is '.$content;
$template = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
$info = sprintf($template, $toUser, $fromUser, $time, $msgType, $content);
echo $info;
}
}

In Official Accounts,when you subscibe, it will say hello to you . When you send a text, it will send what you sent:

Done


Develope WeChat Official Account's Hello Message

本文原创自http://laker.me/blog,转载请注明出处,欢迎交换友链

如果本文对您有帮助,微信扫一扫,请我吃个鸡腿吧

评论组件不稳定,有事请联系V信 lakerHQ (请备注来自博客)