大家好 这是我的第一篇文章 我是一名非常菜鸟的PHP程序员 今天做了一下支付宝的授权接口分享一下 希望能帮助大家
首先进入支付宝沙箱环境应用页
https://openhome.alipay.com/platform/appDaily.htm?tab=info
然后下载支付宝的SDK
下面是CI框架下的代码
class Authorize extends CI_Controller
{
//沙盒环境参数
private $appid = '2016080400164499';
private $url = "https://openauth.alipaydev.com/oauth2/publicAppAuthorize.htm";
private $alipay_api = "https://openapi.alipaydev.com/gateway.do";
public function __construct ()
{
parent::__construct();
include_once APPPATH.'libraries/Alipay/AopSdk.php';
$this->AopClient = new AopClient();
$this->load->helper('function');
}
//授权和回调
public function auth()
{
//获取url参数
$params = $this->input->get(NULL,true);
//首次进入跳转到支付宝授权口
if(!isset($params['auth_code'])) {
$redirect_url = urlencode('http://bxu2442160182.my3w.com/index.php/Authorize/auth');
$auth_url = $this->url ."?app_id={$this->appid}&scope=auth_user&redirect_uri={$redirect_url}&state=test";
$this->load->view('Authorize', array('auth_url'=>$auth_url));
//支付宝回调处理
} else {
//获取支付宝回传的参数
$auth_code = $params['auth_code'];
//组装请求支付宝获取会员ID
$this->AopClient->gatewayUrl = $this->alipay_api;
$this->AopClient->appId = $this->appid;
$this->AopClient->rsaPrivateKey = 'MIIEowIBAAKCAQEAr9BL7Ec7LXugVmonLu4r4CUEbSoQVr+vkcVZFnypdaW1sT0ywK1mlLtgIUTtwpFveNO5O0O2p5i79HXrNq2Ca/VvR+hM5QvmDv/g5pEMYJBpj06T9LD7/B9pByob0a77IiUmUMNYJOADdlE4r0yUekO0A7hswbeB2HDOFVjPLn17jQCiMrCZmn4K8OD25BDB5P64R1C71CDcs5kzLhuOI7kQPPg9e5P/mDt03lugPP/2t0xLGuY6eHmtSLrA8su7TjknXy+CNCv0OouynV+M12vwaGR0KxUaZxiQnFXUoRfHS/B+5xi5hTX5mF+yl8qrUAQRvJ7CYTXSTZgNJvxY4QIDAQABAoIBAGS5JuYEoQibrO4U/sHKPWS6kGCiGp44TzNqJnHVlZgLCZ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LC1sIrEfY2TY9890wjJ499ZYrqbcx0yJ+FgiuADm/NX0331O69MnxfNUKmsYNP1abONzNFjhY/HEDiOIlxjX2C/UUJmlXzTpowexZkon';
$this->AopClient->alipayrsaPublicKey='MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAr9BL7Ec7LXugVmonLu4r4CUEbSoQVr+vkcVZFnypdaW1sT0ywK1mlLtgIUTtwpFveNO5O0O2p5i79HXrNq2Ca/VvR+hM5QvmDv/g5pEMYJBpj06T9LD7/B9pByob0a77IiUmUMNYJOADdlE4r0yUekO0A7hswbeB2HDOFVjPLn17jQCiMrCZmn4K8OD25BDB5P64R1C71CDcs5kzLhuOI7kQPPg9e5P/mDt03lugPP/2t0xLGuY6eHmtSLrA8su7TjknXy+CNCv0OouynV+M12vwaGR0KxUaZxiQnFXUoRfHS/B+5xi5hTX5mF+yl8qrUAQRvJ7CYTXSTZgNJvxY4QIDAQAB';
$this->AopClient->apiVersion = '1.0';
$this->AopClient->signType = 'RSA2';
$this->AopClient->postCharset='utf-8';
$this->AopClient->format='json';
include_once APPPATH.'libraries/Alipay/aop/request/AlipaySystemOauthTokenRequest.php';
$request = new AlipaySystemOauthTokenRequest ();
$request->setGrantType("authorization_code");
$request->setCode($auth_code);
$result = $this->AopClient->execute($request);
//数组输出
echo object_to_array($result);
}
}