微信`sdk`

微信

wechat-php-sdk

下载zip包,只需要wechat.class.phpdemo.php

wechat.class.php复制到ThinkPHP/Library/Org/Util/下,然后在控制器中如下引入

1
2
3
import('Org.Util.wechat');//进入
$weObj = new \Wechat($option);//实例化,$option是微信的配置,在demo.php中有demo

官方demo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
include "wechat.class.php";
$options = array(
'token'=>'tokenaccesskey', //填写你设定的key
'encodingaeskey'=>'encodingaeskey' //填写加密用的EncodingAESKey,如接口为明文模式可忽略
);
$weObj = new Wechat($options);
$weObj->valid();//明文或兼容模式可以在接口验证通过后注释此句,但加密模式一定不能注释,否则会验证失败
$type = $weObj->getRev()->getRevType();
switch($type) {
case Wechat::MSGTYPE_TEXT:
$weObj->text("hello, I'm wechat")->reply();
exit;
break;
case Wechat::MSGTYPE_EVENT:
break;
case Wechat::MSGTYPE_IMAGE:
break;
default:
$weObj->text("help info")->reply();

自己改进后demo

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
<?php
namespace Home\Controller;
use Think\Controller;
import('Org.Util.Wechat');
class EventController extends Controller{
function index(){
$options = array(
'appid' => 'wx51c8eacd17a190c0',//微信appid
'appsecret' => 'f30185d7c8ff809ab44597d7c8ad8457',//微信appsecret
'token' => '81965224d145c658896fef654072a8a1', //填写你设定的key
'encodingaeskey' => 'cdd96aa3071266d088ad44411066df87cd8bf993ada' //填写加密用的EncodingAESKey,如接口为明文模式可忽略
);
$weObj = new \Wechat($options);
$weObj->valid();//明文或兼容模式可以在接口验证通过后注释此句,但加密模式一定不能注释,否则会验证失败
$type = $weObj->getRev()->getRevType();
$openId = $weObj->getRevFrom();//获取消息发送者
$content = $weObj->getRevContent();//获取发送内容
$event = $weObj->getRevEvent();//事件类型:$event['event'],类型关键字:$event['key']
//图文数据组织
$newsCourse = array(
'0' => array(
'Title' => '最好的健身课',
'Description' => '忍不住地想要上课啦!',
'Url' => 'http://sass-wx.happyfit.studio/course/index?openId=' . $openId, 'PicUrl' => 'http://o7atl50ri.bkt.clouddn.com/1467773885.jpg?&e=1467777485&token=RYyqdPimpYxIUJl4KVcJ6APplN90d5CEpU1kZ-a6:KZTy43_pWbY0wC3WUopht0SzZms='
));
$newsNotice = array(
'0' => array(
'Title' => '最新的公告',
'Description' => '又有新公告了,进来看看吧,惊喜等着你!',
'Url' => 'http://sass-wx.happyfit.studio/course/index?openId=' . $openId, 'PicUrl' => 'http://o7atl50ri.bkt.clouddn.com/1467773885.jpg?&e=1467777485&token=RYyqdPimpYxIUJl4KVcJ6APplN90d5CEpU1kZ-a6:KZTy43_pWbY0wC3WUopht0SzZms='
));
$newsAbout = array(
'0' => array(
'Title' => '最棒的场馆',
'Description' => '这个场馆绝对适合你!',
'Url' => 'http://sass-wx.happyfit.studio/course/index?openId=' . $openId, 'PicUrl' => 'http://o7atl50ri.bkt.clouddn.com/1467773885.jpg?&e=1467777485&token=RYyqdPimpYxIUJl4KVcJ6APplN90d5CEpU1kZ-a6:KZTy43_pWbY0wC3WUopht0SzZms='
));
switch ($type) {
case \Wechat::EVENT_SUBSCRIBE:
//用户关注微信号时 TODO 没有记录,添加用户user_social_info
break;
case \Wechat::MSGTYPE_TEXT:
//处理用户输入
if ($content == '我的课程') $weObj->news($newsCourse)->reply();
$weObj->text("欢迎使用精微内容服务!")->reply();
break;
case \Wechat::MSGTYPE_EVENT:
//处理用户事件
if ($event['event'] == 'CLICK') {
if ($event['key'] == 'course') {
$weObj->news($newsCourse)->reply();
}
if ($event['key'] == 'notice') {
$weObj->news($newsNotice)->reply();
}
if ($event['key'] == 'about') {
$weObj->news($newsAbout)->reply();
}
}
break;
};
}
}
坚持原创技术分享,您的支持将鼓励我继续创作!