0

0

QQ互联OAuth

PHP中文网

PHP中文网

发布时间:2016-05-23 17:09:45

|

1929人浏览过

|

来源于php中文网

原创

代码片段(1) [全屏查看所有代码]

                       

                       

1. [代码][PHP]代码     跳至                     [1]     [全屏预览]

           

/**
 * 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'];
	}
}

                   

SCNet智能助手
SCNet智能助手

SCNet超算互联网平台AI智能助手

下载

                   

QQ
QQ

QQ是腾讯在中国开发的一款社交应用,用户可用QQ在手机和电脑上聊天。QQ截止目前拥有超过8.2亿用户,是中国最多人使用的社交应用程序之一,有需要的小伙伴快来保存下载体验吧!

下载

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
php源码安装教程大全
php源码安装教程大全

本专题整合了php源码安装教程,阅读专题下面的文章了解更多详细内容。

65

2025.12.31

php网站源码教程大全
php网站源码教程大全

本专题整合了php网站源码相关教程,阅读专题下面的文章了解更多详细内容。

43

2025.12.31

视频文件格式
视频文件格式

本专题整合了视频文件格式相关内容,阅读专题下面的文章了解更多详细内容。

35

2025.12.31

不受国内限制的浏览器大全
不受国内限制的浏览器大全

想找真正自由、无限制的上网体验?本合集精选2025年最开放、隐私强、访问无阻的浏览器App,涵盖Tor、Brave、Via、X浏览器、Mullvad等高自由度工具。支持自定义搜索引擎、广告拦截、隐身模式及全球网站无障碍访问,部分更具备防追踪、去谷歌化、双内核切换等高级功能。无论日常浏览、隐私保护还是突破地域限制,总有一款适合你!

41

2025.12.31

出现404解决方法大全
出现404解决方法大全

本专题整合了404错误解决方法大全,阅读专题下面的文章了解更多详细内容。

204

2025.12.31

html5怎么播放视频
html5怎么播放视频

想让网页流畅播放视频?本合集详解HTML5视频播放核心方法!涵盖<video>标签基础用法、多格式兼容(MP4/WebM/OGV)、自定义播放控件、响应式适配及常见浏览器兼容问题解决方案。无需插件,纯前端实现高清视频嵌入,助你快速打造现代化网页视频体验。

9

2025.12.31

关闭win10系统自动更新教程大全
关闭win10系统自动更新教程大全

本专题整合了关闭win10系统自动更新教程大全,阅读专题下面的文章了解更多详细内容。

8

2025.12.31

阻止电脑自动安装软件教程
阻止电脑自动安装软件教程

本专题整合了阻止电脑自动安装软件教程,阅读专题下面的文章了解更多详细教程。

3

2025.12.31

html5怎么使用
html5怎么使用

想快速上手HTML5开发?本合集为你整理最实用的HTML5使用指南!涵盖HTML5基础语法、主流框架(如Bootstrap、Vue、React)集成方法,以及无需安装、直接在线编辑运行的平台推荐(如CodePen、JSFiddle)。无论你是新手还是进阶开发者,都能轻松掌握HTML5网页制作、响应式布局与交互功能开发,零配置开启高效前端编程之旅!

2

2025.12.31

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号