增加ThinkSDK的支付宝第三方登录
1、配置文件增加相应参数//支付宝登录<br>
'THINK_SDK_ALIPAY' => array(<br>
'APP_KEY' => '', //应用注册成功后分配的 APP ID<br>
'APP_SECRET' => '', //应用注册成功后分配的KEY<br>
'CALLBACK' => URL_CALLBACK . 'alipay',<br>
),
2、相关SDK代码,因为我们公司用的zend framework,我将thinkSDK移植进来肯定做了修改,所以仅供参考。支付宝登录与其他的不同,登录时有sign验证,而且只用请求一次,所以重载了thinkouath的部分类。<?php <br />
/**<br>
* @Author: 杰少pakey<br>
* @Email : admin@ptcms.com<br>
* @File : AlipaySDK.php<br>
*/<br>
<br>
class AlipaySDK extends ThinkOauth{<br>
/**<br>
* 获取requestCode的api接口<br>
* @var string<br>
*/<br>
protected $GetRequestCodeURL = 'https://mapi.alipay.com/gateway.do';<br>
<br>
/**<br>
* API根路径<br>
* @var string<br>
*/<br>
protected $ApiBase = 'https://mapi.alipay.com/gateway.do';<br>
<br>
public function getRequestCodeURL(){<br>
$this->config();<br>
$params = array(<br>
'input_charset' => 'utf-8',<br>
'login_service' => 'user_auth',<br>
'partner' => $this->AppKey,<br>
'request_id'=>date('YmdHis').rand(1000,9999),<br>
'return_url'=>$this->Callback,<br>
'return_url_failed'=>$this->Callback,<br>
'service'=>'wap.user.common.login',<br>
);<br>
$params['sign']=$this->createSign($params);<br>
$params['sign_type']='MD5';<br>
return $this->GetRequestCodeURL . '?' . http_build_query($params);<br>
}<br>
<br>
public function getAccessToken($code='',$extend=''){<br>
$sign=$_GET['sign'];<br>
unset($_GET['a'],$_GET['type'],$_GET['sign']);<br>
if ($sign===$this->createSign($_GET)){<br>
$this->Token=array(<br>
'access_token'=>$_GET['login_token'],<br>
'openid'=>$_GET['user_id'],<br>
);<br>
return $this->Token;<br>
}else{<br>
exit('签名验证失败');<br>
}<br>
}<br>
<br>
protected function createSign($params)<br>
{<br>
ksort($params);<br>
$stringToBeSigned = "";<br>
$i = 0;<br>
foreach ($params as $k => $v) {<br>
if ($i == 0) {<br>
$stringToBeSigned .= "$k" . "=" . "$v";<br>
} else {<br>
$stringToBeSigned .= "&" . "$k" . "=" . "$v";<br>
}<br>
$i++;<br>
}<br>
unset ($k, $v);<br>
return md5($stringToBeSigned.$this->AppSecret);<br>
}<br>
<br>
/**<br>
* 组装接口调用参数 并调用接口<br>
*/<br>
public function call($api, $param = array(), $method = 'GET', $multi = false){<br>
$param['partner']=$this->AppKey;<br>
$param['sign']=$this->createSign($param);<br>
$param['sign_type']='MD5';<br>
$data = $this->http($this->url($api), $param, $method);<br>
return ($data);<br>
}<br>
<br>
/**<br>
* 解析access_token方法请求后的返回值<br>
*/<br>
protected function parseToken($result, $extend){<br>
<br>
}<br>
<br>
/**<br>
* 获取当前授权应用的openid<br>
*/<br>
public function openid(){<br>
$data = $this->Token;<br>
if(!empty($data['openid']))<br>
return $data['openid'];<br>
else<br>
exit('没有获取到支付宝用户ID!');<br>
}<br>
<br>
}
3、获取信息的接口,支付宝默认仅提供userid和usertype,更多需要去申请。另外返回是xml,需要解析,这里就简单做下示例。public static function alipay($token){<br>
$alipay=DKOauth::getInstance('alipay', $token);<br>
$param=array(<br>
'_input_charset'=>'utf-8',<br>
'input_charset'=>'utf-8',<br>
'service'=>'mobile.common.login.userInfo.query',<br>
'token'=>$token['access_token'],<br>
'format'=>'json',<br>
);<br>
$data=$alipay->call('',$param);<br>
return $data;<br>
}
首发:http://pakey.net/blog/557.html,有问题请到这里留言
AD:真正免费,域名+虚机+企业邮箱=0元