开心网 的账号登录及api操作,使用oauth 2.0
官方提供的sdk都太过庞大,这是我自己简化的,提供简单的账号登录、获取个人信息、发布记录等功能,如果需要其他功能可以根据官方的api文档自行添加
001 | <?php |
002 | /** |
003 | * PHP Library for kaixin001.com |
004 | * |
005 | * @author PiscDong (http://www.piscdong.com/ http://wwww.php100.com) |
006 | */ |
007 | class kaixinPHP |
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, $scope=''){ |
016 | $params=array( |
017 | 'response_type'=>'code', |
018 | 'client_id'=>$this->client_id, |
019 | 'redirect_uri'=>$callback_url, |
020 | 'scope'=>$scope |
021 | ); |
022 | return 'http://api.kaixin001.com/oauth2/authorize?'.http_build_query($params); |
023 | } |
024 |
025 | function access_token($callback_url, $code){ |
026 | $params=array( |
027 | 'grant_type'=>'authorization_code', |
028 | 'code'=>$code, |
029 | 'client_id'=>$this->client_id, |
030 | 'client_secret'=>$this->client_secret, |
031 | 'redirect_uri'=>$callback_url |
032 | ); |
033 | $url='https://api.kaixin001.com/oauth2/access_token'; |
034 | return $this->http($url, http_build_query($params), 'POST'); |
035 | } |
036 |
037 | function access_token_refresh($refresh_token){ |
038 | $params=array( |
039 | 'grant_type'=>'refresh_token', |
040 | 'refresh_token'=>$refresh_token, |
041 | 'client_id'=>$this->client_id, |
042 | 'client_secret'=>$this->client_secret |
043 | ); |
044 | $url='https://api.kaixin001.com/oauth2/access_token'; |
045 | return $this->http($url, http_build_query($params), 'POST'); |
046 | } |
047 |
048 | function me(){ |
049 | $params=array(); |
050 | $url='https://api.kaixin001.com/users/me.json'; |
051 | return $this->api($url, $params); |
052 | } |
053 |
054 | function records_add($content, $picurl=''){ |
055 | $params=array( |
056 | 'content'=>$content |
057 | ); |
058 | if($picurl!='')$params['picurl']=$picurl; |
059 | $url='https://api.kaixin001.com/records/add.json'; |
060 | return $this->api($url, $params, 'POST'); |
061 | } |
062 |
063 | function records_me($num=10, $start=0){ |
064 | $params=array( |
065 | 'start'=>$start, |
066 | 'num'=>$num |
067 | ); |
068 | $url='https://api.kaixin001.com/records/me.json'; |
069 | return $this->api($url, $params); |
070 | } |
071 |
072 | function comment_list($id, $uid, $num=10, $start=0){ |
073 | $params=array( |
074 | 'objtype'=>'records', |
075 | 'objid'=>$id, |
076 | 'ouid'=>$uid, |
077 | 'start'=>$start, |
078 | 'num'=>$num |
079 | ); |
080 | $url='https://api.kaixin001.com/comment/list.json'; |
081 | return $this->api($url, $params); |
082 | } |
083 |
084 | function forward_list($id, $uid, $num=10, $start=0){ |
085 | $params=array( |
086 | 'objtype'=>'records', |
087 | 'objid'=>$id, |
088 | 'ouid'=>$uid, |
089 | 'start'=>$start, |
090 | 'num'=>$num |
091 | ); |
092 | $url='https://api.kaixin001.com/forward/list.json'; |
093 | return $this->api($url, $params); |
094 | } |
095 |
096 | function like_show($id, $uid, $num=10, $start=0){ |
097 | $params=array( |
098 | 'objtype'=>'records', |
099 | 'objid'=>$id, |
100 | 'ouid'=>$uid, |
101 | 'start'=>$start, |
102 | 'num'=>$num |
103 | ); |
104 | $url='https://api.kaixin001.com/like/show.json'; |
105 | return $this->api($url, $params); |
106 | } |
107 |
108 | function api($url, $params, $method='GET'){ |
109 | $params['access_token']=$this->access_token; |
110 | if($method=='GET'){ |
111 | $result=$this->http($url.'?'.http_build_query($params)); |
112 | }else{ |
113 | $result=$this->http($url, http_build_query($params), 'POST'); |
114 | } |
115 | return $result; |
116 | } |
117 |
118 | function http($url, $postfields='', $method='GET', $headers=array()){ |
119 | $ci=curl_init(); |
120 | curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE); |
121 | curl_setopt($ci, CURLOPT_RETURNTRANSFER, 1); |
122 | curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30); |
123 | curl_setopt($ci, CURLOPT_TIMEOUT, 30); |
124 | if($method=='POST'){ |
125 | curl_setopt($ci, CURLOPT_POST, TRUE); |
126 | if($postfields!='')curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields); |
127 | } |
128 | $headers[]="User-Agent: kaixinPHP(piscdong.com)"; |
129 | curl_setopt($ci, CURLOPT_HTTPHEADER, $headers); |
130 | curl_setopt($ci, CURLOPT_URL, $url); |
131 | $response=curl_exec($ci); |
132 | curl_close($ci); |
133 | $json_r=array(); |
134 | if($response!='')$json_r=json_decode($response, true); |
135 | return $json_r; |
136 | } |
137 | } |
[文件] config.php
1 | <?php |
2 | //配置文件 |
3 | header('Content-Type: text/html; charset=UTF-8'); |
4 |
5 | $kaixin_k=''; //开心网应用API Key |
6 | $kaixin_s=''; //开心网应用Secret Key |
7 | $callback_url='http://yoururl/callback.php'; //授权回调网址 |
8 | $scope='create_records'; //权限列表,具体权限请查看官方的api文档 |
9 | ?> |
[文件] index.php
01 | <?php |
02 | session_start(); |
03 | require_once('config.php'); |
04 | require_once('kaixin.php'); |
05 |
06 | $kaixin_t=isset($_SESSION['kaixin_t'])?$_SESSION['kaixin_t']:''; |
07 |
08 | //检查是否已登录 |
09 |
10 | if($kaixin_t!=''){ |
11 | $kaixin=new kaixinPHP($kaixin_k, $kaixin_s, $kaixin_t); |
12 |
13 | //获取登录用户信息 |
14 | $result=$kaixin->me(); |
15 | var_dump($result); |
16 |
17 | /** |
18 | //access token到期后使用refresh token刷新access token |
19 | $result=$kaixin->access_token_refresh($_SESSION['kaixin_r']); |
20 | var_dump($result); |
21 | **/ |
22 |
23 | /** |
24 | //发布记录 |
25 | $img='baidu_sylogo1.gif'; |
26 | $result=$kaixin->records_add('记录内容', $img); |
27 | var_dump($result); |
28 | **/ |
29 |
30 | }else{ |
31 | //生成登录链接 |
32 | $kaixin=new kaixinPHP($kaixin_k, $kaixin_s); |
33 | $login_url=$kaixin->login_url($callback_url, $scope); |
34 | echo '<a href="',$login_url,'">点击进入授权页面</a>'; |
35 | } |
36 | ?> |
[文件] callback.php
01 | <?php |
02 | //授权回调页面,即配置文件中的$callback_url |
03 | session_start(); |
04 | require_once('config.php'); |
05 | require_once('kaixin.php'); |
06 |
07 | if(isset($_GET['code']) && $_GET['code']!=''){ |
08 | $kaixin=new kaixinPHP($kaixin_k, $kaixin_s); |
09 | $result=$kaixin->access_token($callback_url, $_GET['code']); |
10 | } |
11 | if(isset($result['access_token']) && $result['access_token']!=''){ |
12 | echo '授权完成,请记录<br/>access token:<input size="50" value="',$result['access_token'],'"><br/>refresh token:<input size="50" value="',$result['refresh_token'],'">'; |
13 |
14 | //保存登录信息,此示例中使用session保存 |
15 | $_SESSION['kaixin_t']=$result['access_token']; //access token |
16 | $_SESSION['kaixin_r']=$result['refresh_token']; //refresh token |
17 | }else{ |
18 | echo '授权失败'; |
19 | } |
20 | echo '<br/><a href="./">返回</a>'; |
21 | ?> |
时间: 2024-10-30 02:56:02