zoukankan      html  css  js  c++  java
  • 腾讯微博的账号登录及api操作

    1.tqq.php
    <?php
    /**
     * PHP Library for t.qq.com
     *
     * @author 
     */
    class tqqPHP
    {
        function __construct($client_id, $client_secret, $access_token=NULL, $openid=NULL){
            $this->client_id=$client_id;
            $this->client_secret=$client_secret;
            $this->access_token=$access_token;
            $this->openid=$openid;
        }
     
        function login_url($callback_url){
            $params=array(
                'response_type'=>'code',
                'client_id'=>$this->client_id,
                'redirect_uri'=>$callback_url
            );
            return 'https://open.t.qq.com/cgi-bin/oauth2/authorize?'.http_build_query($params);
        }
     
        function access_token($callback_url, $code){
            $params=array(
                'grant_type'=>'authorization_code',
                'code'=>$code,
                'client_id'=>$this->client_id,
                'client_secret'=>$this->client_secret,
                'redirect_uri'=>$callback_url
            );
            $url='https://open.t.qq.com/cgi-bin/oauth2/access_token?'.http_build_query($params);
            $result_str=$this->http($url);
            $json_r=array();
            if($result_str!='')parse_str($result_str, $json_r);
            return $json_r;
        }
     
        function access_token_refresh($refresh_token){
            $params=array(
                'grant_type'=>'refresh_token',
                'refresh_token'=>$refresh_token,
                'client_id'=>$this->client_id
            );
            $url='https://open.t.qq.com/cgi-bin/oauth2/access_token?'.http_build_query($params);
            $result_str=$this->http($url);
            $json_r=array();
            if($result_str!='')parse_str($result_str, $json_r);
            return $json_r;
        }
     
        function me(){
            $params=array();
            $url='https://open.t.qq.com/api/user/info';
            return $this->api($url, $params);
        }
     
        function getMyTweet($reqnum=10, $pageflag=0){
            $params=array(
                'pageflag'=>$pageflag,
                'reqnum'=>$reqnum
            );
            $url='https://open.t.qq.com/api/statuses/broadcast_timeline';
            return $this->api($url, $params);
        }
     
        function getRecount($ids){
            $params=array(
                'ids'=>$ids,
                'flag'=>2
            );
            $url='https://open.t.qq.com/api/t/re_count';
            return $this->api($url, $params);
        }
     
        function getReplay($id, $flag=0, $f=0, $n=10){
            $params=array(
                'rootid'=>$id,
                'pageflag'=>$f,
                'reqnum'=>$n,
                'flag'=>$flag
            );
            $url='https://open.t.qq.com/api/t/re_list';
            return $this->api($url, $params);
        }
     
        function postOne($img_c, $pic=''){
            $params=array(
                'content'=>$img_c
            );
            if($pic!='' && is_array($pic)){
                $url='https://open.t.qq.com/api/t/add_pic';
                $params['pic']=$pic;
            }else{
                $url='https://open.t.qq.com/api/t/add';
            }
            return $this->api($url, $params, 'POST');
        }
     
        function api($url, $params, $method='GET'){
            $params['oauth_consumer_key']=$this->client_id;
            $params['access_token']=$this->access_token;
            $params['openid']=$this->openid;
            $params['clientip']=$this->getIP();
            $params['oauth_version']='2.a';
            $params['format']='json';
            $params['scope']='all';
            if($method=='GET'){
                $result_str=$this->http($url.'?'.http_build_query($params));
            }else{
                if(isset($params['pic'])){
                    uksort($params, 'strcmp');
                    $str_b=uniqid('------------------');
                    $str_m='--'.$str_b;
                    $str_e=$str_m. '--';
                    $body='';
                    foreach($params as $k=>$v){
                        if($k=='pic'){
                            if(is_array($v)){
                                $img_c=$v[2];
                                $img_n=$v[1];
                            }elseif($v{0}=='@'){
                                $url=ltrim($v, '@');
                                $img_c=file_get_contents($url);
                                $url_a=explode('?', basename($url));
                                $img_n=$url_a[0];
                            }
                            $body.=$str_m."
    ";
                            $body.='Content-Disposition: form-data; name="'.$k.'"; filename="'.$img_n.'"'."
    ";
                            $body.="Content-Type: image/unknown
    
    ";
                            $body.=$img_c."
    ";
                        }else{
                            $body.=$str_m."
    ";
                            $body.='Content-Disposition: form-data; name="'.$k.""
    
    ";
                            $body.=$v."
    ";
                        }
                    }
                    $body.=$str_e;
                    $headers[]="Content-Type: multipart/form-data; boundary=".$str_b;
                    $result_str=$this->http($url, $body, 'POST', $headers);
                }else{
                    $result_str=$this->http($url, http_build_query($params), 'POST');
                }
            }
            $json_r=array();
            if($result_str!='')$json_r=json_decode($result_str, true);
            return $json_r;
        }
     
        function getIP(){
            if(isset($_ENV['HTTP_CLIENT_IP'])){
                $ip=$_ENV['HTTP_CLIENT_IP'];
            }elseif(isset($_ENV['HTTP_X_FORWARDED_FOR'])){
                $ip=$_ENV['HTTP_X_FORWARDED_FOR'];
            }elseif(isset($_ENV['REMOTE_ADDR'])){
                $ip=$_ENV['REMOTE_ADDR'];
            }else{
                $ip=$_SERVER['REMOTE_ADDR'];
            }
            if(strstr($ip, ':')){
                $ipa=explode(':', $ip);
                foreach($ipa as $v){
                    if(strlen($v)>7)$ip=$v;
                }
            }
            if(strlen($ip)<7)$ip='0.0.0.0';
            return $ip;
        }
     
        function http($url, $postfields='', $method='GET', $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=='POST'){
                curl_setopt($ci, CURLOPT_POST, TRUE);
                if($postfields!='')curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
            }
            $headers[]="User-Agent: tqqPHP(piscdong.com)";
            curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($ci, CURLOPT_URL, $url);
            $response=curl_exec($ci);
            curl_close($ci);
            return $response;
        }
    } 

    2.config.php

    <?php
    //配置文件
    header('Content-Type: text/html; charset=UTF-8');
     
    $tqq_k=''; //腾讯微博应用App Key
    $tqq_s=''; //腾讯微博应用App Secret
    $callback_url='http://yoururl/callback.php'; //授权回调网址
    ?> 
     
    3.index.php

    <?php
    session_start();
    require_once('config.php');
    require_once('tqq.php');
     
    function getimgp($u){
        //图片处理
        $c=@file_get_contents($u);
        $name=md5($u).'.jpg';
        $mime='image/unknown';
        return array($mime, $name, $c);
    }
     
    $tqq_t=isset($_SESSION['tqq_t'])?$_SESSION['tqq_t']:'';
    $tqq_id=isset($_SESSION['tqq_id'])?$_SESSION['tqq_id']:'';
     
    //检查是否已登录
    if($tqq_t!='' && $tqq_id!=''){
        $tqq=new tqqPHP($tqq_k, $tqq_s, $tqq_t, $tqq_id);
     
        //获取登录用户信息
        $result=$tqq->me();
        var_dump($result);
     
        /**
        //access token到期后使用refresh token刷新access token
        $result=$tqq->access_token_refresh($_SESSION['tqq_r']);
        var_dump($result);
        **/
     
        /**
        //发布微博
        $img='http://www.baidu.com/img/baidu_sylogo1.gif';
        $img_a=getimgp($img);
        if($img_a[2]!=''){
            $p_img=$img_a;
        }else{
            $p_img='';
        }
        $result=$tqq->postOne('微博内容', $p_img);
        var_dump($result);
        **/
     
        /**
        //微博列表
        $result=$tqq->getMyTweet();
        var_dump($result);
        **/
     
    }else{
        //生成登录链接
        $tqq=new tqqPHP($tqq_k, $tqq_s);
        $login_url=$tqq->login_url($callback_url);
        echo '<a href="',$login_url,'">点击进入授权页面</a>';
    }
    ?> 

    4.callback.php

    <?php
    //授权回调页面,即配置文件中的$callback_url
    session_start();
    require_once('config.php');
    require_once('tqq.php');
     
    if(isset($_GET['code']) && $_GET['code']!=''){
        $tqq=new tqqPHP($tqq_k, $tqq_s);
        $result=$tqq->access_token($callback_url, $_GET['code']);
    }
    if(isset($result['access_token']) && $result['access_token']!='' && isset($_GET['openid']) && $_GET['openid']!=''){
        echo '授权完成,请记录<br/>access token:<input size="50" value="',$result['access_token'],'"><br/>openid:<input size="50" value="',$_GET['openid'],'"><br/>refresh token:<input size="50" value="',$result['refresh_token'],'">';
     
        //保存登录信息,此示例中使用session保存
        $_SESSION['tqq_t']=$result['access_token']; //access token
        $_SESSION['tqq_id']=$_GET['openid']; //openid
        $_SESSION['tqq_r']=$result['refresh_token']; //refresh token
    }else{
        echo '授权失败';
    }
    echo '<br/><a href="./">返回</a>';
    ?>
  • 相关阅读:
    设计模式学习总结
    WCF一个Host实现多契约服务
    通过服务端监控结果,说说WCF的并发处理
    分词中常用算法比较与设想
    SQL Server 2008建立分区表(Table Partition)
    .NET 4中Entity Framework 新增查询与优化
    Web Service 接口大量数据传输解决方案
    面向对象的设计原则与目标[总结篇]
    说说ADO.NET EF 中的实体修改方法
    数据库大型应用解决方案总结
  • 原文地址:https://www.cnblogs.com/qhorse/p/4844197.html
Copyright © 2011-2022 走看看