php支付宝小程序获取用户user_id

吐槽一下官方文档就是坑,写的啥不清不楚不更新,sdk也是乱七八糟的
1:首先下载文章底部的sdk,放到项目目录中,添加composer文件

"autoload": {
        "classmap": [
            "aop"
        ]
    }

如图
在这里插入图片描述
2:执行 composer update 自动加载

3:封装一个方法

<?php

namespace app\helper;


use think\Exception;

class AliClient
{

    public static function base()
    {
        $aop                          = new \AopCertClient();
        $aop->appId                   = "202100211413xxxx";
        $aop->rsaPrivateKey           = file_get_contents(APP_PATH . Env::get('alipay.private_key')); //私钥 文件地址
        $aop->alipayrsaPublicKey      =  $aop->getPublicKey("./alipayCertPublicKey_RSA2.crt");//调用getPublicKey从支付宝公钥证书中提取公钥,即文件 alipayCertPublicKey_RSA2.crt 的地址,如:
        $aop->apiVersion              = '1.0';
        $aop->signType                = "RSA2";
        $aop->format                  = "json";
        $aop->postCharset             = "utf-8";
        $aop->isCheckAlipayPublicCert = true; //是否校验自动下载的支付宝公钥证书,如果开启校验要保证支付宝根证书在有效期内
        $aop->appCertSN               = $aop->getCertSN("./appCertPublicKey.crt"); //调用getCertSN获取证书序列号
        $aop->alipayRootCertSN        = $aop->getRootCertSN("./alipayRootCert.crt");//调用getRootCertSN获取支付宝根证书序列号
        return $aop;
    }

    public static function getOpenId($auth_code)
    {
        $aop     = self::base();
        $request = new \AlipaySystemOauthTokenRequest();
        $request->setGrantType("authorization_code");
        $request->setCode($auth_code);
        $result   = $aop->execute($request);
        $response = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        if (isset($result->error_response)) {
        	return errorMsg($result->error_response->sub_msg); //返回报错信息
        }
        return $result->$response->user_id;
    }

}

支付宝的文档就是坑!

链接:https://pan.baidu.com/s/1qXX5NPsDFZ3hAoYK5zYJ-Q
提取码:lm6g

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值