[关闭]
@52fhy 2014-11-27T13:40:43.000000Z 字数 12018 阅读 527

wechat-php-sdk

微信公众平台php开发包,细化各项接口操作,支持链式调用,欢迎Fork此项目
weixin developer SDK.
项目地址:https://github.com/dodgepudding/wechat-php-sdk
项目blog:http://binsee.github.io/wechat-php-sdk

使用详解

使用前需先打开微信帐号的开发模式,详细步骤请查看微信公众平台接口使用说明:
微信公众平台: http://mp.weixin.qq.com/wiki/
微信企业平台: http://qy.weixin.qq.com/wiki/

微信支付接入文档:
https://mp.weixin.qq.com/cgi-bin/readtemplate?t=business/course2_tmpl&lang=zh_CN

微信多客服:http://dkf.qq.com

目录

wechat.class.php 官方API类库
wechatext.class.php 非官方扩展API
wechatauth.class.php 授权登陆
wechat.js 内嵌JS
errCode.php 全局返回码类
qywechat.class.php 企业号API类库
调用示例


1. wechat.class.php 官方API类库

调用官方API,具有更灵活的消息分类响应方式,支持链式调用操作 ;

主要功能

初始化动作

  1. $options = array(
  2. 'token'=>'tokenaccesskey', //填写你设定的key
  3. 'encodingaeskey'=>'encodingaeskey', //填写加密用的EncodingAESKey
  4. 'appid'=>'wxdk1234567890', //填写高级调用功能的app id, 请在微信开发模式后台查询
  5. 'appsecret'=>'xxxxxxxxxxxxxxxxxxx', //填写高级调用功能的密钥
  6. 'partnerid'=>'88888888', //财付通商户身份标识,支付权限专用,没有可不填
  7. 'partnerkey'=>'', //财付通商户权限密钥Key,支付权限专用
  8. 'paysignkey'=>'' //商户签名密钥Key,支付权限专用
  9. );
  10. $weObj = new Wechat($options); //创建实例对象
  11. //TODO:调用$weObj各实例方法

新增Auth高级权限类方法:

2. wechatext.class.php 非官方扩展API

非官方扩展API,需要配置公众平台账户和密码,能实现对已关注用户的点对点微信,此方式不保证长期有效。
类方法里提及的用户id在接口返回结构里表述为FakeId, 属同一概念, 在下面wechatauth类里则表示为Uin, 用户id对应的微信号必须通过getInfo()方法通过返回数组的Username值获取, 但非关注关系用户资料不能获取.
调用下列方法前必须经过login()方法和checkValid()验证方法才能获得调用权限. 有的账户无法通过登陆可能因为要求提供验证码, 可以手动登陆后把获取到的cookie写进程序存放cookie的文件解决.
程序使用了经过修改的snoopy兼容式HTTP类方法, 在类似BAE/SAE云服务器上可能不能正常运行, 因为云服务的curl方法是经过重写的, 某些header参数如网站来源参数不被支持.

类主要方法:

3. wechatauth.class.php 授权登陆

通过微信二维码登陆微信的API, 能实现第三方网站同步登陆, 首先程序分别通过get_login_code和get_code_image方法获取授权二维码图片, 然后利用微信手机客户端扫描二维码图片后将自动跳出授权页面, 用户点击授权后即可获取对应的用户资料和头像信息. 详细验证步骤请看test3.php例子.

类主要方法:

4. wechat.js 内嵌JS

微信内嵌网页特殊功能js调用:

5. errCode.php 全局返回码类

当调用API接口失败时,可以用此类来换取失败原因的中文说明。

使用方法:

  1. include "errCode.php"; //或 qyerrCode.php
  2. $ret=ErrCode::getErrText(48001); //错误码可以通过公众号类库的公开变量errCode得到
  3. if ($ret)
  4. echo $ret;
  5. else
  6. echo "未找到对应的内容";

6. qywechat.class.php 企业号API类库

调用官方API,具有更灵活的消息分类响应方式,支持链式调用操作 ;

主要功能

初始化动作

  1. $options = array(
  2. 'token'=>'tokenaccesskey', //填写应用接口的Token
  3. 'encodingaeskey'=>'encodingaeskey', //填写加密用的EncodingAESKey
  4. 'appid'=>'wxdk1234567890', //填写高级调用功能的app id
  5. 'appsecret'=>'xxxxxxxxxxxxxxxxxxx', //填写高级调用功能的密钥
  6. 'agentid'=>'1', //应用的id
  7. 'debug'=>false, //调试开关
  8. '_logcallback'=>'logg', //调试输出方法,需要有一个string类型的参数
  9. );
  10. $weObj = new Wechat($options); //创建实例对象
  11. //TODO:调用$weObj各实例方法

被动接口方法:

主动接口方法:

调用示例


官方Wechat调用示例:

  1. //test1.php
  2. include "wechat.class.php";
  3. $options = array(
  4. 'token'=>'tokenaccesskey' //填写你设定的key
  5. );
  6. $weObj = new Wechat($options);
  7. $weObj->valid(); //注意, 应用验证通过后,可将此句注释掉, 但会降低网站安全性
  8. $type = $weObj->getRev()->getRevType();
  9. switch($type) {
  10. case Wechat::MSGTYPE_TEXT:
  11. $weObj->text("hello, I'm wechat")->reply();
  12. exit;
  13. break;
  14. case Wechat::MSGTYPE_EVENT:
  15. break;
  16. case Wechat::MSGTYPE_IMAGE:
  17. break;
  18. default:
  19. $weObj->text("help info")->reply();
  20. }

扩展包Wechatext调用示例:

  1. //test2.php
  2. include "wechatext.class.php";
  3. function logdebug($text){
  4. file_put_contents('./data/log.txt',$text."\n",FILE_APPEND);
  5. };
  6. $options = array(
  7. 'account'=>'demo@domain.com',
  8. 'password'=>'demo',
  9. 'datapath'=>'./data/cookie_',
  10. 'debug'=>true,
  11. 'logcallback'=>'logdebug'
  12. );
  13. $wechat = new Wechatext($options);
  14. if ($wechat->checkValid()) {
  15. // 获取用户信息
  16. $data = $wechat->getInfo('3974255');
  17. var_dump($data);
  18. // 获取最新一条消息
  19. $topmsg = $wechat->getTopMsg();
  20. var_dump($topmsg);
  21. // 主动回复消息
  22. if ($topmsg && $topmsg['has_reply']==0)
  23. $wechat->send($topmsg['fakeid'],'hi '.$topmsg['nick_name'].',rev:'.$topmsg['content']);
  24. }

微信二维码Wechatauth登陆示例:

  1. //test3.php
  2. include "../wechatauth.class.php";
  3. session_start();
  4. $sid = session_id();
  5. $options = array(
  6. 'account'=>$sid,
  7. 'datapath'=>'../data/cookiecode_',
  8. );
  9. $wechat = new Wechatauth($options);
  10. if (isset($_POST['code'])) {
  11. $logincode = $_POST['code'];
  12. $vres = $wechat->set_login_code($logincode)->verify_code();
  13. if ($vres===false) {
  14. $result = array('status'=>0);
  15. } else {
  16. $result = array('status'=>$vres);
  17. if ($vres==200) {
  18. $result['info'] = $wechat->get_login_info();
  19. $result['cookie'] = $wechat->get_login_cookie(true);
  20. }
  21. }
  22. die(json_encode($result));
  23. }
  24. $logincode = $wechat->get_login_code(); //获取授权码
  25. $qrimg = $wechat->get_code_image(); //待输出的二维码图片

HTML部分请看test/test3.php, 主要是定时ajax查询是否已经授权成功

企业号API类库调用示例:

可参考test目录下的qydemo.php

  1. include "wechat.class.php";
  2. $options = array(
  3. 'token'=>'9Ixxxxxxx', //填写应用接口的Token
  4. 'encodingaeskey'=>'d4o9WVg8sxxxxxxxxxxxxxxxxxxxxxx',//填写加密用的EncodingAESKey
  5. 'appid'=>'wxa07979baxxxxxxxx', //填写高级调用功能的appid
  6. );
  7. $weObj = new Wechat($options);
  8. $weObj->valid(); //注意, 企业号与普通公众号不同,必须打开验证,不要注释掉
  9. $type = $weObj->getRev()->getRevType();
  10. switch($type) {
  11. case Wechat::MSGTYPE_TEXT:
  12. $weObj->text("hello, I'm wechat")->reply();
  13. exit;
  14. break;
  15. case Wechat::MSGTYPE_EVENT:
  16. break;
  17. case Wechat::MSGTYPE_IMAGE:
  18. break;
  19. default:
  20. $weObj->text("help info")->reply();
  21. }

License

This is licensed under the GNU LGPL, version 2.1 or later.
For details, see: http://creativecommons.org/licenses/LGPL/2.1/

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注