新浪、qq的账号登录及PHP api操作

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

[文件] sina.php

001
002 /**
003  * PHP Library for weibo.com
004  *
005  * @author PiscDong (http://www.piscdong.com/)
006  */
007 class sinaPHP
008 {
009     function __construct($client_id, $client_secret, $access_token=NULL){
010         $this->client_id=$client_id;
011         $this->client_secret=$client_secret;
012         $this->access_token=$access_token;
013     }
014  
015     function login_url($callback_url){
016         $params=array(
017             'response_type'=>'code',
018             'client_id'=>$this->client_id,
019             'redirect_uri'=>$callback_url
020         );
021         return 'https://api.weibo.com/oauth2/authorize?'.http_build_query($params);
022     }
023  
024     function access_token($callback_url, $code){
025         $params=array(
026             'grant_type'=>'authorization_code',
027             'code'=>$code,
028             'client_id'=>$this->client_id,
029             'client_secret'=>$this->client_secret,
030             'redirect_uri'=>$callback_url
031         );
032         $url='https://api.weibo.com/oauth2/access_token';
033         return $this->http($url, http_build_query($params), 'POST');
034     }
035  
036     /**
037     function access_token_refresh($refresh_token){
038     }
039     **/
040  
041     function get_uid(){
042         $params=array();
043         $url='https://api.weibo.com/2/account/get_uid.json';
044         return $this->api($url, $params);
045     }
046  
047     function show_user_by_id($uid){
048         $params=array(
049             'uid'=>$uid
050         );
051         $url='https://api.weibo.com/2/users/show.json';
052         return $this->api($url, $params);
053     }
054  
055     function statuses_count($ids){
056         $params=array(
057             'ids'=>$ids
058         );
059         $url='https://api.weibo.com/2/statuses/count.json';
060         return $this->api($url, $params);
061     }
062  
063     function get_comments_by_sid($id, $count=10, $page=1){
064         $params=array(
065             'id'=>$id,
066             'page'=>$page,
067             'count'=>$count
068         );
069         $url='https://api.weibo.com/2/comments/show.json';
070         return $this->api($url, $params);
071     }
072  
073     function repost_timeline($id, $count=10, $page=1){
074         $params=array(
075             'id'=>$id,
076             'page'=>$page,
077             'count'=>$count
078         );
079         $url='https://api.weibo.com/2/statuses/repost_timeline.json';
080         return $this->api($url, $params);
081     }
082  
083     function update($img_c, $pic=''){
084         $params=array(
085             'status'=>$img_c
086         );
087         if($pic!='' && is_array($pic)){
088             $url='https://api.weibo.com/2/statuses/upload.json';
089             $params['pic']=$pic;
090         }else{
091             $url='https://api.weibo.com/2/statuses/update.json';
092         }
093         return $this->api($url, $params, 'POST');
094     }
095  
096     function user_timeline($uid, $count=10, $page=1){
097         $params=array(
098             'uid'=>$uid,
099             'page'=>$page,
100             'count'=>$count
101         );
102         $url='https://api.weibo.com/2/statuses/user_timeline.json';
103         return $this->api($url, $params);
104     }
105  
106     function querymid($id, $type=1, $is_batch=0){
107         $params=array(
108             'id'=>$id,
109             'type'=>$type,
110             'is_batch'=>$is_batch
111         );
112         $url='https://api.weibo.com/2/statuses/querymid.json';
113         return $this->api($url, $params);
114     }
115  
116     function api($url, $params, $method='GET'){
117         $params['access_token']=$this->access_token;
118         if($method=='GET'){
119             $result=$this->http($url.'?'.http_build_query($params));
120         }else{
121             if(isset($params['pic'])){
122                 uksort($params, 'strcmp');
123                 $str_b=uniqid('------------------');
124                 $str_m='--'.$str_b;
125                 $str_e=$str_m. '--';
126                 $body='';
127                 foreach($params as $k=>$v){
128                     if($k=='pic'){
129                         if(is_array($v)){
130                             $img_c=$v[2];
131                             $img_n=$v[1];
132                         }elseif($v{0}=='@'){
133                             $url=ltrim($v, '@');
134                             $img_c=file_get_contents($url);
135                             $url_a=explode('?', basename($url));
136                             $img_n=$url_a[0];
137                         }
138                         $body.=$str_m."\r\n";
139                         $body.='Content-Disposition: form-data; name="'.$k.'"; filename="'.$img_n.'"'."\r\n";
140                         $body.="Content-Type: image/unknown\r\n\r\n";
141                         $body.=$img_c."\r\n";
142                     }else{
143                         $body.=$str_m."\r\n";
144                         $body.='Content-Disposition: form-data; name="'.$k."\"\r\n\r\n";
145                         $body.=$v."\r\n";
146                     }
147                 }
148                 $body.=$str_e;
149                 $headers[]="Content-Type: multipart/form-data; boundary=".$str_b;
150                 $result=$this->http($url, $body, 'POST', $headers);
151             }else{
152                 $result=$this->http($url, http_build_query($params), 'POST');
153             }
154         }
155         return $result;
156     }
157  
158     function http($url, $postfields='', $method='GET', $headers=array()){
159         $ci=curl_init();
160         curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE);
161         curl_setopt($ci, CURLOPT_RETURNTRANSFER, 1);
162         curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30);
163         curl_setopt($ci, CURLOPT_TIMEOUT, 30);
164         if($method=='POST'){
165             curl_setopt($ci, CURLOPT_POST, TRUE);
166             if($postfields!='')curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
167         }
168         $headers[]="User-Agent: sinaPHP(piscdong.com)";
169         curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
170         curl_setopt($ci, CURLOPT_URL, $url);
171         $response=curl_exec($ci);
172         curl_close($ci);
173         $json_r=array();
174         if($response!='')$json_r=json_decode($response, true);
175         return $json_r;
176     }
177 }

[文件] config.php 

1
2 //配置文件
3 header('Content-Type: text/html; charset=UTF-8');
4  
5 $sina_k=''; //新浪微博应用App Key
6 $sina_s=''; //新浪微博应用App Secret
7 $callback_url='http://yoururl/callback.php'; //授权回调网址
8 ?>

[文件] index.php

01
02 require_once('config.php');
03 require_once('sina.php');
04  
05 function getimgp($u){
06     //图片处理
07     $c=@file_get_contents($u);
08     $name=md5($u).'.jpg';
09     $mime='image/unknown';
10     return array($mime, $name, $c);
11 }
12  
13 $sina_t=isset($_SESSION['sina_t'])?$_SESSION['sina_t']:'';
14  
15 //检查是否已登录
16 if($sina_t!=''){
17     $sina=new sinaPHP($sina_k, $sina_s, $sina_t);
18  
19     //获取登录用户id
20     $sina_uid=$c->get_uid();
21     $uid=$sina_uid['uid'];
22  
23     //获取登录用户信息
24     $result=$sina->show_user_by_id($uid);
25     var_dump($result);
26  
27     /**
28     //发布微博
29     $content='微博内容';
30     $img='http://www.baidu.com/img/baidu_sylogo1.gif';
31     $img_a=getimgp($img);
32     if($img_a[2]!=''){
33         $result=$sina->update($content, $img_a);
34         //发布带图片微博
35     }else{
36         $result=$sina->update($content);
37         //发布纯文字微博
38     }
39     var_dump($result);
40     **/
41  
42     /**
43     //微博列表
44     $result=$sina->user_timeline($uid);
45     var_dump($result);
46     **/
47  
48 }else{
49     //生成登录链接
50     $sina=new sinaPHP($sina_k, $sina_s);
51     $login_url=$sina->login_url($callback_url);
52     echo '点击进入授权页面';
53 }
54 ?>

[文件] callback.php

01
02 //授权回调页面,即配置文件中的$callback_url
03 require_once('config.php');
04 require_once('sina.php');
05  
06 if(isset($_GET['code']) && $_GET['code']!=''){
07     $o=new sinaPHP($sina_k, $sina_s);
08     $result=$o->access_token($callback_url, $_GET['code']);
09 }
10 if(isset($result['access_token']) && $result['access_token']!=''){
11     echo '授权完成,请记录
access token:';
12  
13     //保存登录信息,此示例中使用session保存
14     $_SESSION['sina_t']=$result['access_token']; //access token
15 }else{
16     echo '授权失败';
17 }
18 echo '
返回';
19 ?>


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

[文件] qq.php

001
002 /**
003  * PHP Library for qq.com
004  *
005  * @author php100(http://www.php100.com/)
006  */
007 class qqPHP
008 {
009     function __construct($appid, $appkey, $access_token=NULL){
010         $this->appid=$appid;
011         $this->appkey=$appkey;
012         $this->access_token=$access_token;
013     }
014  
015     function login_url($callback_url, $scope=''){
016         $params=array(
017             'client_id'=>$this->appid,
018             'redirect_uri'=>$callback_url,
019             'response_type'=>'code',
020             'scope'=>$scope
021         );
022         return 'https://graph.qq.com/oauth2.0/authorize?'.http_build_query($params);
023     }
024  
025     function access_token($callback_url, $code){
026         $params=array(
027             'grant_type'=>'authorization_code',
028             'client_id'=>$this->appid,
029             'client_secret'=>$this->appkey,
030             'code'=>$code,
031             'state'=>'',
032             'redirect_uri'=>$callback_url
033         );
034         $url='https://graph.qq.com/oauth2.0/token?'.http_build_query($params);
035         $result_str=$this->http($url);
036         $json_r=array();
037         if($result_str!='')parse_str($result_str, $json_r);
038         return $json_r;
039     }
040  
041     /**
042     function access_token_refresh($refresh_token){
043     }
044     **/
045  
046     function get_openid(){
047         $params=array(
048             'access_token'=>$this->access_token
049         );
050         $url='https://graph.qq.com/oauth2.0/me?'.http_build_query($params);
051         $result_str=$this->http($url);
052         $json_r=array();
053         if($result_str!=''){
054             preg_match('/callback\(\s+(.*?)\s+\)/i', $result_str, $result_a);
055             $json_r=json_decode($result_a[1], true);
056         }
057         return $json_r;
058     }
059  
060     function get_user_info($openid){
061         $params=array(
062             'openid'=>$openid
063         );
064         $url='https://graph.qq.com/user/get_user_info';
065         return $this->api($url, $params);
066     }
067  
068     function add_share($openid, $title, $url, $site, $fromurl, $images='', $summary=''){
069         $params=array(
070             'openid'=>$openid,
071             'title'=>$title,
072             'url'=>$url,
073             'site'=>$site,
074             'fromurl'=>$fromurl,
075             'images'=>$images,
076             'summary'=>$summary
077         );
078         $url='https://graph.qq.com/share/add_share';
079         return $this->api($url, $params, 'POST');
080     }
081  
082     function api($url, $params, $method='GET'){
083         $params['access_token']=$this->access_token;
084         $params['oauth_consumer_key']=$this->appid;
085         $params['format']='json';
086         if($method=='GET'){
087             $result_str=$this->http($url.'?'.http_build_query($params));
088         }else{
089             $result_str=$this->http($url, http_build_query($params), 'POST');
090         }
091         $result=array();
092         if($result_str!='')$result=json_decode($result_str, true);
093         return $result;
094     }
095  
096     function http($url, $postfields='', $method='GET', $headers=array()){
097         $ci=curl_init();
098         curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE);
099         curl_setopt($ci, CURLOPT_RETURNTRANSFER, 1);
100         curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30);
101         curl_setopt($ci, CURLOPT_TIMEOUT, 30);
102         if($method=='POST'){
103             curl_setopt($ci, CURLOPT_POST, TRUE);
104             if($postfields!='')curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
105         }
106         $headers[]="User-Agent: qqPHP(piscdong.com)";
107         curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
108         curl_setopt($ci, CURLOPT_URL, $url);
109         $response=curl_exec($ci);
110         curl_close($ci);
111         return $response;
112     }
113 }

[文件] config.php 

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

[文件] index.php

01
02 session_start();
03 require_once('config.php');
04 require_once('qq.php');
05  
06 $qq_t=isset($_SESSION['qq_t'])?$_SESSION['qq_t']:'';
07  
08 //检查是否已登录
09 if($qq_t!=''){
10     $qq=new qqPHP($qq_k, $qq_s, $qq_t);
11     $qq_oid=$qq->get_openid();
12     $openid=$qq_oid['openid']; //获取登录用户open id
13  
14     //获取登录用户信息
15     $result=$qq->get_user_info($openid);
16     var_dump($result);
17  
18     /**
19     //发布分享
20     $title='开源中国'; //分享页面标题
21     $url='http://www.oschina.net/'; //分享页面网址
22     $site=''; //QQ应用名称
23     $fromurl='';  //QQ应用网址
24     $result=$qq->add_share($openid, $title, $url, $site, $fromurl);
25     var_dump($result);
26     **/
27  
28 }else{
29     //生成登录链接
30     $qq=new qqPHP($qq_k, $qq_s);
31     $login_url=$qq->login_url($callback_url, $scope);
32     echo '点击进入授权页面';
33 }
34 ?>

[文件] callback.php

01
02 //授权回调页面,即配置文件中的$callback_url
03 session_start();
04 require_once('config.php');
05 require_once('qq.php');
06  
07 if(isset($_GET['code']) && trim($_GET['code'])!=''){
08     $qq=new qqPHP($qq_k, $qq_s);
09     $result=$qq->access_token($callback_url, $_GET['code']);
10 }
11 if(isset($result['access_token']) && $result['access_token']!=''){
12     echo '授权完成,请记录
access token:';
13  
14     //保存登录信息,此示例中使用session保存
15     $_SESSION['qq_t']=$result['access_token']; //access token
16 }else{
17     echo '授权失败';
18 }
19 echo '
返回';
20 ?>

QQ的账号登录及PHP api操作

新浪微博的账号登录及PHP api操作


人人网的账号登录及 PHP api操作


你可能感兴趣的:(php)