支付寶生活號授權獲取用戶信息


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;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM