-
-
session_start();
- class openlogin{
- public $_URL = "";
-
- public $config = array();
public function __construct(){
- $this->openlogin();
- }
- function openlogin(){
}
/*取得登陸頁面URL*/
- public function login_url(){
-
- if(empty($this ->config)){
- 返回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)){
- 返回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);
- }
-
-
-
- /* 設定$this-> _param 佇列*/
- public function set($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,拼接後的
- * @return string 要求傳回的內容
- */
- public function post_contents($url) {
- if(empty($url)){
- return false;
- }
- $param = $this->combineURL("" , $this->_param);
- $ch =curl_init();
- // 設定URL 和對應的選項
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_FAILONERROR, false);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPTPT, 0VER);
- curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
- // 抓取URL放置其提交給瀏覽器
- $reponse = curl_exec($ch);
- curl_close($ch);
- 回傳$reponse;
-
- }
-
- /**
- * get_contents
- * 伺服器透過get請求取得內容
- * @param string $url 要求的url,拼接後的
- * @return string 要求傳回的內容
- */
- public function get_contents($url){
- $ch =curl_init();
- curl_setopt($ch, CURLOPT_init();
- curl_setopt($ch, CURLOPT_init();
- curl_setopt($ch, CURLOPT_SSL_VERIFPT_SSL_PERcurl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
- curl_setopt($ch, CURLOPT_URL, $url);
- $response =curl_exec($ch);
- curl_close($chch); ;
//-------請求為空
- if(empty($response)){
- return false;
- }
回傳$response;
- }
/**
- * combineURL
- * 拼接url
- * @param string $baseURL 基於的url
- * @param array $keysArr 參數列表數組
- * @return string 返回的url
- */
- public function mergeURL($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 extends openlogin{
- private $openname = "qq";
- public $config = array(
- "appkey"=> ;"你的應用智慧",
- "appsecret"=>"你的應用程式秘密",
- "redirect_uri"=>"XXXXX",
- "login_url" => "https://graph.qq; .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,addt,d,del_list, get_other_info,get_fanslist,get_idolist,add_idol,del_idol,get_tenpay_addr",
- "authorization_url"=>"https://graph.qq.com/oauth2.0/token"
- );
-
函數__construct()
- {
- $this->qq_openlogin();
- }
-
- 函數__construct(){
- 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 */
- $ response = $this->get_contents("https:/ /graph .qq.com/oauth2.0/me?access_token=".$params['access_token']);
; //--------偵測錯誤是否發生
- if(strpos($response, "callback") !== false){
$lpos = strpos($回應, "(");
- $rpos = strrpos($response, ")");
- $response = substr($response, $lpos + 1, $rpos - $lpos -1);
- }
$user = json_decode($response);
- if(isset($user->error)){
- return false;
- }
/*
- 取得使用者資訊需要參數:openid(使用者的ID,與QQ號碼一一對應),access_token(可透過使用Authorization_Code取得Access_Token 或來取得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";
- /*拼接url*/
- $get_user_url = $this->combineURL("https://graph.qq.com/user/ get_user_info",$open_param);
- //獵取使用者資訊
- $userinfo = $this->get_contents($get_user_url);
$userinfo = json_decode($userinfo);
return $userinfo;
- }
- }
//php實作微博登入
- class weibo_openlogin extends openlogin{
- private $openname = "weibo";
- public $config = array(
- "appkey"=>"your appkey",
- "appsecret"=>"your appsecret",
- "login_url"appsecret"=>"your appsecret",
- "login_url"appsecret"=>"your appsecret",
- "login_url"appsecret"=>"your appsecret",
- "login_url"appsecret"=>"your appsecret",
- "login_url" > "https://api.weibo.com/oauth2/authorize",
- "redirect_uri"=>"XXXXXXX",
- "authorization_url"=>"https://api.weibo.com/oauth2/ access_token"
- );
function __construct()
- {
- $this->qq_openlogin();
- }
-
- function qq_openin🎜> }
-
- function qq_openin🎜> {
- parent::__construct();
- }
function get_access_token(){
- $response = parent::get_access_token();
- $userinfo = jdecode ($response);
- return $userinfo;
- }
- }
//php實作淘寶登入
- class taobao_openlogin extends openlogin{
- private $openname> private $name = "taobao";
- public $config = array(
- "appkey"=>"your appkey",
- "appsecret"=>"your appsecret",
- "redirect_uri"=>"XXXXX ",
- "authorization_url"=>"https://oauth.taobao.com/token",
- "login_url"=>"https://oauth.taobao.com/authorize"
- );
function __construct()
- {
- $this->qq_openlogin();
- }
-
- function qq_openlogin(){
- parent::🎜> parent:: __construct();
- }
function 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 extends openlogin{
- private $openopenname = "renren" "appid"=>"你的appid",
- "appkey"=>"你的appkey",
- "appsecret"=>"你的金鑰",
- "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>
function 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的刷新Token,長期有效,不會過期;
- 範圍:Access Token最終的訪問範圍,既是用戶實際獲取的權限清單(使用者在授權頁面時,有可能會取消某些要求的權限)。關於權限的具體資訊請參考
- */
- }
/*取得登陸頁面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("位置:".$url);
- }else{
- return false;
- }
- }
- }
-
-
複製程式碼
|