|
10 years ago | |
---|---|---|
Thinkphp | 10 years ago | |
test | 11 years ago | |
.buildpath | 12 years ago | |
.gitignore | 11 years ago | |
README.md | 10 years ago | |
demo.php | 12 years ago | |
snoopy.class.php | 10 years ago | |
wechat.class.php | 10 years ago | |
wechat.js | 10 years ago | |
wechatauth.class.php | 10 years ago | |
wechatext.class.php | 10 years ago |
微信公众平台php开发包,细化各项接口操作,支持链式调用,欢迎Fork此项目
weixin developer SDK.
使用前需先打开微信帐号的开发模式,详细步骤请查看微信公众平台接口使用说明:
http://mp.weixin.qq.com/wiki/
微信支付接入文档: https://mp.weixin.qq.com/cgi-bin/readtemplate?t=business/course2_tmpl&lang=zh_CN
$options = array(
'token'=>'tokenaccesskey', //填写你设定的key
'appid'=>'wxdk1234567890', //填写高级调用功能的app id, 请在微信开发模式后台查询
'appsecret'=>'xxxxxxxxxxxxxxxxxxx', //填写高级调用功能的密钥
'partnerid'=>'88888888', //财付通商户身份标识,支付权限专用,没有可不填
'partnerkey'=>'', //财付通商户权限密钥Key,支付权限专用
'paysignkey'=>'' //商户签名密钥Key,支付权限专用
);
$weObj = new Wechat($options); //创建实例对象
//TODO:调用$weObj各实例方法
新增Auth高级权限类方法:
wechatext.class.php
非官方扩展API,需要配置公众平台账户和密码,能实现对已关注用户的点对点微信,此方式不保证长期有效。
类方法里提及的用户id在接口返回结构里表述为FakeId, 属同一概念, 在下面wechatauth类里则表示为Uin, 用户id对应的微信号必须通过getInfo()方法通过返回数组的Username值获取, 但非关注关系用户资料不能获取.
调用下列方法前必须经过login()方法和checkValid()验证方法才能获得调用权限. 有的账户无法通过登陆可能因为要求提供验证码, 可以手动登陆后把获取到的cookie写进程序存放cookie的文件解决.
程序使用了经过修改的snoopy兼容式HTTP类方法, 在类似BAE/SAE云服务器上可能不能正常运行, 因为云服务的curl方法是经过重写的, 某些header参数如网站来源参数不被支持.
类主要方法:
wechatauth.class.php
通过微信二维码登陆微信的API, 能实现第三方网站同步登陆, 首先程序分别通过get_login_code和get_code_image方法获取授权二维码图片, 然后利用微信手机客户端扫描二维码图片后将自动跳出授权页面, 用户点击授权后即可获取对应的用户资料和头像信息. 详细验证步骤请看test3.php例子.
类主要方法:
wechat.js 微信内嵌网页特殊功能js调用:
javascript
var dataForWeixin={
appId:"",
MsgImg:"消息图片路径",
TLImg:"时间线图路径",
url:"分享url路径",
title:"标题",
desc:"描述",
fakeid:"",
callback:function(){}
};
//test1.php
include "wechat.class.php";
$options = array(
'token'=>'tokenaccesskey' //填写你设定的key
);
$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();
}
//test2.php
include "wechatext.class.php";
function logdebug($text){
file_put_contents('./data/log.txt',$text."\n",FILE_APPEND);
};
$options = array(
'account'=>'demo@domain.com',
'password'=>'demo',
'datapath'=>'./data/cookie_',
'debug'=>true,
'logcallback'=>'logdebug'
);
$wechat = new Wechatext($options);
if ($wechat->checkValid()) {
// 获取用户信息
$data = $wechat->getInfo('3974255');
var_dump($data);
// 获取最新一条消息
$topmsg = $wechat->getTopMsg();
var_dump($topmsg);
// 主动回复消息
if ($topmsg && $topmsg['has_reply']==0)
$wechat->send($topmsg['fakeid'],'hi '.$topmsg['nick_name'].',rev:'.$topmsg['content']);
}
//test3.php
include "../wechatauth.class.php";
session_start();
$sid = session_id();
$options = array(
'account'=>$sid,
'datapath'=>'../data/cookiecode_',
);
$wechat = new Wechatauth($options);
if (isset($_POST['code'])) {
$logincode = $_POST['code'];
$vres = $wechat->set_login_code($logincode)->verify_code();
if ($vres===false) {
$result = array('status'=>0);
} else {
$result = array('status'=>$vres);
if ($vres==200) {
$result['info'] = $wechat->get_login_info();
$result['cookie'] = $wechat->get_login_cookie(true);
}
}
die(json_encode($result));
}
$logincode = $wechat->get_login_code(); //获取授权码
$qrimg = $wechat->get_code_image(); //待输出的二维码图片
HTML部分请看test/test3.php, 主要是定时ajax查询是否已经授权成功
This is licensed under the GNU LGPL, version 2.1 or later.
For details, see: http://creativecommons.org/licenses/LGPL/2.1/