


Native PHP implements third-party login in Alipay App to obtain user information
Background
The App project requires the implementation of third-party WeChat and Alipay logins. WeChat can directly complete authentication on the App side to obtain user information, while Alipay requires back-end acquisition.
Process
1. The server first obtains the infoStr required by the App to call Alipay SDK
2. The App obtains user authorization through infoStr code
3. The server obtains the request token through the authorization code
4. The server obtains the user information through the token
Code
Before this, you should have some understanding of the Alipay interface docking process.
1. Create RSA2 method: get sign:
/** * enRSA2 RSA加密 * * @param String $data * @return String */ private function enRSA2($data) { $str = chunk_split(trim($this->private_key), 64, "\n"); $key = "-----BEGIN RSA PRIVATE KEY-----\n$str-----END RSA PRIVATE KEY-----\n"; // $key = file_get_contents(storage_path('rsa_private_key.pem')); 为文件时这样引入 $signature = ''; $signature = openssl_sign($data, $signature, $key, OPENSSL_ALGO_SHA256)?base64_encode($signature):NULL; return $signature; }
2. Create a Get parameter splicing method to ensure that it meets the Alipay signature string requirements:
/** * myHttpBuildQuery * 之所以不用 自带函数 `http_build_query` * 是因为格式化的时间带有 ‘:’ 会被转换成十六进制 utf-8 码 * * @param Array * @return String */ private function myHttpBuildQuery($dataArr) { ksort($dataArr); $signStr = ''; foreach ($dataArr as $key => $val) { if (empty($signStr)) { $signStr = $key.'='.$val; } else { $signStr .= '&'.$key.'='.$val; } } return $signStr; }
3. Give Go to the infoStr required on the APP:
/** * InfoStr APP登录需要的的infostr * * @return String */ public function infoStr() { $infoStr = http_build_query([ 'apiname' => 'com.alipay.account.auth', 'method' => 'alipay.open.auth.sdk.code.get', 'app_id' => $this->app_id, 'app_name' => 'mc', 'biz_type' => 'openservice', 'pid' => $this->pid, 'product_id' => 'APP_FAST_LOGIN', 'scope' => 'kuaijie', 'target_id' => mt_rand(999, 99999), //商户标识该次用户授权请求的ID,该值在商户端应保持唯一 'auth_type' => 'AUTHACCOUNT', // AUTHACCOUNT代表授权;LOGIN代表登录 'sign_type' => 'RSA2', ]); $infoStr .= '&sign='.$this->enRSA2($infoStr); return $infoStr; }
4. Get user information:
/** * AlipayToken 获得用户 请求token, 通过它获得 用户信息 * * 需要按照支付宝加签流程来。 */ public function userInfo($app_auth_token) { $infoArr = [ 'method' => 'alipay.system.oauth.token', 'app_id' => $this->app_id, 'charset' => 'utf-8', 'sign_type' => 'RSA2', 'timestamp' => date('Y-m-d H:i:s'), 'version' => '1.0', 'code' => $app_auth_token, 'grant_type' => 'authorization_code', ]; $signStr = $this->myHttpBuildQuery($infoArr); $sign = urlencode($this->enRSA2($signStr)); $qureStr = $signStr.'&sign='.$sign; $res = new Client(); $body = $res->get('https://openapi.alipay.com/gateway.do?'.$qureStr)->getBody()->getContents(); $body = json_decode($body); if (!isset($body->alipay_system_oauth_token_response->access_token)) { return '接口异常'; } else { $autho_token = $body->alipay_system_oauth_token_response->access_token; $userinfo = $this->aliPayUserInfo($autho_token); return $userinfo; // 或则 返回 json_encode($userinfo) 根据实际需求来 } } /** * AliPayUserInfo 通过 token 获取用户信息 */ private function aliPayUserInfo($autho_token) { $infoArr = [ 'method' => 'alipay.user.info.share', 'app_id' => $this->app_id, 'charset' => 'utf-8', 'sign_type' => 'RSA2', 'timestamp' => date('Y-m-d H:i:s'), 'version' => '1.0', 'auth_token' => $autho_token, ]; $signStr = $this->myHttpBuildQuery($infoArr); $sign = urlencode($this->enRSA2($signStr)); $qureStr = $signStr.'&sign='.$sign; $res = new Client(); $body = $res->get('https://openapi.alipay.com/gateway.do?'.$qureStr)->getBody()->getContents(); $body = json_decode($body); if (!isset($body->alipay_user_info_share_response)) { return '接口异常'; } $body = $body->alipay_user_info_share_response; return $body; }
Code Overview
'com.alipay.account.auth', 'method' => 'alipay.open.auth.sdk.code.get', 'app_id' => $this->app_id, 'app_name' => 'mc', 'biz_type' => 'openservice', 'pid' => $this->pid, 'product_id' => 'APP_FAST_LOGIN', 'scope' => 'kuaijie', 'target_id' => mt_rand(999, 99999), //商户标识该次用户授权请求的ID,该值在商户端应保持唯一 'auth_type' => 'AUTHACCOUNT', // AUTHACCOUNT代表授权;LOGIN代表登录 'sign_type' => 'RSA2', ]); $infoStr .= '&sign='.$this->enRSA2($infoStr); return $infoStr; } /** * AlipayToken 获得用户 请求token, 通过它获得 用户信息 * * 需要按照支付宝加签流程来。 */ public function userInfo($app_auth_token) { $infoArr = [ 'method' => 'alipay.system.oauth.token', 'app_id' => $this->app_id, 'charset' => 'utf-8', 'sign_type' => 'RSA2', 'timestamp' => date('Y-m-d H:i:s'), 'version' => '1.0', 'code' => $app_auth_token, 'grant_type' => 'authorization_code', ]; $signStr = $this->myHttpBuildQuery($infoArr); $sign = urlencode($this->enRSA2($signStr)); $qureStr = $signStr.'&sign='.$sign; $res = new Client(); $body = $res->get('https://openapi.alipay.com/gateway.do?'.$qureStr)->getBody()->getContents(); $body = json_decode($body); if (!isset($body->alipay_system_oauth_token_response->access_token)) { return '接口异常'; } else { $autho_token = $body->alipay_system_oauth_token_response->access_token; $userinfo = $this->aliPayUserInfo($autho_token); return $userinfo; // 或则 返回 json_encode($userinfo) 根据实际需求来 } } /** * AliPayUserInfo 通过 token 获取用户信息 */ private function aliPayUserInfo($autho_token) { $infoArr = [ 'method' => 'alipay.user.info.share', 'app_id' => $this->app_id, 'charset' => 'utf-8', 'sign_type' => 'RSA2', 'timestamp' => date('Y-m-d H:i:s'), 'version' => '1.0', 'auth_token' => $autho_token, ]; $signStr = $this->myHttpBuildQuery($infoArr); $sign = urlencode($this->enRSA2($signStr)); $qureStr = $signStr.'&sign='.$sign; $res = new Client(); $body = $res->get('https://openapi.alipay.com/gateway.do?'.$qureStr)->getBody()->getContents(); $body = json_decode($body); if (!isset($body->alipay_user_info_share_response)) { return '接口异常'; } $body = $body->alipay_user_info_share_response; return $body; } /** * enRSA2 RSA加密 * * @param String $data * @return String */ private function enRSA2($data) { $str = chunk_split(trim($this->private_key), 64, "\n"); $key = "-----BEGIN RSA PRIVATE KEY-----\n$str-----END RSA PRIVATE KEY-----\n"; // $key = file_get_contents(storage_path('rsa_private_key.pem')); 为文件时这样引入 $signature = ''; $signature = openssl_sign($data, $signature, $key, OPENSSL_ALGO_SHA256)?base64_encode($signature):NULL; return $signature; } /** * myHttpBuildQuery 返回一个 http Get 传参数组 * 之所以不用 自带函数 http_build_query 时间带 ‘:’ 会被转换 * * @param Array * @return String */ private function myHttpBuildQuery($dataArr) { ksort($dataArr); $signStr = ''; foreach ($dataArr as $key => $val) { if (empty($signStr)) { $signStr = $key.'='.$val; } else { $signStr .= '&'.$key.'='.$val; } } return $signStr; } }
Others
1. Note: This code is extracted from the original project. It is mainly to provide a reference for developers who have this need. It has not been tested whether it can be used directly. Please test it yourself. .
2. The reason why Alipay php_SDK is not used is because the demand is limited: it is only necessary to obtain user information, which is not necessary.
3. Please point out any unreasonable parts of the code so that everyone can learn from each other.
The above is the detailed content of Native PHP implements third-party login in Alipay App to obtain user information. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
