首頁 >php教程 >PHP源码 >部分主流sns平台的账号登录及api操作

部分主流sns平台的账号登录及api操作

PHP中文网
PHP中文网原創
2016-05-25 17:08:521699瀏覽

新浪微博、腾讯微博、QQ、人人网、开心网、网易微博、豆瓣、百度、Google、微软、Instagram、Facebook、360、GitHub等平台的账号登录及api操作,使用oauth 2.0
官方提供的sdk都太过庞大,这是我自己简化的,提供简单的账号登录、获取个人信息、发布微博等功能,如果需要其他功能可以根据官方的api文档自行添加

1.新浪微博

<?php
/**
 * PHP Library for weibo.com
 *
 * @author PiscDong (http://www.piscdong.com/)
 */
class sinaPHP
{
    function __construct($client_id, $client_secret, $access_token=NULL){
        $this->client_id=$client_id;
        $this->client_secret=$client_secret;
        $this->access_token=$access_token;
    }
 
    function login_url($callback_url){
        $params=array(
            &#39;response_type&#39;=>&#39;code&#39;,
            &#39;client_id&#39;=>$this->client_id,
            &#39;redirect_uri&#39;=>$callback_url
        );
        return &#39;https://api.weibo.com/oauth2/authorize?&#39;.http_build_query($params);
    }
 
    function access_token($callback_url, $code){
        $params=array(
            &#39;grant_type&#39;=>&#39;authorization_code&#39;,
            &#39;code&#39;=>$code,
            &#39;client_id&#39;=>$this->client_id,
            &#39;client_secret&#39;=>$this->client_secret,
            &#39;redirect_uri&#39;=>$callback_url
        );
        $url=&#39;https://api.weibo.com/oauth2/access_token&#39;;
        return $this->http($url, http_build_query($params), &#39;POST&#39;);
    }
 
    /**
    function access_token_refresh($refresh_token){
    }
    **/
 
    function get_uid(){
        $params=array();
        $url=&#39;https://api.weibo.com/2/account/get_uid.json&#39;;
        return $this->api($url, $params);
    }
 
    function show_user_by_id($uid){
        $params=array(
            &#39;uid&#39;=>$uid
        );
        $url=&#39;https://api.weibo.com/2/users/show.json&#39;;
        return $this->api($url, $params);
    }
 
    function statuses_count($ids){
        $params=array(
            &#39;ids&#39;=>$ids
        );
        $url=&#39;https://api.weibo.com/2/statuses/count.json&#39;;
        return $this->api($url, $params);
    }
 
    function get_comments_by_sid($id, $count=10, $page=1){
        $params=array(
            &#39;id&#39;=>$id,
            &#39;page&#39;=>$page,
            &#39;count&#39;=>$count
        );
        $url=&#39;https://api.weibo.com/2/comments/show.json&#39;;
        return $this->api($url, $params);
    }
 
    function repost_timeline($id, $count=10, $page=1){
        $params=array(
            &#39;id&#39;=>$id,
            &#39;page&#39;=>$page,
            &#39;count&#39;=>$count
        );
        $url=&#39;https://api.weibo.com/2/statuses/repost_timeline.json&#39;;
        return $this->api($url, $params);
    }
 
    function update($img_c, $pic=&#39;&#39;){
        $params=array(
            &#39;status&#39;=>$img_c
        );
        if($pic!=&#39;&#39; && is_array($pic)){
            $url=&#39;https://api.weibo.com/2/statuses/upload.json&#39;;
            $params[&#39;pic&#39;]=$pic;
        }else{
            $url=&#39;https://api.weibo.com/2/statuses/update.json&#39;;
        }
        return $this->api($url, $params, &#39;POST&#39;);
    }
 
    function user_timeline($uid, $count=10, $page=1){
        $params=array(
            &#39;uid&#39;=>$uid,
            &#39;page&#39;=>$page,
            &#39;count&#39;=>$count
        );
        $url=&#39;https://api.weibo.com/2/statuses/user_timeline.json&#39;;
        return $this->api($url, $params);
    }
 
    function querymid($id, $type=1, $is_batch=0){
        $params=array(
            &#39;id&#39;=>$id,
            &#39;type&#39;=>$type,
            &#39;is_batch&#39;=>$is_batch
        );
        $url=&#39;https://api.weibo.com/2/statuses/querymid.json&#39;;
        return $this->api($url, $params);
    }
 
    function api($url, $params, $method=&#39;GET&#39;){
        $params[&#39;access_token&#39;]=$this->access_token;
        if($method==&#39;GET&#39;){
            $result=$this->http($url.&#39;?&#39;.http_build_query($params));
        }else{
            if(isset($params[&#39;pic&#39;])){
                uksort($params, &#39;strcmp&#39;);
                $str_b=uniqid(&#39;------------------&#39;);
                $str_m=&#39;--&#39;.$str_b;
                $str_e=$str_m. &#39;--&#39;;
                $body=&#39;&#39;;
                foreach($params as $k=>$v){
                    if($k==&#39;pic&#39;){
                        if(is_array($v)){
                            $img_c=$v[2];
                            $img_n=$v[1];
                        }elseif($v{0}==&#39;@&#39;){
                            $url=ltrim($v, &#39;@&#39;);
                            $img_c=file_get_contents($url);
                            $url_a=explode(&#39;?&#39;, basename($url));
                            $img_n=$url_a[0];
                        }
                        $body.=$str_m."\r\n";
                        $body.=&#39;Content-Disposition: form-data; name="&#39;.$k.&#39;"; filename="&#39;.$img_n.&#39;"&#39;."\r\n";
                        $body.="Content-Type: image/unknown\r\n\r\n";
                        $body.=$img_c."\r\n";
                    }else{
                        $body.=$str_m."\r\n";
                        $body.=&#39;Content-Disposition: form-data; name="&#39;.$k."\"\r\n\r\n";
                        $body.=$v."\r\n";
                    }
                }
                $body.=$str_e;
                $headers[]="Content-Type: multipart/form-data; boundary=".$str_b;
                $result=$this->http($url, $body, &#39;POST&#39;, $headers);
            }else{
                $result=$this->http($url, http_build_query($params), &#39;POST&#39;);
            }
        }
        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: sinaPHP(piscdong.com)";
        curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ci, CURLOPT_URL, $url);
        $response=curl_exec($ci);
        curl_close($ci);
        $json_r=array();
        if($response!=&#39;&#39;)$json_r=json_decode($response, true);
        return $json_r;
    }
}

2.腾讯微博

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

3.qq

<?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;
    }
}

4.人人网

<?php
/**
 * PHP Library for renren.com
 *
 * @author PiscDong (http://www.piscdong.com/)
 */
class renrenPHP
{
    function __construct($client_id, $client_secret, $access_token=NULL){
        $this->client_id=$client_id;
        $this->client_secret=$client_secret;
        $this->access_token=$access_token;
    }
 
    function login_url($callback_url, $scope=&#39;&#39;){
        $params=array(
            &#39;response_type&#39;=>&#39;code&#39;,
            &#39;client_id&#39;=>$this->client_id,
            &#39;redirect_uri&#39;=>$callback_url,
            &#39;scope&#39;=>$scope
        );
        return &#39;https://graph.renren.com/oauth/authorize?&#39;.http_build_query($params);
    }
 
    function access_token($callback_url, $code){
        $params=array(
            &#39;grant_type&#39;=>&#39;authorization_code&#39;,
            &#39;code&#39;=>$code,
            &#39;client_id&#39;=>$this->client_id,
            &#39;client_secret&#39;=>$this->client_secret,
            &#39;redirect_uri&#39;=>$callback_url
        );
        $url=&#39;https://graph.renren.com/oauth/token&#39;;
        return $this->http($url, http_build_query($params), &#39;POST&#39;);
    }
 
    function access_token_refresh($refresh_token){
        $params=array(
            &#39;grant_type&#39;=>&#39;refresh_token&#39;,
            &#39;refresh_token&#39;=>$refresh_token,
            &#39;client_id&#39;=>$this->client_id,
            &#39;client_secret&#39;=>$this->client_secret
        );
        $url=&#39;https://graph.renren.com/oauth/token&#39;;
        return $this->http($url, http_build_query($params), &#39;POST&#39;);
    }
 
    function me(){
        $params=array();
        return $this->api(&#39;users.getInfo&#39;, $params, &#39;POST&#39;);
    }
 
    function setStatus($status){
        $params=array(
            &#39;status&#39;=>$status
        );
        return $this->api(&#39;status.set&#39;, $params, &#39;POST&#39;);
    }
 
    function getStatus($uid, $count=10, $page=1){
        $params=array(
            &#39;uid&#39;=>$uid,
            &#39;page&#39;=>$page,
            &#39;count&#39;=>$count
        );
        return $this->api(&#39;status.gets&#39;, $params, &#39;POST&#39;);
    }
 
    function addBlog($title, $content){
        $params=array(
            &#39;title&#39;=>$title,
            &#39;content&#39;=>$content
        );
        return $this->api(&#39;blog.addBlog&#39;, $params, &#39;POST&#39;);
    }
 
    function getBlog($id, $uid){
        $params=array(
            &#39;id&#39;=>$id,
            &#39;uid&#39;=>$uid
        );
        return $this->api(&#39;blog.get&#39;, $params, &#39;POST&#39;);
    }
 
    function getComments($id, $uid, $count=10, $page=1){
        $params=array(
            &#39;id&#39;=>$id,
            &#39;uid&#39;=>$uid,
            &#39;page&#39;=>$page,
            &#39;count&#39;=>$count
        );
        return $this->api(&#39;blog.getComments&#39;, $params, &#39;POST&#39;);
    }
 
    function api($method_name, $params, $method=&#39;GET&#39;){
        $params[&#39;method&#39;]=$method_name;
        $params[&#39;v&#39;]=&#39;1.0&#39;;
        $params[&#39;access_token&#39;]=$this->access_token;
        $params[&#39;format&#39;]=&#39;json&#39;;
        ksort($params);
        $sig_str=&#39;&#39;;
        foreach($params as $k=>$v)$sig_str.=$k.&#39;=&#39;.$v;
        $sig_str.=$this->client_secret;
        $sig=md5($sig_str);
        $params[&#39;sig&#39;]=$sig;
        $url=&#39;http://api.renren.com/restserver.do&#39;;
        if($method==&#39;GET&#39;){
            $result=$this->http($url.&#39;?&#39;.http_build_query($params));
        }else{
            $result=$this->http($url, http_build_query($params), &#39;POST&#39;);
        }
        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: renrenPHP(piscdong.com)";
        curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ci, CURLOPT_URL, $url);
        $response=curl_exec($ci);
        curl_close($ci);
        $json_r=array();
        if($response!=&#39;&#39;)$json_r=json_decode($response, true);
        return $json_r;
    }
}

5.开心网

<?php
/**
 * PHP Library for kaixin001.com
 *
 * @author PiscDong (http://www.piscdong.com/)
 */
class kaixinPHP
{
    function __construct($client_id, $client_secret, $access_token=NULL){
        $this->client_id=$client_id;
        $this->client_secret=$client_secret;
        $this->access_token=$access_token;
    }
 
    function login_url($callback_url, $scope=&#39;&#39;){
        $params=array(
            &#39;response_type&#39;=>&#39;code&#39;,
            &#39;client_id&#39;=>$this->client_id,
            &#39;redirect_uri&#39;=>$callback_url,
            &#39;scope&#39;=>$scope
        );
        return &#39;http://api.kaixin001.com/oauth2/authorize?&#39;.http_build_query($params);
    }
 
    function access_token($callback_url, $code){
        $params=array(
            &#39;grant_type&#39;=>&#39;authorization_code&#39;,
            &#39;code&#39;=>$code,
            &#39;client_id&#39;=>$this->client_id,
            &#39;client_secret&#39;=>$this->client_secret,
            &#39;redirect_uri&#39;=>$callback_url
        );
        $url=&#39;https://api.kaixin001.com/oauth2/access_token&#39;;
        return $this->http($url, http_build_query($params), &#39;POST&#39;);
    }
 
    function access_token_refresh($refresh_token){
        $params=array(
            &#39;grant_type&#39;=>&#39;refresh_token&#39;,
            &#39;refresh_token&#39;=>$refresh_token,
            &#39;client_id&#39;=>$this->client_id,
            &#39;client_secret&#39;=>$this->client_secret
        );
        $url=&#39;https://api.kaixin001.com/oauth2/access_token&#39;;
        return $this->http($url, http_build_query($params), &#39;POST&#39;);
    }
 
    function me(){
        $params=array();
        $url=&#39;https://api.kaixin001.com/users/me.json&#39;;
        return $this->api($url, $params);
    }
 
    function records_add($content, $picurl=&#39;&#39;){
        $params=array(
            &#39;content&#39;=>$content
        );
        if($picurl!=&#39;&#39;)$params[&#39;picurl&#39;]=$picurl;
        $url=&#39;https://api.kaixin001.com/records/add.json&#39;;
        return $this->api($url, $params, &#39;POST&#39;);
    }
 
    function records_me($num=10, $start=0){
        $params=array(
            &#39;start&#39;=>$start,
            &#39;num&#39;=>$num
        );
        $url=&#39;https://api.kaixin001.com/records/me.json&#39;;
        return $this->api($url, $params);
    }
 
    function comment_list($id, $uid, $num=10, $start=0){
        $params=array(
            &#39;objtype&#39;=>&#39;records&#39;,
            &#39;objid&#39;=>$id,
            &#39;ouid&#39;=>$uid,
            &#39;start&#39;=>$start,
            &#39;num&#39;=>$num
        );
        $url=&#39;https://api.kaixin001.com/comment/list.json&#39;;
        return $this->api($url, $params);
    }
 
    function forward_list($id, $uid, $num=10, $start=0){
        $params=array(
            &#39;objtype&#39;=>&#39;records&#39;,
            &#39;objid&#39;=>$id,
            &#39;ouid&#39;=>$uid,
            &#39;start&#39;=>$start,
            &#39;num&#39;=>$num
        );
        $url=&#39;https://api.kaixin001.com/forward/list.json&#39;;
        return $this->api($url, $params);
    }
 
    function like_show($id, $uid, $num=10, $start=0){
        $params=array(
            &#39;objtype&#39;=>&#39;records&#39;,
            &#39;objid&#39;=>$id,
            &#39;ouid&#39;=>$uid,
            &#39;start&#39;=>$start,
            &#39;num&#39;=>$num
        );
        $url=&#39;https://api.kaixin001.com/like/show.json&#39;;
        return $this->api($url, $params);
    }
 
    function api($url, $params, $method=&#39;GET&#39;){
        $params[&#39;access_token&#39;]=$this->access_token;
        if($method==&#39;GET&#39;){
            $result=$this->http($url.&#39;?&#39;.http_build_query($params));
        }else{
            $result=$this->http($url, http_build_query($params), &#39;POST&#39;);
        }
        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: kaixinPHP(piscdong.com)";
        curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ci, CURLOPT_URL, $url);
        $response=curl_exec($ci);
        curl_close($ci);
        $json_r=array();
        if($response!=&#39;&#39;)$json_r=json_decode($response, true);
        return $json_r;
    }
}

6.网易微博

<?php
/**
 * PHP Library for t.163.com
 *
 * @author PiscDong (http://www.piscdong.com/)
 */
class t163PHP
{
    function __construct($client_id, $client_secret, $access_token=NULL){
        $this->client_id=$client_id;
        $this->client_secret=$client_secret;
        $this->access_token=$access_token;
    }
 
    function login_url($callback_url){
        $params=array(
            &#39;response_type&#39;=>&#39;code&#39;,
            &#39;client_id&#39;=>$this->client_id,
            &#39;redirect_uri&#39;=>$callback_url
        );
        return &#39;https://api.t.163.com/oauth2/authorize?&#39;.http_build_query($params);
    }
 
    function access_token($callback_url, $code){
        $params=array(
            &#39;grant_type&#39;=>&#39;authorization_code&#39;,
            &#39;code&#39;=>$code,
            &#39;client_id&#39;=>$this->client_id,
            &#39;client_secret&#39;=>$this->client_secret,
            &#39;redirect_uri&#39;=>$callback_url
        );
        $url=&#39;https://api.t.163.com/oauth2/access_token&#39;;
        return $this->http($url, http_build_query($params), &#39;POST&#39;);
    }
 
    function access_token_refresh($refresh_token){
        $params=array(
            &#39;grant_type&#39;=>&#39;refresh_token&#39;,
            &#39;refresh_token&#39;=>$refresh_token,
            &#39;client_id&#39;=>$this->client_id,
            &#39;client_secret&#39;=>$this->client_secret
        );
        $url=&#39;https://api.t.163.com/oauth2/access_token&#39;;
        return $this->http($url, http_build_query($params), &#39;POST&#39;);
    }
 
    function me(){
        $params=array();
        $url=&#39;https://api.t.163.com/users/show.json&#39;;
        return $this->api($url, $params);
    }
 
    function user_timeline($id, $count=10){
        $params=array(
            &#39;user_id&#39;=>$id,
            &#39;count&#39;=>$count
        );
        $url=&#39;https://api.t.163.com/statuses/user_timeline.json&#39;;
        return $this->api($url, $params);
    }
 
    function update($status){
        $params=array(
            &#39;status&#39;=>$status
        );
        $url=&#39;https://api.t.163.com/statuses/update.json&#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;
        if($method==&#39;GET&#39;){
            $result=$this->http($url.&#39;?&#39;.http_build_query($params));
        }else{
            $result=$this->http($url, http_build_query($params), &#39;POST&#39;);
        }
        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: t163PHP(piscdong.com)";
        curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ci, CURLOPT_URL, $url);
        $response=curl_exec($ci);
        curl_close($ci);
        $json_r=array();
        if($response!=&#39;&#39;)$json_r=json_decode($response, true);
        return $json_r;
    }
}

7.豆瓣

<?php
/**
 * PHP Library for douban.com
 *
 * @author PiscDong (http://www.piscdong.com/)
 */
class doubanPHP
{
    function __construct($client_id, $client_secret, $access_token=NULL){
        $this->client_id=$client_id;
        $this->client_secret=$client_secret;
        $this->access_token=$access_token;
    }
 
    function login_url($callback_url, $scope=&#39;&#39;){
        $params=array(
            &#39;response_type&#39;=>&#39;code&#39;,
            &#39;client_id&#39;=>$this->client_id,
            &#39;redirect_uri&#39;=>$callback_url,
            &#39;scope&#39;=>$scope,
            &#39;state&#39;=>md5(time())
        );
        return &#39;https://www.douban.com/service/auth2/auth?&#39;.http_build_query($params);
    }
 
    function access_token($callback_url, $code){
        $params=array(
            &#39;grant_type&#39;=>&#39;authorization_code&#39;,
            &#39;code&#39;=>$code,
            &#39;client_id&#39;=>$this->client_id,
            &#39;client_secret&#39;=>$this->client_secret,
            &#39;redirect_uri&#39;=>$callback_url
        );
        $url=&#39;https://www.douban.com/service/auth2/token&#39;;
        return $this->http($url, http_build_query($params), &#39;POST&#39;);
    }
 
    function access_token_refresh($callback_url, $refresh_token){
        $params=array(
            &#39;grant_type&#39;=>&#39;refresh_token&#39;,
            &#39;refresh_token&#39;=>$refresh_token,
            &#39;client_id&#39;=>$this->client_id,
            &#39;client_secret&#39;=>$this->client_secret,
            &#39;redirect_uri&#39;=>$callback_url
        );
        $url=&#39;https://www.douban.com/service/auth2/token&#39;;
        return $this->http($url, http_build_query($params), &#39;POST&#39;);
    }
 
    function me(){
        $params=array();
        $url=&#39;https://api.douban.com/v2/user/~me&#39;;
        return $this->api($url, $params);
    }
 
    function share($text, $title, $url, $description=&#39;&#39;, $pic=&#39;&#39;){
        $params=array(
            &#39;text&#39;=>$text,
            &#39;rec_title&#39;=>$title,
            &#39;rec_url&#39;=>$url,
            &#39;rec_desc&#39;=>$description,
            &#39;rec_image&#39;=>$pic
        );
        $url=&#39;https://api.douban.com/shuo/v2/statuses/&#39;;
        return $this->api($url, $params, &#39;POST&#39;);
    }
 
    function api($url, $params, $method=&#39;GET&#39;){
        $headers[]="Authorization: Bearer ".$this->access_token;
        if($method==&#39;GET&#39;){
            $result=$this->http($url.&#39;?&#39;.http_build_query($params), &#39;&#39;, &#39;GET&#39;, $headers);
        }else{
            $result=$this->http($url, http_build_query($params), &#39;POST&#39;, $headers);
        }
        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: doubanPHP(piscdong.com)";
        curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ci, CURLOPT_URL, $url);
        $response=curl_exec($ci);
        curl_close($ci);
        $json_r=array();
        if($response!=&#39;&#39;)$json_r=json_decode($response, true);
        return $json_r;
    }
}

8.百度

<?php
/**
 * PHP Library for baidu.com
 *
 * @author PiscDong (http://www.piscdong.com/)
 */
class baiduPHP
{
    function __construct($client_id, $client_secret, $access_token=NULL){
        $this->client_id=$client_id;
        $this->client_secret=$client_secret;
        $this->access_token=$access_token;
    }
 
    function login_url($callback_url, $scope=&#39;&#39;){
        $params=array(
            &#39;response_type&#39;=>&#39;code&#39;,
            &#39;client_id&#39;=>$this->client_id,
            &#39;redirect_uri&#39;=>$callback_url,
            &#39;scope&#39;=>$scope,
            &#39;state&#39;=>md5(time()),
            &#39;display&#39;=>&#39;page&#39;
        );
        return &#39;https://openapi.baidu.com/oauth/2.0/authorize?&#39;.http_build_query($params);
    }
 
    function access_token($callback_url, $code){
        $params=array(
            &#39;grant_type&#39;=>&#39;authorization_code&#39;,
            &#39;code&#39;=>$code,
            &#39;client_id&#39;=>$this->client_id,
            &#39;client_secret&#39;=>$this->client_secret,
            &#39;redirect_uri&#39;=>$callback_url
        );
        $url=&#39;https://openapi.baidu.com/oauth/2.0/token&#39;;
        return $this->http($url, http_build_query($params), &#39;POST&#39;);
    }
 
    function access_token_refresh($refresh_token){
        $params=array(
            &#39;grant_type&#39;=>&#39;refresh_token&#39;,
            &#39;refresh_token&#39;=>$refresh_token,
            &#39;client_id&#39;=>$this->client_id,
            &#39;client_secret&#39;=>$this->client_secret
        );
        $url=&#39;https://openapi.baidu.com/oauth/2.0/token&#39;;
        return $this->http($url, http_build_query($params), &#39;POST&#39;);
    }
 
    function user(){
        $params=array();
        $url=&#39;https://openapi.baidu.com/rest/2.0/passport/users/getLoggedInUser&#39;;
        return $this->api($url, $params);
    }
 
    function api($url, $params, $method=&#39;GET&#39;){
        $params[&#39;access_token&#39;]=$this->access_token;
        if($method==&#39;GET&#39;){
            $result=$this->http($url.&#39;?&#39;.http_build_query($params));
        }else{
            $result=$this->http($url, http_build_query($params), &#39;POST&#39;);
        }
        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: baiduPHP(piscdong.com)";
        curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ci, CURLOPT_URL, $url);
        $response=curl_exec($ci);
        curl_close($ci);
        $json_r=array();
        if($response!=&#39;&#39;)$json_r=json_decode($response, true);
        return $json_r;
    }
}

9.google

<?php
/**
 * PHP Library for google.com
 *
 * @author PiscDong (http://www.piscdong.com/)
 */
class googlePHP
{
    function __construct($client_id, $client_secret, $access_token=NULL){
        $this->client_id=$client_id;
        $this->client_secret=$client_secret;
        $this->access_token=$access_token;
    }
 
    function login_url($callback_url, $scope=&#39;&#39;){
        $params=array(
            &#39;response_type&#39;=>&#39;code&#39;,
            &#39;client_id&#39;=>$this->client_id,
            &#39;redirect_uri&#39;=>$callback_url,
            &#39;scope&#39;=>$scope,
            &#39;state&#39;=>&#39;profile&#39;,
            &#39;access_type&#39;=>&#39;offline&#39;
        );
        return &#39;https://accounts.google.com/o/oauth2/auth?&#39;.http_build_query($params);
    }
 
    function access_token($callback_url, $code){
        $params=array(
            &#39;grant_type&#39;=>&#39;authorization_code&#39;,
            &#39;code&#39;=>$code,
            &#39;client_id&#39;=>$this->client_id,
            &#39;client_secret&#39;=>$this->client_secret,
            &#39;redirect_uri&#39;=>$callback_url
        );
        $url=&#39;https://accounts.google.com/o/oauth2/token&#39;;
        $result=$this->http($url, http_build_query($params), &#39;POST&#39;);
        return $result;
    }
 
    function access_token_refresh($refresh_token){
        $params=array(
            &#39;grant_type&#39;=>&#39;refresh_token&#39;,
            &#39;refresh_token&#39;=>$refresh_token,
            &#39;client_id&#39;=>$this->client_id,
            &#39;client_secret&#39;=>$this->client_secret
        );
        $url=&#39;https://accounts.google.com/o/oauth2/token&#39;;
        $result=$this->http($url, http_build_query($params), &#39;POST&#39;);
        return $result;
    }
 
    function me(){
        $params=array();
        $url=&#39;https://www.googleapis.com/oauth2/v1/userinfo&#39;;
        return $this->api($url, $params);
    }
 
    function api($url, $params, $method=&#39;GET&#39;){
        $headers[]="Authorization: Bearer ".$this->access_token;
        if($method==&#39;GET&#39;){
            $result=$this->http($url.&#39;?&#39;.http_build_query($params), &#39;&#39;, &#39;GET&#39;, $headers);
        }else{
            $result=$this->http($url, http_build_query($params), &#39;POST&#39;, $headers);
        }
        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: google(piscdong.com)";
        curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ci, CURLOPT_URL, $url);
        $response=curl_exec($ci);
        curl_close($ci);
        $json_r=array();
        if($response!=&#39;&#39;)$json_r=json_decode($response, true);
        return $json_r;
    }
}

10微软

<?php
/**
 * PHP Library for live.com
 *
 * @author PiscDong (http://www.piscdong.com/)
 */
class livePHP
{
    function __construct($client_id, $client_secret, $access_token=NULL){
        $this->client_id=$client_id;
        $this->client_secret=$client_secret;
        $this->access_token=$access_token;
    }
 
    function login_url($callback_url, $scope=&#39;&#39;){
        $params=array(
            &#39;response_type&#39;=>&#39;code&#39;,
            &#39;client_id&#39;=>$this->client_id,
            &#39;redirect_uri&#39;=>$callback_url,
            &#39;scope&#39;=>$scope
        );
        return &#39;https://login.live.com/oauth20_authorize.srf?&#39;.http_build_query($params);
    }
 
    function access_token($callback_url, $code){
        $params=array(
            &#39;grant_type&#39;=>&#39;authorization_code&#39;,
            &#39;code&#39;=>$code,
            &#39;client_id&#39;=>$this->client_id,
            &#39;client_secret&#39;=>$this->client_secret,
            &#39;redirect_uri&#39;=>$callback_url
        );
        $url=&#39;https://login.live.com/oauth20_token.srf&#39;;
        return $this->http($url, http_build_query($params), &#39;POST&#39;);
    }
 
    function access_token_refresh($refresh_token){
        $params=array(
            &#39;grant_type&#39;=>&#39;refresh_token&#39;,
            &#39;refresh_token&#39;=>$refresh_token,
            &#39;client_secret&#39;=>$this->client_secret,
            &#39;client_id&#39;=>$this->client_id
        );
        $url=&#39;https://login.live.com/oauth20_token.srf&#39;;
        return $this->http($url, http_build_query($params), &#39;POST&#39;);
    }
 
    function me(){
        $params=array();
        $url=&#39;https://apis.live.net/v5.0/me&#39;;
        return $this->api($url, $params);
    }
 
    function api($url, $params, $method=&#39;GET&#39;){
        $params[&#39;access_token&#39;]=$this->access_token;
        if($method==&#39;GET&#39;){
            $result=$this->http($url.&#39;?&#39;.http_build_query($params));
        }else{
            $result=$this->http($url, http_build_query($params), &#39;POST&#39;);
        }
        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: livePHP(piscdong.com)";
        curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ci, CURLOPT_URL, $url);
        $response=curl_exec($ci);
        curl_close($ci);
        $json_r=array();
        if($response!=&#39;&#39;)$json_r=json_decode($response, true);
        return $json_r;
    }
}

11.instagram

<?php
/**
 * PHP Library for instagram.com
 *
 * @author PiscDong (http://www.piscdong.com/)
 */
class instagramPHP
{
    function __construct($client_id, $client_secret, $access_token=NULL){
        $this->client_id=$client_id;
        $this->client_secret=$client_secret;
        $this->access_token=$access_token;
    }
 
    function login_url($callback_url){
        $params=array(
            &#39;response_type&#39;=>&#39;code&#39;,
            &#39;client_id&#39;=>$this->client_id,
            &#39;redirect_uri&#39;=>$callback_url
        );
        return &#39;https://api.instagram.com/oauth/authorize/?&#39;.http_build_query($params);
    }
 
    function access_token($callback_url, $code){
        $params=array(
            &#39;grant_type&#39;=>&#39;authorization_code&#39;,
            &#39;code&#39;=>$code,
            &#39;client_id&#39;=>$this->client_id,
            &#39;client_secret&#39;=>$this->client_secret,
            &#39;redirect_uri&#39;=>$callback_url
        );
        $url=&#39;https://api.instagram.com/oauth/access_token&#39;;
        return $this->http($url, http_build_query($params), &#39;POST&#39;);
    }
 
    /**
    function access_token_refresh($refresh_token){
    }
    **/
 
    function user($id){
        $params=array();
        $url=&#39;https://api.instagram.com/v1/users/&#39;.$id.&#39;/&#39;;
        return $this->api($url, $params);
    }
 
    function user_media($id, $count=10, $max_id=&#39;&#39;){
        $params=array(
            &#39;count&#39;=>$count
        );
        if($max_id!=&#39;&#39;)$params[&#39;max_id&#39;]=$max_id;
        $url=&#39;https://api.instagram.com/v1/users/&#39;.$id.&#39;/media/recent/&#39;;
        return $this->api($url, $params);
    }
 
    function api($url, $params, $method=&#39;GET&#39;){
        $params[&#39;access_token&#39;]=$this->access_token;
        if($method==&#39;GET&#39;){
            $result=$this->http($url.&#39;?&#39;.http_build_query($params));
        }else{
            $result=$this->http($url, http_build_query($params), &#39;POST&#39;);
        }
        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: instagramPHP(piscdong.com)";
        curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ci, CURLOPT_URL, $url);
        $response=curl_exec($ci);
        curl_close($ci);
        $json_r=array();
        if($response!=&#39;&#39;)$json_r=json_decode($response, true);
        return $json_r;
    }
}

12.facebook

<?php
/**
 * PHP Library for facebook.com
 *
 * @author PiscDong (http://www.piscdong.com/)
 */
class facebookPHP
{
    function __construct($client_id, $client_secret, $access_token=NULL){
        $this->client_id=$client_id;
        $this->client_secret=$client_secret;
        $this->access_token=$access_token;
    }
 
    function login_url($callback_url, $scope=&#39;&#39;){
        $params=array(
            &#39;response_type&#39;=>&#39;code&#39;,
            &#39;client_id&#39;=>$this->client_id,
            &#39;redirect_uri&#39;=>$callback_url,
            &#39;scope&#39;=>$scope
        );
        return &#39;https://graph.facebook.com/oauth/authorize?&#39;.http_build_query($params);
    }
 
    function access_token($callback_url, $code){
        $params=array(
            &#39;grant_type&#39;=>&#39;authorization_code&#39;,
            &#39;code&#39;=>$code,
            &#39;client_id&#39;=>$this->client_id,
            &#39;client_secret&#39;=>$this->client_secret,
            &#39;redirect_uri&#39;=>$callback_url
        );
        $url=&#39;https://graph.facebook.com/oauth/access_token&#39;;
        return $this->http($url, http_build_query($params), &#39;POST&#39;);
    }
 
    /**
    function access_token_refresh($refresh_token){
    }
    **/
 
    function me(){
        $params=array();
        $url=&#39;https://graph.facebook.com/me&#39;;
        return $this->api($url, $params);
    }
 
    function my_feed($count=10, $page=1){
        $params=array(
            &#39;page&#39;=>$page,
            &#39;count&#39;=>$count
        );
        $url=&#39;https://graph.facebook.com/me/feed&#39;;
        return $this->api($url, $params);
    }
 
    function update($content){
        $params=array(
            &#39;message&#39;=>$content
        );
        $url=&#39;https://graph.facebook.com/me/feed/&#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;
        if($method==&#39;GET&#39;){
            $result=$this->http($url.&#39;?&#39;.http_build_query($params));
        }else{
            $result=$this->http($url, http_build_query($params), &#39;POST&#39;);
        }
        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: facebookPHP(piscdong.com)";
        curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ci, CURLOPT_URL, $url);
        $response=curl_exec($ci);
        curl_close($ci);
        $json_r=array();
        if($response!=&#39;&#39;)$json_r=json_decode($response, true);
        return $json_r;
    }
}

13.360

<?php
/**
 * PHP Library for 360.cn
 *
 * @author PiscDong (http://www.piscdong.com/)
 */
class o360PHP
{
    function __construct($client_id, $client_secret, $access_token=NULL){
        $this->client_id=$client_id;
        $this->client_secret=$client_secret;
        $this->access_token=$access_token;
    }
 
    function login_url($callback_url){
        $params=array(
            &#39;response_type&#39;=>&#39;code&#39;,
            &#39;client_id&#39;=>$this->client_id,
            &#39;redirect_uri&#39;=>$callback_url
        );
        return &#39;https://openapi.360.cn/oauth2/authorize?&#39;.http_build_query($params);
    }
 
    function access_token($callback_url, $code){
        $params=array(
            &#39;grant_type&#39;=>&#39;authorization_code&#39;,
            &#39;code&#39;=>$code,
            &#39;client_id&#39;=>$this->client_id,
            &#39;client_secret&#39;=>$this->client_secret,
            &#39;redirect_uri&#39;=>$callback_url
        );
        $url=&#39;https://openapi.360.cn/oauth2/access_token&#39;;
        return $this->http($url, http_build_query($params), &#39;POST&#39;);
    }
 
    function access_token_refresh($refresh_token){
        $params=array(
            &#39;grant_type&#39;=>&#39;refresh_token&#39;,
            &#39;refresh_token&#39;=>$refresh_token,
            &#39;client_id&#39;=>$this->client_id,
            &#39;client_secret&#39;=>$this->client_secret
        );
        $url=&#39;https://openapi.360.cn/oauth2/access_token&#39;;
        return $this->http($url, http_build_query($params), &#39;POST&#39;);
    }
 
    function me(){
        $params=array();
        $url=&#39;https://openapi.360.cn/user/me.json&#39;;
        return $this->api($url, $params);
    }
 
    function api($url, $params, $method=&#39;GET&#39;){
        $params[&#39;access_token&#39;]=$this->access_token;
        if($method==&#39;GET&#39;){
            $result=$this->http($url.&#39;?&#39;.http_build_query($params));
        }else{
            $result=$this->http($url, http_build_query($params), &#39;POST&#39;);
        }
        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: o360PHP(piscdong.com)";
        curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ci, CURLOPT_URL, $url);
        $response=curl_exec($ci);
        curl_close($ci);
        $json_r=array();
        if($response!=&#39;&#39;)$json_r=json_decode($response, true);
        return $json_r;
    }
}

14.github

<?php
/**
 * PHP Library for github.com
 *
 * @author PiscDong (http://www.piscdong.com/)
 */
class githubPHP
{
    function __construct($client_id, $client_secret, $access_token=NULL){
        $this->client_id=$client_id;
        $this->client_secret=$client_secret;
        $this->access_token=$access_token;
    }
 
    function login_url($callback_url, $scope=&#39;&#39;){
        $params=array(
            &#39;client_id&#39;=>$this->client_id,
            &#39;redirect_uri&#39;=>$callback_url,
            &#39;scope&#39;=>$scope
        );
        return &#39;https://github.com/login/oauth/authorize?&#39;.http_build_query($params);
    }
 
    function access_token($callback_url, $code){
        $params=array(
            &#39;code&#39;=>$code,
            &#39;client_id&#39;=>$this->client_id,
            &#39;client_secret&#39;=>$this->client_secret,
            &#39;redirect_uri&#39;=>$callback_url
        );
        $url=&#39;https://github.com/login/oauth/access_token&#39;;
        $result_str=$this->http($url, http_build_query($params), &#39;POST&#39;);
        $json_r=array();
        if($result_str!=&#39;&#39;)parse_str($result_str, $json_r);
        return $json_r;
    }
 
    /**
    function access_token_refresh($refresh_token){
    }
    **/
 
    function me(){
        $params=array();
        $url=&#39;https://api.github.com/user&#39;;
        return $this->api($url, $params);
    }
 
    function api($url, $params, $method=&#39;GET&#39;){
        $params[&#39;access_token&#39;]=$this->access_token;
        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: githubPHP(piscdong.com)";
        curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ci, CURLOPT_URL, $url);
        $response=curl_exec($ci);
        curl_close($ci);
        return $response;
    }
}

15.示例文件,以新浪微博为例,其他平台方法类似

/**
 * 示例文件,以新浪微博为例,其他平台方法类似
 */
 
//生成登录链接
require_once(&#39;sina.php&#39;);
$app_key=&#39;&#39;; //新浪微博应用App Key
$app_secret=&#39;&#39;; //新浪微博应用App Secret
$callback_url=&#39;http://yoururl/sina_callback.php&#39;; //回调网址,请根据自己的实际情况修改
 
$sina=new sinaPHP($app_key, $app_secret);
$login_url=$sina->login_url($callback_url); //生成登录链接,部分平台需要权限,格式请参考各平台api文档
echo &#39;<a href="&#39;.$login_url.&#39;">点击进入授权页面</a>&#39;;
 
 
//授权回调页面,即生成登录链接时的$callback_url
require_once(&#39;sina.php&#39;);
$app_key=&#39;&#39;; //新浪微博应用App Key
$app_secret=&#39;&#39;; //新浪微博应用App Secret
$callback_url=&#39;http://yoururl/sina_callback.php&#39;; //回调网址,必须和生成登录链接时相同
 
if(isset($_GET[&#39;code&#39;]) && $_GET[&#39;code&#39;]!=&#39;&#39;){
    $sina=new sinaPHP($app_key, $app_secret);
    $result=$sina->access_token($callback_url, $_GET[&#39;code&#39;]); //获取access token
    /**
     * $result[&#39;access_token&#39;],用户access token
     * $result[&#39;expires_in&#39;],access token的有效期,单位:秒
     * 部分平台会有$result[&#39;refresh_token&#39;],refresh token,access token到期后使用refresh token生成新的access token
     * 腾讯微博还需要保存$_GET[&#39;openid&#39;]
     */
}
 
 
//用户登录授权后操作api
require_once(&#39;sina.php&#39;);
$app_key=&#39;&#39;; //新浪微博应用App Key
$app_secret=&#39;&#39;; //新浪微博应用App Secret
$access_token=&#39;&#39;; //授权回调页面生成的用户access token
 
$sina=new sinaPHP($app_key, $app_secret, $access_token); //腾讯微博还需要openid,授权回调页面保存的$_GET[&#39;openid&#39;]
 
$sina_uid=$sina->get_uid(); //登录用户uid
//其他功能请参考sina.php自行使用,或者根据api文档自行添加
//其他平台的使用方法和新浪微博类似,各种api的返回数据格式各有不同,请自行参考各平台的api文档
 
 
//支持refresh token的平台在access token到期后请使用access_token_refresh()生成新的access token
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn