php微信公众平台API接口(thinkphp5)

来源:互联网 发布:scarsong知乎 编辑:程序博客网 时间:2024/06/10 20:47

整合了微信公众号的接口。包括但不限于:获取access_token,自定义菜单,消息管理,用户管理等。持续更新。。。下一步增加微信支付和支付宝支付接口

更多详细信息和更新可访问项目github地址,请留下个Star: https://github.com/guorongjie/sdkSet

接口类使用了命名空间,可直接引入并实例化。基于thinkphp5测试。

接口类放在extend/Wechat 目录下。测试项目附有示例:application/wechat/controller/Index.php
这里写图片描述

<?phpnamespace app\wechat\controller;use think\Controller;use Wechat\WechatMenu;use Wechat\WechatOauth;use Wechat\WechatUser;class Index extends Controller{    public function index(){        return $this->fetch();    }    /**     * 获取公众号access_token     */    public function getAccessToken()    {        $wechatOauth = new WechatOauth();        $accessToken = $wechatOauth->get_access_token();        echo $accessToken;    }    /**     * 生成公众号菜单     */    public function createMenu()    {        $data = //测试数据            '{            "button":[{                "type":"click",                "name":"今日歌曲2",               "key":"V1001_TODAY_MUSIC"             },            {           "name":"菜单",           "sub_button":[           {                   "type":"view",               "name":"搜索",               "url":"http://www.soso.com/"            },            {               "type":"click",               "name":"赞一下我们",               "key":"V1001_GOOD"            }]             }]             }';        $wechatMenu = new WechatMenu();        $result = $wechatMenu->createMenu($data);        echo $result;    }    /**     * 批量获取关注粉丝列表     */    public function getUserLists()    {        $wechatUser = new  WechatUser();        $result = $wechatUser->getUserList();        dump($result);    }    public function location()    {        $wechatUser = new  WechatUser();        $result = $wechatUser->location('113.323916', '23.089716');        $result = json_decode($result,true);        dump($result['regeocode']['addressComponent']['city']);    }}

接口类注释详细:

class Wechat {    /**     * @var string|WechatReceive     * 这里作为微信公众号的消息入口(包括绑定入口)。用户在公众号发送的消息,微信服务器会推送到这里(index).这个类可迁到开发的请求控制器中     */    private $WechatReceive = '';//消息实例    private $WechatOauth = '';//授权实例    private $FromUserName = '';//消息发送者    private $ToUserName = '';  //消息接受者    private $MsgType = '';  //接收消息的类型    private $CreateTime = '';  //接收消息的时间    private $Keyword = '';  //接收消息的值
原创粉丝点击