PHP code example of suntianxiang / cn-oauth

1. Go to this page and download the library: Download suntianxiang/cn-oauth library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

suntianxiang / cn-oauth example snippets


    # 微信
    $wechat = new \CnOAuth\Provider\WechatOfficialAccount([
        'clientId' => 'you client id',
        'clientSecret' => 'you client secret',
        'redirectUri' => 'redirect uri'
    ]);

    $url = $wechat->getAuthorizationUrl([
        'scope' => $wechat->getDefaultScopes(),
        'state' => 'state'
    ]);

    header('Location: '.$url);

    # 微信
    $wechat = new \CnOAuth\Provider\WechatOfficialAccount([
        'clientId' => 'you client id',
        'clientSecret' => 'you client secret',
        'redirectUri' => 'redirect uri'
    ]);
    $grant = $wechat->getGrant('authorization');

    if ($grant->getCode()) {
        $access_token = $wechat->getAccessToken($grant);

        $owner = $wechat->getResourceOwner($access_token);

        print_r($owner->toArray());
    } else {
        // 用户取消授权
    }

$alipay = new \CnOAuth\Provider\Alipay([
    'gatewayUrl' => 'https://openapi.alipay.com/gateway.do',
    'clientId' => '2017090408550236',
    'rsaPrivateKey' => 'your private key',
    'alipayrsaPublicKey' => 'your alipay public key [支付宝公钥]',
    'apiVersion' => '1.0',
    'signType' => 'RSA2',
    'postCharset' => 'UTF-8',
    'format' => 'json',
    'redirectUri' => 'you callback url'
]);

$url = $alipay->getAuthorizationUrl([
    'scope' => $provider->getDefaultScopes(),
    'state' => mt_rand(1000, 9999)
]);

header('Location: '.$url);

    $alipay = new \CnOAuth\Provider\Alipay([
        'gatewayUrl' => 'https://openapi.alipay.com/gateway.do',
        'clientId' => '2017090408550236',
        'rsaPrivateKey' => 'your private key',
        'alipayrsaPublicKey' => 'your alipay public key [支付宝公钥]',
        'apiVersion' => '1.0',
        'signType' => 'RSA2',
        'postCharset' => 'UTF-8',
        'format' => 'json',
        'redirectUri' => 'you callback url'
    ]);

    $grant = $alipay->getGrant('authorization');

    if ($grant->getCode()) {
        $access_token = $alipay->getAccessToken($grant);

        $owner = $alipay->getResourceOwner($access_token);

        print_r($owner->toArray());
    } else {
        // user cancel auth
    }