PHP版QQ互联OAuth示例代码分享
程序员文章站
2023-08-20 10:28:41
由于国内qq用户的普遍性,所以现在各大网站都尽可能的提供qq登陆口,下面我们来看看php版,给大家参考下
/**
* qq互联 oauth
* @auth...
由于国内qq用户的普遍性,所以现在各大网站都尽可能的提供qq登陆口,下面我们来看看php版,给大家参考下
/** * qq互联 oauth * @author dyllen * */ class oauth { //取authorization code url const pc_code_url = 'https://graph.qq.com/oauth2.0/authorize'; //取access token url const pc_access_token_url = 'https://graph.qq.com/oauth2.0/token'; //取用户 open id url const open_id_url = 'https://graph.qq.com/oauth2.0/me'; //用户授权之后的回调地址 public $redirecturi = null; // app id public $appid = null; //app key public $appkey = null; //授权列表 //字符串,多个用逗号隔开 public $scope = null; //授权code public $code = null; //续期access token的凭证 public $refreshtoken = null; //access token public $accesstoken = null; //access token 有效期,单位秒 public $expiresin = null; //state public $state = null; public $openid = null; //construct public function __construct($config=[]) { foreach($config as $key => $value) { $this->$key = $value; } } /** * 得到获取code的url * @throws \invalidargumentexception * @return string */ public function codeurl() { if (!$this->redirecturi) { throw new \exception('parameter $redirecturi must be set.'); } $query = [ 'response_type' => 'code', 'client_id' => $this->appid, 'redirect_uri' => $this->redirecturi, 'state' => $this->getstate(), 'scope' => $this->scope, ]; return self::pc_code_url . '?' . http_build_query($query); } /** * 取access token * @throws exception * @return boolean */ public function getaccesstoken() { $params = [ 'grant_type' => 'authorization_code', 'client_id' => $this->appid, 'client_secret' => $this->appkey, 'code' => $this->code, 'redirect_uri' => $this->redirecturi, ]; $url = self::pc_access_token_url . '?' . http_build_query($params); $content = $this->geturl($url); parse_str($content, $res); if ( !isset($res['access_token']) ) { $this->thrwoerror($content); } $this->accesstoken = $res['access_token']; $this->expiresin = $res['expires_in']; $this->refreshtoken = $res['refresh_token']; return true; } /** * 刷新access token * @throws exception * @return boolean */ public function refreshtoken() { $params = [ 'grant_type' => 'refresh_token', 'client_id' => $this->appid, 'client_secret' => $this->appkey, 'refresh_token' => $this->refreshtoken, ]; $url = self::pc_access_token_url . '?' . http_build_query($params); $content = $this->geturl($url); parse_str($content, $res); if ( !isset($res['access_token']) ) { $this->thrwoerror($content); } $this->accesstoken = $res['access_token']; $this->expiresin = $res['expires_in']; $this->refreshtoken = $res['refresh_token']; return true; } /** * 取用户open id * @return string */ public function getopenid() { $params = [ 'access_token' => $this->accesstoken, ]; $url = self::open_id_url . '?' . http_build_query($params); $this->openid = $this->parseopenid( $this->geturl($url) ); return $this->openid; } /** * get方式取url内容 * @param string $url * @return mixed */ public function geturl($url) { $ch = curl_init(); curl_setopt($ch, curlopt_ssl_verifypeer, false); curl_setopt($ch, curlopt_returntransfer, true); curl_setopt($ch, curlopt_url, $url); $response = curl_exec($ch); curl_close($ch); return $response; } /** * post方式取url内容 * @param string $url * @param array $keysarr * @param number $flag * @return mixed */ public function posturl($url, $keysarr, $flag = 0) { $ch = curl_init(); if(! $flag) curl_setopt($ch, curlopt_ssl_verifypeer, false); curl_setopt($ch, curlopt_returntransfer, true); curl_setopt($ch, curlopt_post, true); curl_setopt($ch, curlopt_postfields, $keysarr); curl_setopt($ch, curlopt_url, $url); $ret = curl_exec($ch); curl_close($ch); return $ret; } /** * 取state * @return string */ protected function getstate() { $this->state = md5(uniqid(rand(), true)); //state暂存在缓存里面 //自己定义 //。。。。。。。。。 return $this->state; } /** * 验证state * @return boolean */ protected function verifystate() { //。。。。。。。 } /** * 抛出异常 * @param string $error * @throws \exception */ protected function thrwoerror($error) { $suberror = substr($error, strpos($error, "{")); $suberror = strstr($suberror, "}", true) . "}"; $error = json_decode($suberror, true); throw new \exception($error['error_description'], (int)$error['error']); } /** * 从获取openid接口的返回数据中解析出openid * @param string $str * @return string */ protected function parseopenid($str) { $substr = substr($str, strpos($str, "{")); $substr = strstr($substr, "}", true) . "}"; $strarr = json_decode($substr, true); if(!isset($strarr['openid'])) { $this->thrwoerror($str); } return $strarr['openid']; } }
以上所述就是本文的全部内容了,希望大家能够喜欢。
上一篇: PHP 获取ip地址代码汇总
下一篇: 叫人懵B的搞笑事儿