get_access_token($appid, $rsaPrivateKey, $alipayrsaPublicKey, $code); $access_token_msg = $this->get_access_token($appid, $rsaPrivateKey, $alipayrsaPublicKey, $code); $msg = array('code' => false, 'user_info' => array(), 'msg' => ''); if ($access_token_msg['code'] == false) { $msg['code'] = false; $msg['msg'] = $access_token_msg['msg']; } else { //获取用户信息 $accessToken = $access_token_msg['access_token']; Vendor('Pay.Ali2_0.AopClient'); Vendor('Pay.Ali2_0.AlipayUserInfoShareRequest'); Vendor('Pay.Ali2_0.SignData'); $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 = 'RSA'; $aop->postCharset = 'UTF-8'; $aop->format = 'json'; $request = new \AlipayUserInfoShareRequest (); $result = $aop->execute($request, $accessToken); $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response"; $result = json_decode(json_encode($result->$responseNode), true); if (!empty($result['code']) && $result['code'] = 10000) { $msg['code'] = true; $msg['user_info'] = $result; $msg['msg'] = '获取用户信息成功'; } else { $msg['code'] = false; $msg['user_info'] = $result; $msg['msg'] = '获取用户信息失败'; } } return $msg; } /** * get_access_token * @return mixed * @author: huangch */ private function get_access_token_bak($code) { $nonce_str = $this->createNonceStr(); $sig = md5($nonce_str.TOKEN_POOL_SALT); $url = ALI_TOKEN_POOL_URL.'?sig='.$sig.'&nonce_str='.$nonce_str.'&code='.$code; $res = json_decode($this->httpGet($url),true); if ($res['code'] == true) { return $res['token']; } else { cplog('get_access_token error:'.var_export($res,true)); } } private function createNonceStr($length = 16) { $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; $str = ""; for ($i = 0; $i < $length; $i++) { $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1); } return $str; } /** * 获取access_token * @author: linch * @param $appid * @param $rsaPrivateKey * @param $alipayrsaPublicKey * @param $code * @return array * @throws Exception */ public function get_access_token($appid, $rsaPrivateKey, $alipayrsaPublicKey, $code) { $msg = array('code' => false, 'access_token' => '', 'msg' => ''); Vendor('Pay.Ali2_0.AopClient'); Vendor('Pay.Ali2_0.AlipaySystemOauthTokenRequest'); Vendor('Pay.Ali2_0.SignData'); $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 = 'RSA'; $aop->postCharset = 'UTF-8'; $aop->format = 'json'; $request = new \AlipaySystemOauthTokenRequest (); //使用 code 换取 $request->setGrantType("authorization_code"); $request->setCode($code); $result = $aop->execute($request); $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response"; $result = json_decode(json_encode($result->$responseNode), true); if (!empty($result) && empty($result['code'])) { $msg['code'] = true; $msg['access_token'] = $result['access_token']; $msg['msg'] = '获取access_token成功'; } else { $msg['code'] = false; $msg['access_token'] = ''; $msg['msg'] = '获取access_token失败'; } return $msg; } /** * 获取access_token或者jsapi_ticket * @param $name * @return mixed */ private function getConfig($name) { $redis = new \Common\Cache\RedisCache(); return $redis->get($name); } /** * 设置access_token或者jsapi_ticket * @param $name * @param $value * @return mixed */ private function setConfig($name, $value, $time) { $redis = new \Common\Cache\RedisCache(); return $redis->set($name, $value, $time); } /** * curl get 方式 * @author: linch * @param $url * @return mixed */ private function httpGet($url) { $curl = curl_init(); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); //忽略证书 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($curl, CURLOPT_TIMEOUT, 500); curl_setopt($curl, CURLOPT_URL, $url); $res = curl_exec($curl); curl_close($curl); return $res; } }