支付宝 网页授权 获取用户信息

                      ****** 支付宝 网页授权 获取用户信息(昵称,头像等)******

最近有个项目 需要获取到支付宝的头像和昵称作为互动数据,作为php新人,对于看文档来说
很是痛苦,网上也没有很多关于支付宝网页授权接口的详细信息。在一天零一个早上,才弄明白支付宝授权接口,所以想把代码分享出来,让一些新手们,可以节省点时间吧。(代码很简陋,请大神们不要喷。)

    1.  登录支付宝开放平台(蚂蚁金服开放平台)。https://open.alipay.com/

    2.   进入开发者中心,创建应用,并申请应用上线。

    3.   签约-获取会员信息功能。

    4.   设置 授权回调路径 和 接口加签方式(选择RSA2(SHA256)密钥) 

        密钥 生成 需要下载支付宝提供的生成工具。
        下载工具链接 : https://doc.open.alipay.com/docs/doc.htm?spm=a219a.7386797.0.0.iZGFov&treeId=291&articleId=105971&docType=1        

        选择2048,生成后绑定到支付宝开发中心。

    5.   下载支付宝SDK

    https://doc.open.alipay.com/doc2/detail?treeId=54&articleId=103419&docType=1


    6.   获取code。
$appid = '你的APPID';
$redirect_uri = urlencode ( '你的回调网址' );
$url ="https://openauth.alipay.com/oauth2/publicAppAuthorize.htm?app_id={$appid}&scope=auth_userinfo&redirect_uri={$redirect_uri}";

header("Location:".$url);
    7.  获取access_token,获取用户信息
<?php
header("Content-type:text/html;charset=utf-8");
//引入的SDK
include "./alipay/AopSdk.php";
include "./alipay/aop/AopClient.php";
include "./alipay/aop/request/AlipaySystemOauthTokenRequest.php";
include "./alipay/aop/request/AlipayUserUserinfoShareRequest.php";

$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);//这里传入 code
$result = $aop->execute($request);
$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
$access_token = $result->$responseNode->access_token;

//获取用户信息
$request_a = new AlipayUserUserinfoShareRequest ();
$result_a = $aop->execute ($request_a,$access_token); //这里传入获取的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);
die;
相关文档

https://doc.open.alipay.com/docs/doc.htm?spm=a219a.7386797.0.0.lGO8PQ&treeId=53&articleId=104114&docType=1
  • 14
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值