-
-
session_start(); - class openlogin{
- public $_URL = "";
-
- 공개 $config = array();
공용 함수 __construct(){
- $this->openlogin();
- }
- 함수 openlogin(){
}
/*获取登陆页면URL*/
- public function login_url(){
-
- if(empty($this->config)){
- return false;
- }
-
- $config = $this->config;
- $login_url = $config['login_url'];
- $_SESSION['state'] = $state = md5( uniqid(rand(), TRUE));
- $array = array(
- "response_type"=>"code",
- "state" => $state,
- "client_id"= >$config['appkey'],
- "redirect_uri"=>urlencode( $config['redirect_uri'] )
- );
$this->set($array);
- $url = $this->combineURL($login_url , $this->_param);
- if($url){
- @header( "위치:".$url);
- }else{
- return false;
- }
- }
/*获取access_token*/
- public function get_access_token(){
- if(empty($this->config)){
- return false;
- }
$config = $this->config;
if(! $config['code'] = $_REQUEST['code'] ){
- return false;
- }
$url = $config['authorization_url'];
- $state = $_SESSION['state'];
- $array = array(
- "grant_type"=>"authorization_code",
- " client_id" => $config['appkey'],
- "client_secret"=>$config['appsecret'],
- "code"=>$config['code'],
- "redirect_uri"=>urlencode( $config['redirect_uri'] ),
- "state"=>$state
- );
- $this->set($array);
- return $this->post_contents($url);
- }
-
-
-
- /* set $this->_param 数组*/
- 공용 함수 세트($array) {
- if(empty($array)){
- return false;
- }
- $this->_param = array();
- foreach($array as $name=> $value){
- $this->_param[$name] = $value;
- }
- }
- /**
- * post_contents
- * 서버는 post 요청을 통해 콘텐츠를 얻습니다
- * @param string $url 요청된 URL, spliced
- * @return string 요청에 의해 반환된 콘텐츠
- */
- 공개 함수 post_contents($url) {
- if(empty($url)){
- return false;
- }
- $param = $this->combineURL("" , $this->_param);
- $ch = cur_init();
- // URL과 상호작용하는 URL
- 컬_setopt($ch, CURLOPT_URL, $url);
- 컬_setopt($ch, CURLOPT_FAILONERROR, false);
- 컬_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- 컬_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
- 컬_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
- 컬_setopt($ch, CURLOPT_POST, 1);
- 컬_setopt($ch, CURLOPT_POSTFIELDS, $param);
- // 抓取URL并把它传递给浏览器
- $reponse =curl_exec($ch);
- 컬_닫기($ch);
- $응답 반환;
-
- }
-
- /**
- * get_contents
- * 서버는 get 요청을 통해 콘텐츠를 얻습니다
- * @param string $url 요청된 URL, spliced
- * @return string 요청에 의해 반환된 콘텐츠
- */
- 공개 함수 get_contents($url){
- $ch =curl_init();
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
- 컬_세톱($ch, CURLOPT_RETURNTRANSFER, TRUE);
- 컬_세톱($ch, CURLOPT_URL, $url);
- $response = 컬_exec($ch);
- 컬_닫기($ch);
//-------请求为공간
- if(empty($response)){
- return false;
- }
return $response;
- }
/**
- * CombineURL
- * 연결된 URL
- * @param string $baseURL based on url
- * @param array $keysArr 매개변수 목록 배열
- * @return 문자열은 연결된 URL을 반환합니다
- */
- 공개 함수 CombineURL($baseURL,$keysArr){
- if( $baseURL=="" ){
- $combined = "";
- }else{
- $combined = $baseURL."?";
- }
- $valueArr = array();
foreach($keysArr as $key => $val){
- $valueArr[] = "$key=$val";
- }
$keyStr = implode("&",$valueArr);
- $combined .= ($keyStr);
-
- return $combined;
- }
-
- }
//php实现QQ登录
- class qq_openlogin은 openlogin을 확장합니다.{
- private $openname = "qq";
- public $config = array(
- "appkey"=> ;"당신의 앱 키",
- "appsecret"=>"당신의 앱 비밀",
- "redirect_uri"=>"XXXXX",
- "login_url" => .com/oauth2.0/authorize",
- "scope"=>"get_user_info,add_share,list_album,add_album,upload_pic,add_topic,add_one_blog,add_weibo,check_page_fans,add_t,add_pic_t,del_t,get_repost_list,
- get_info ,get_other_info,get_fanslist,get_idolist,add_idol,del_idol,get_tenpay_addr",
- "authorization_url"=>"https://graph.qq.com/oauth2.0/token"
- );
function __construct()
- {
- $this->qq_openlogin();
- }
-
- function qq_openlogin(){
- parent::__construct();
- }< ;/p>
function get_access_token(){
- $response = parent::get_access_token();
-
- /*检测错误是否发生*/
- if(strpos($response, "callback") !== false ){
$lpos = strpos($response, "(");
- $rpos = strrpos($response, ")");
- $response = substr($response, $lpos 1, $rpos - $lpos - 1);
- $msg = json_decode($response);
if(isset($msg->error)){
- return false;
- }
- }
$params = array();
- parse_str($response, $params);
- /*access_token == $params[access_token]*/
-
- /*获取 openid */
- $ 응답 = $this->get_contents("https://graph.qq.com/oauth2.0/me?access_token=".$params['access_token']);
- if(strpos($response, "callback") !== false){
$lpos = strpos($response, "(");
- $rpos = strrpos($response, ")");
- $response = substr($response, $lpos 1, $rpos - $lpos - 1);
- }
$user = json_decode($response);
- if(isset($user->error)){
- return false;
- }
/*
- 사용자 정보 획득에 필요한 매개변수: openid(사용자 ID, QQ 번호와 1:1 대응), access_token(Access_Token은 Authorization_Code를 사용하여 얻을 수 있음) access_token의 유효 기간은 3개월입니다.), oauth_consumer_key(사용자 appid), format(반환 형식)
-
- */
- /*데이터베이스 저장*/
- $open_param = array(
- "openid"= > ;$user->openid,
- "access_token"=>$params['access_token']
- );
- //
- $open_param['oauth_consumer_key'] = $this-> ; config['appkey'];
- $open_param['format'] = "json";
- /*splicing url*/
- $get_user_url = $this->combineURL("https:// graph .qq.com/user/get_user_info",$open_param);
- //사용자 정보 가져오기
- $userinfo = $this->get_contents($get_user_url);
- < p> ; $userinfo = json_decode($userinfo);
return $userinfo;
- }
- }
//php 구현 Weibo 로그인
- class weibo_openlogin 확장 openlogin{
- private $openname = "weibo";
- public $config = array(
- "appkey"=>"your appkey",
- "appsecret "=>"your appsecret",
- "login_url" => "https://api.weibo.com/oauth2/authorize",
- "redirect_uri"=>"XXXXXXX",
- "authorization_url"=>"https://api.weibo.com/oauth2/access_token"
- );
함수 __construct()
- {
- $ this->qq_openlogin();
- }
-
- 함수 qq_openlogin(){
- parent::__construct();
- }
get_access_token(){
- $response = parent::get_access_token();
- $userinfo = json_decode($response);
- return $userinfo;
- }
- }
//php는 Taobao 로그인을 구현합니다.
- class taobao_openlogin은 openlogin을 확장합니다.{
- private $openname = "taobao";
- public $config = array(
- "appkey"=>" your appkey",
- "appsecret"=>"appsecret",
- "redirect_uri"=>"XXXXX",
- "authorization_url"=>"https://oauth.taobao.com /token",
- "login_url"=>"https://oauth.taobao.com/authorize"
- );
함수 __construct()
- {
- $this->qq_openlogin();
- }
-
- 함수 qq_openlogin(){
- parent::__construct();
- }
-
함수 get_access_token(){
- $response = parent::get_access_token();
- $userinfo = json_decode($response);
- return $userinfo;
- }
}
- if($_GET['openname']){
- $openname = $_GET['openname']."_openlogin";
- $openlogin = new $openname( );
- if(!isset($_REQUEST['code'])){
- //url 요청
- $url = $openlogin->login_url();
- if(!$url ){
- echo "0";
- exit();
- }
- }else{
- if(isset($_REQUEST["state"]) && ($_SESSION['state' ] != $_REQUEST["state"] )){
- echo "1";
- exit();
- }
-
- $rs = $openlogin->get_access_token();
- print_r( $rs );
- }
- }
- ?>
-
코드 복사
附,人人网登陆代码。
-
-
class renren_openlogin은 openlogin을 확장합니다.{ - private $openname = "renren";
- public $config = array(
- "appid"=>"your appid",
- "appkey"=>"your appkey",
- "appsecret"=>"your secret key",
- "redirect_uri"=>"XXXXXX",
- "authorization_url"=>"https://graph.renren.com/oauth/token",
- "login_url"=>"https: //graph.renren.com/oauth/authorize"
- );
function __construct()
- {
- $this->qq_openlogin();
- }
-
- function qq_openlogin(){
- parent::__construct();
- }< ;/p>
함수 get_access_token(){
- $response = parent::get_access_token();
$userinfo = json_decode($response);
return $userinfo;
- /*
- access_token:获取的Access Token;
- expires_in:Access Token의 유效期,以秒为单位;
- refresh_token:용于刷新Access Token의 Refresh Token,长期有效,不会过期;
- 범위:액세스 토큰最终的访问范围,既用户实际授予的权限列表(用户在授权页face时,有可能会取消掉某些请求的权限)。关于权限的具体信息请参考
- */
- }
/*获取登陆页면URL*/
- public function login_url(){
-
- if(empty($this->config)){
- return false;
- }
-
- $config = $this->config;
- $login_url = $config['login_url'];
- $array = array(
- "response_type"=> ;"code",
- "client_id"=>$config['appid'],
- "redirect_uri"=>urlencode( $config['redirect_uri'] )
- );
$this->set($array);
$url = $this->combineURL($login_url , $this->_param);
if($url){
- @header("Location:".$url);
- }else{
- return false;
- }
- }
- }
-
复代代码
|