Home  >  Article  >  php教程  >  QQ的账号登录及api操作

QQ的账号登录及api操作

PHP中文网
PHP中文网Original
2016-05-25 17:06:571436browse

QQ的账号登录及api操作,使用oauth 2.0 
官方提供的sdk都太过庞大,这是我自己简化的,提供简单的账号登录、获取个人信息、发布分享等功能,如果需要其他功能可以根据官方的api文档自行添加

qq.php

<?php
/**
 * PHP Library for qq.com
 *
 * @author PiscDong (http://www.piscdong.com/)
 */
class qqPHP
{
    function __construct($appid, $appkey, $access_token=NULL){
        $this->appid=$appid;
        $this->appkey=$appkey;
        $this->access_token=$access_token;
    }
 
    function login_url($callback_url, $scope=&#39;&#39;){
        $params=array(
            &#39;client_id&#39;=>$this->appid,
            &#39;redirect_uri&#39;=>$callback_url,
            &#39;response_type&#39;=>&#39;code&#39;,
            &#39;scope&#39;=>$scope
        );
        return &#39;https://graph.qq.com/oauth2.0/authorize?&#39;.http_build_query($params);
    }
 
    function access_token($callback_url, $code){
        $params=array(
            &#39;grant_type&#39;=>&#39;authorization_code&#39;,
            &#39;client_id&#39;=>$this->appid,
            &#39;client_secret&#39;=>$this->appkey,
            &#39;code&#39;=>$code,
            &#39;state&#39;=>&#39;&#39;,
            &#39;redirect_uri&#39;=>$callback_url
        );
        $url=&#39;https://graph.qq.com/oauth2.0/token?&#39;.http_build_query($params);
        $result_str=$this->http($url);
        $json_r=array();
        if($result_str!=&#39;&#39;)parse_str($result_str, $json_r);
        return $json_r;
    }
 
    /**
    function access_token_refresh($refresh_token){
    }
    **/
 
    function get_openid(){
        $params=array(
            &#39;access_token&#39;=>$this->access_token
        );
        $url=&#39;https://graph.qq.com/oauth2.0/me?&#39;.http_build_query($params);
        $result_str=$this->http($url);
        $json_r=array();
        if($result_str!=&#39;&#39;){
            preg_match(&#39;/callback\(\s+(.*?)\s+\)/i&#39;, $result_str, $result_a);
            $json_r=json_decode($result_a[1], true);
        }
        return $json_r;
    }
 
    function get_user_info($openid){
        $params=array(
            &#39;openid&#39;=>$openid
        );
        $url=&#39;https://graph.qq.com/user/get_user_info&#39;;
        return $this->api($url, $params);
    }
 
    function add_share($openid, $title, $url, $site, $fromurl, $images=&#39;&#39;, $summary=&#39;&#39;){
        $params=array(
            &#39;openid&#39;=>$openid,
            &#39;title&#39;=>$title,
            &#39;url&#39;=>$url,
            &#39;site&#39;=>$site,
            &#39;fromurl&#39;=>$fromurl,
            &#39;images&#39;=>$images,
            &#39;summary&#39;=>$summary
        );
        $url=&#39;https://graph.qq.com/share/add_share&#39;;
        return $this->api($url, $params, &#39;POST&#39;);
    }
 
    function api($url, $params, $method=&#39;GET&#39;){
        $params[&#39;access_token&#39;]=$this->access_token;
        $params[&#39;oauth_consumer_key&#39;]=$this->appid;
        $params[&#39;format&#39;]=&#39;json&#39;;
        if($method==&#39;GET&#39;){
            $result_str=$this->http($url.&#39;?&#39;.http_build_query($params));
        }else{
            $result_str=$this->http($url, http_build_query($params), &#39;POST&#39;);
        }
        $result=array();
        if($result_str!=&#39;&#39;)$result=json_decode($result_str, true);
        return $result;
    }
 
    function http($url, $postfields=&#39;&#39;, $method=&#39;GET&#39;, $headers=array()){
        $ci=curl_init();
        curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE); 
        curl_setopt($ci, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($ci, CURLOPT_TIMEOUT, 30);
        if($method==&#39;POST&#39;){
            curl_setopt($ci, CURLOPT_POST, TRUE);
            if($postfields!=&#39;&#39;)curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
        }
        $headers[]="User-Agent: qqPHP(piscdong.com)";
        curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ci, CURLOPT_URL, $url);
        $response=curl_exec($ci);
        curl_close($ci);
        return $response;
    }
}

config.php

<?php
//配置文件
header(&#39;Content-Type: text/html; charset=UTF-8&#39;);
 
$qq_k=&#39;&#39;; //QQ应用APP ID
$qq_s=&#39;&#39;; //QQ应用APP KEY
$callback_url=&#39;http://yoururl/callback.php&#39;; //授权回调网址
$scope=&#39;get_user_info,add_share&#39;; //权限列表,具体权限请查看官方的api文档
?>

3.index.php

<?php
session_start();
require_once(&#39;config.php&#39;);
require_once(&#39;qq.php&#39;);
 
$qq_t=isset($_SESSION[&#39;qq_t&#39;])?$_SESSION[&#39;qq_t&#39;]:&#39;&#39;;
 
//检查是否已登录
if($qq_t!=&#39;&#39;){
    $qq=new qqPHP($qq_k, $qq_s, $qq_t);
    $qq_oid=$qq->get_openid();
    $openid=$qq_oid[&#39;openid&#39;]; //获取登录用户open id
 
    //获取登录用户信息
    $result=$qq->get_user_info($openid);
    var_dump($result);
 
    /**
    //发布分享
    $title=&#39;开源中国&#39;; //分享页面标题
    $url=&#39;http://www.oschina.net/&#39;; //分享页面网址
    $site=&#39;&#39;; //QQ应用名称
    $fromurl=&#39;&#39;;  //QQ应用网址
    $result=$qq->add_share($openid, $title, $url, $site, $fromurl);
    var_dump($result);
    **/
 
}else{
    //生成登录链接
    $qq=new qqPHP($qq_k, $qq_s);
    $login_url=$qq->login_url($callback_url, $scope);
    echo &#39;<a href="&#39;,$login_url,&#39;">点击进入授权页面</a>&#39;;
}
?>

callback.php

<?php
//授权回调页面,即配置文件中的$callback_url
session_start();
require_once(&#39;config.php&#39;);
require_once(&#39;qq.php&#39;);
 
if(isset($_GET[&#39;code&#39;]) && trim($_GET[&#39;code&#39;])!=&#39;&#39;){
    $qq=new qqPHP($qq_k, $qq_s);
    $result=$qq->access_token($callback_url, $_GET[&#39;code&#39;]);
}
if(isset($result[&#39;access_token&#39;]) && $result[&#39;access_token&#39;]!=&#39;&#39;){
    echo &#39;授权完成,请记录<br/>access token:<input size="50" value="&#39;,$result[&#39;access_token&#39;],&#39;">&#39;;
 
    //保存登录信息,此示例中使用session保存
    $_SESSION[&#39;qq_t&#39;]=$result[&#39;access_token&#39;]; //access token
}else{
    echo &#39;授权失败&#39;;
}
echo &#39;<br/><a href="./">返回</a>&#39;;
?>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn