支付宝生活号授权获取用户信息
1.第一步在蚂蚁开放平台注册申请应用上线
2.下载支付宝秘钥生成器,生成秘钥(加在代码里面)和公钥(填写到蚂蚁开放平台)下载sdk放到extend文件夹里面
3.引用SDK
//头部加上 use think\Loader; header("Content-type:text/html;charset=utf-8"); Loader::import('alipay.AopSdk', EXTEND_PATH);
4.访问方法拼写授权地址
$encoded_url = 回调地址; $appid = APPID; $code_url = "https://openauth.alipay.com/oauth2/publicAppAuthorize.htm?app_id=$appid&scope=auth_user&redirect_uri=".urlencode($encoded_url);//获取auth_code $this->redirect($code_url);
5.获取用户信息
$code = $_GET['auth_code']; //APPID $appid = '支付宝平台的appid'; //应用私钥 文件名(rsa_private_key.pem) $rsaPrivateKey = "应用秘钥"; //支付宝公钥 文件名 (rsa_public_key.pem) $alipayrsaPublicKey = "支付宝公钥"; //初始化 $aop = new \AopClient (); $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do'; $aop->appId = $appid; $aop->rsaPrivateKey = $rsaPrivateKey; $aop->alipayrsaPublicKey = $alipayrsaPublicKey; $aop->apiVersion = '1.0'; $aop->signType = 'RSA2'; $aop->postCharset='UTF-8'; $aop->format='json'; //获取access_token $request = new \AlipaySystemOauthTokenRequest(); $request->setGrantType("authorization_code"); $request->setCode($code); $result = $aop->execute($request); $access_token = $result->alipay_system_oauth_token_response->access_token; //获取用户信息 $request_a = new \AlipayUserinfoShareRequest(); $result_a = $aop->execute ($request_a,'authusrB4d426f65fc0f4a1299ac3e6cc48ffX00'); //这里传入获取的access_token $responseNode_a = str_replace(".", "_", $request_a->getApiMethodName()) . "_response"; $user_id = $result_a->$responseNode_a->user_id; //用户唯一id $headimgurl = $result_a->$responseNode_a->avatar; //用户头像 $nick_name = $result_a->$responseNode_a->nick_name; //用户昵称 var_dump($user_id); var_dump($headimgurl); var_dump($nick_name); var_dump($result_a->$responseNode_a); die;
· 大模型 Token 究竟是啥:图解大模型Token
· 35岁程序员的中年求职记:四次碰壁后的深度反思
· 继承的思维:从思维模式到架构设计的深度解析
· 如何在 .NET 中 使用 ANTLR4
· 后端思维之高并发处理方案
· BotSharp + MCP 三步实现智能体开发
· BotSharp 5.0 MCP:迈向更开放的AI Agent框架
· 5. RabbitMQ 消息队列中 Exchanges(交换机) 的详细说明
· 【ESP32】两种模拟 USB 鼠标的方法
· 设计模式脉络