Giter Club home page Giter Club logo

wechatminiprogram's Introduction

微信小程序 SDK

用于 PHP 方便调用微信小程序的 服务端接口

Latest Stable Version Total Downloads Latest Unstable Version License

接入要求

"php": ">=5.4"

接入方式

1、composer 安装

composer require luckyhjh/wechat-mini-program

2、新建文件

新建类继承 WechatMiniProgram,并实现相关接口,token 可保存在 redis 或 mysql 等。

例如 Laravel/Lumen,可新建文件 app/Libs/WechatMiniProgram.php:

namespace App\Libs;

use Illuminate\Support\Facades\Cache;
use WechatMiniProgram\Api\AccessToken;
use WechatMiniProgram\ApiException;

class WechatMiniProgram extends \WechatMiniProgram\WechatMiniProgram
{
    public function __construct($appid = '', $secret = '')
    {
        empty($appid) and $appid = env('WX_APPID');
        empty($secret) and $secret = env('WX_SECRET');
        parent::__construct($appid, $secret);
    }

    /**
     * 读取token
     * @return string
     */
    public function getApiToken()
    {
        $cache_name = 'wx_token_' . $this->getAppid();
        $access_token = Cache::get($cache_name);
        if ($access_token) {
            return $access_token;
        }

        $AccessToken = new AccessToken($this);
        try {
            $Token = $AccessToken->getAccessToken();
            $access_token = $Token->access_token;
            $this->setApiToken($access_token, $Token->expires_in);
            return $access_token;

        } catch (ApiException $apiException) {
            return '';
        }
    }

    /**
     * 保存token
     * @param string $access_token
     * @param int $expire
     * @return bool
     */
    public function setApiToken($access_token, $expire = 7200)
    {
        $cache_name = 'wx_token_' . $this->getAppid();
        return Cache::set($cache_name, $access_token, $expire);
    }
}

例如 ThinkPHP,可新建文件 extend/WechatMiniProgram.php:

use WechatMiniProgram\Api\AccessToken;
use WechatMiniProgram\ApiException;

class WechatMiniProgram extends \WechatMiniProgram\WechatMiniProgram
{
    public function __construct($appid = '', $secret = '')
    {
        empty($appid) and $appid = env('WX_APPID');
        empty($secret) and $secret = env('WX_SECRET');
        parent::__construct($appid, $secret);
    }

    /**
     * 读取token
     * @return string
     */
    public function getApiToken()
    {
        $cache_name = 'wx_token_' . $this->getAppid();
        $access_token = cache($cache_name);
        if ($access_token) {
            return $access_token;
        }

        $AccessToken = new AccessToken($this);
        try {
            $Token = $AccessToken->getAccessToken();
            $access_token = $Token->access_token;
            $this->setApiToken($access_token, $Token->expires_in);
            return $access_token;

        } catch (ApiException $apiException) {
            return '';
        }
    }

    /**
     * 保存token
     * @param string $access_token
     * @param int $expire
     * @return bool
     */
    public function setApiToken($access_token, $expire = 7200)
    {
        $cache_name = 'wx_token_' . $this->getAppid();
        return cache($cache_name, $access_token, $expire);
    }
}

3、业务调用

业务里根据需要调用

$mp = new WechatMiniProgram();
$Auth = new Auth($mp);
$User = new \WechatMiniProgram\Api\User($mp);//如果要调用其它接口,可复用$mp
try {
    $session = $Auth->code2session($code);
    $openid = $session->openid;
} catch (ApiException $apiException) {
}

支持接口

License

MIT

wechatminiprogram's People

Contributors

luckyhjh avatar

Stargazers

Yangliuping avatar  avatar  avatar

Watchers

James Cloos avatar  avatar Adnresen avatar

Forkers

luoam ylpsghr

wechatminiprogram's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.