呵呵,大家好。这个微信php类库来自于网络上多人智慧的结晶,特别是被动响应部分基本上都是来自于:dodgepudding。在此向那些牛人们致敬。 说明一下,我是个菜鸟,所以在代码上特别的凌乱,希望有热心人士指点。主动发送部分可能因为腾讯的调整失效,发现问题请联系我:ligboy#gmail.com 欢迎大家fork或者push
被动响应端没有什么好讲的,依旧是没有解决语音消息获取问题。 主动发送方面,群发的效率还是比较高的,测试,一次群发80条消息,耗时9950.0999ms,还是很不错的。主动发送单挑消息也没什么好讲的,跟大家的都差不多,只是这里要说如果大家要用,尽量建立一个消息队列,然后批量发送,这样的效率比较高,后面更新加入针对消息队列的批量发送,不仅仅是现在的群发同一条小时,另外谁有兴趣写一下可以提交一下。
- 解决用户关注后获取用户fakeid问题,之前只可以获取openid,如何才可以让两者对应起来。
require("./Wechat.class.php");
$options = array(
'etoken'=>'ddfdfasd', //微信公共平台设置的接口token
'account'=>'[email protected]', //微信公共平台账号,您不需要主动发送消息不需要设置
'password'=>'********', //微信公共平台账号密码,您不需要主动发送消息不需要设置
);
$wechatObj = new Wechat($options); //创建Wechat的实例并初始化参数
$wechatObj->setCookiefilepath("./app/Runtime/"); //设置cookie文件保存目录
$wechatObj->setWebtokenStoragefile("./app/Runtime/webtoken.txt"); //设置webtoken的保存路径(包括文件名),您不需要主动发送消息不需要设置
//被动响应实例
$wechatObj->valid(); //验证请求来源是否合法,在通过平台验证后可以去掉,但是不安全啊。
$msgtype = $weObj->getRev()->getRevType();
switch(msg$type) {
case Wechat::MSGTYPE_TEXT:
$wechatObj->text("你好我是微信小机器人")->reply();
exit;
break;
case Wechat::MSGTYPE_EVENT:
$revEvent = array();
$revEvent = $this->wechatObj->getRevEvent();
switch ($revEvent['event']) {
//关注订阅事件
case "subscribe":
$wechatObj->text("你好我是微信小机器人")->reply();
break;
//取消关注订阅事件
case "unsubscribe":
//做一些删除用户记录之类的事情
break;
}
break;
case Wechat::MSGTYPE_IMAGE:
break;
case Wechat::MSGTYPE_VOICE:
break;
case Wechat::MSGTYPE_MUSIC:
break;
case Wechat::MSGTYPE_LOCATION:
break;
case Wechat::MSGTYPE_LINK:
break;
default:
$wechatObj->text($wechatObj->wechatObj)->reply();
}
//主动发送消息示例
//群发消息
$fakeids = array("823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881","823058881");
//接收返回结果数组
$batresult = $wechatObj->batSend($fakeids,"这是一种问候啊!\n下个10分钟再见。");
//单条消息发送
$singleresult = $wechatObj->send("823058881", "这是一种问候啊!");