新浪微博 的账号登录及api操作,使用oauth 2.0
官方提供的sdk都太过庞大,这是我自己简化的,提供简单的账号登录、获取个人信息、发布微博等功能,如果需要其他功能可以根据官方的api文档自行添加
[文件] sina.php
003 | * PHP Library for weibo.com |
005 | * @author PiscDong (http://www.piscdong.com/) |
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; |
015 | function login_url($callback_url){ |
017 | 'response_type'=>'code', |
018 | 'client_id'=>$this->client_id, |
019 | 'redirect_uri'=>$callback_url |
021 | return 'https://api.weibo.com/oauth2/authorize?'.http_build_query($params); |
024 | function access_token($callback_url, $code){ |
026 | 'grant_type'=>'authorization_code', |
028 | 'client_id'=>$this->client_id, |
029 | 'client_secret'=>$this->client_secret, |
030 | 'redirect_uri'=>$callback_url |
032 | $url='https://api.weibo.com/oauth2/access_token'; |
033 | return $this->http($url, http_build_query($params), 'POST'); |
037 | function access_token_refresh($refresh_token){ |
043 | $url='https://api.weibo.com/2/account/get_uid.json'; |
044 | return $this->api($url, $params); |
047 | function show_user_by_id($uid){ |
051 | $url='https://api.weibo.com/2/users/show.json'; |
052 | return $this->api($url, $params); |
055 | function statuses_count($ids){ |
059 | $url='https://api.weibo.com/2/statuses/count.json'; |
060 | return $this->api($url, $params); |
063 | function get_comments_by_sid($id, $count=10, $page=1){ |
069 | $url='https://api.weibo.com/2/comments/show.json'; |
070 | return $this->api($url, $params); |
073 | function repost_timeline($id, $count=10, $page=1){ |
079 | $url='https://api.weibo.com/2/statuses/repost_timeline.json'; |
080 | return $this->api($url, $params); |
083 | function update($img_c, $pic=''){ |
087 | if($pic!='' && is_array($pic)){ |
088 | $url='https://api.weibo.com/2/statuses/upload.json'; |
091 | $url='https://api.weibo.com/2/statuses/update.json'; |
093 | return $this->api($url, $params, 'POST'); |
096 | function user_timeline($uid, $count=10, $page=1){ |
102 | $url='https://api.weibo.com/2/statuses/user_timeline.json'; |
103 | return $this->api($url, $params); |
106 | function querymid($id, $type=1, $is_batch=0){ |
110 | 'is_batch'=>$is_batch |
112 | $url='https://api.weibo.com/2/statuses/querymid.json'; |
113 | return $this->api($url, $params); |
116 | function api($url, $params, $method='GET'){ |
117 | $params['access_token']=$this->access_token; |
119 | $result=$this->http($url.'?'.http_build_query($params)); |
121 | if(isset($params['pic'])){ |
122 | uksort($params, 'strcmp'); |
123 | $str_b=uniqid('------------------'); |
127 | foreach($params as $k=>$v){ |
134 | $img_c=file_get_contents($url); |
135 | $url_a=explode('?', basename($url)); |
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"; |
143 | $body.=$str_m."\r\n"; |
144 | $body.='Content-Disposition: form-data; name="'.$k."\"\r\n\r\n"; |
149 | $headers[]="Content-Type: multipart/form-data; boundary=".$str_b; |
150 | $result=$this->http($url, $body, 'POST', $headers); |
152 | $result=$this->http($url, http_build_query($params), 'POST'); |
158 | function http($url, $postfields='', $method='GET', $headers=array()){ |
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); |
165 | curl_setopt($ci, CURLOPT_POST, TRUE); |
166 | if($postfields!='')curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields); |
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); |
174 | if($response!='')$json_r=json_decode($response, true); |
[文件] config.php
3 | header('Content-Type: text/html; charset=UTF-8'); |
5 | $sina_k=''; //新浪微博应用App Key |
6 | $sina_s=''; //新浪微博应用App Secret |
7 | $callback_url='http://yoururl/callback.php'; //授权回调网址 |
[文件] index.php
02 | require_once('config.php'); |
03 | require_once('sina.php'); |
07 | $c=@file_get_contents($u); |
09 | $mime='image/unknown'; |
10 | return array($mime, $name, $c); |
13 | $sina_t=isset($_SESSION['sina_t'])?$_SESSION['sina_t']:''; |
17 | $sina=new sinaPHP($sina_k, $sina_s, $sina_t); |
20 | $sina_uid=$c->get_uid(); |
21 | $uid=$sina_uid['uid']; |
24 | $result=$sina->show_user_by_id($uid); |
30 | $img='http://www.baidu.com/img/baidu_sylogo1.gif'; |
33 | $result=$sina->update($content, $img_a); |
36 | $result=$sina->update($content); |
44 | $result=$sina->user_timeline($uid); |
50 | $sina=new sinaPHP($sina_k, $sina_s); |
51 | $login_url=$sina->login_url($callback_url); |
52 | echo '<a href="',$login_url,'">点击进入授权页面</a>'; |
[文件] callback.php
02 | //授权回调页面,即配置文件中的$callback_url |
03 | require_once('config.php'); |
04 | require_once('sina.php'); |
06 | if(isset($_GET['code']) && $_GET['code']!=''){ |
07 | $o=new sinaPHP($sina_k, $sina_s); |
08 | $result=$o->access_token($callback_url, $_GET['code']); |
10 | if(isset($result['access_token']) && $result['access_token']!=''){ |
11 | echo '授权完成,请记录<br/>access token:<input size="50" value="',$result['access_token'],'">'; |
13 | //保存登录信息,此示例中使用session保存 |
14 | $_SESSION['sina_t']=$result['access_token']; //access token |
18 | echo '<br/><a href="./">返回</a>'; |