[文件] renren.php
001 <?php
002 /**
003 * PHP Library for renren.com
004 *
005 * @author PiscDong (http://www.piscdong.com/ www.php100.com)
006 */
007 class renrenPHP
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=''){
017 'response_type'=>'code',
018 'client_id'=>$this->client_id,
019 'redirect_uri'=>$callback_url,
020 'scope'=>$scope
021 );
022 return 'https://graph.renren.com/oauth/authorize?'.http_build_query($params);
023 }
024
025 function access_token($callback_url, $code){
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://graph.renren.com/oauth/token';
035 }
036
037 function access_token_refresh($refresh_token){
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://graph.renren.com/oauth/token';
046 }
047
048 function me(){
050 return $this->api('users.getInfo', $params, 'POST');
051 }
052
053 function setStatus($status){
055 'status'=>$status
056 );
057 return $this->api('status.set', $params, 'POST');
058 }
059
060 function getStatus($uid, $count=10, $page=1){
062 'uid'=>$uid,
063 'page'=>$page,
064 'count'=>$count
065 );
066 return $this->api('status.gets', $params, 'POST');
067 }
068
069 function addBlog($title, $content){
071 'title'=>$title,
072 'content'=>$content
073 );
074 return $this->api('blog.addBlog', $params, 'POST');
075 }
076
077 function getBlog($id, $uid){
079 'id'=>$id,
080 'uid'=>$uid
081 );
082 return $this->api('blog.get', $params, 'POST');
083 }
084
085 function getComments($id, $uid, $count=10, $page=1){
087 'id'=>$id,
088 'uid'=>$uid,
089 'page'=>$page,
090 'count'=>$count
091 );
092 return $this->api('blog.getComments', $params, 'POST');
093 }
094
095 function api($method_name, $params, $method='GET'){
096 $params['method']=$method_name;
097 $params['v']='1.0';
098 $params['access_token']=$this->access_token;
099 $params['format']='json';
101 $sig_str='';
102 foreach($params as $k=>$v)$sig_str.=$k.'='.$v;
103 $sig_str.=$this->client_secret;
105 $params['sig']=$sig;
106 $url='http://api.renren.com/restserver.do';
107 if($method=='GET'){
109 }else{
111 }
112 return $result;
113 }
114
115 function http
($url, $postfields='', $method='GET', $headers=array()){
121 if($method=='POST'){
123 if($postfields!='')curl_setopt($ci, CURLOPT_POSTFIELDS
, $postfields);
124 }
125 $headers[]="User-Agent: renrenPHP(piscdong.com)";
131 if($response!='')$json_r=json_decode($response, true);
132 return $json_r;
133 }
134 }
[文件] config.php
1 <?php
2 //配置文件
3 header('Content-Type: text/html; charset=UTF-8');
4
5 $renren_k=''; //人人网应用API Key
6 $renren_s=''; //人人网应用Secret Key
7 $callback_url='http://yoururl/callback.php'; //授权回调网址
8 $scope='publish_blog read_user_blog'; //权限列表,具体权限请查看官方的api文档
9 ?>
[文件] index.php
01 <?php
03 require_once('config.php');
04 require_once('renren.php');
05
06 $renren_t=isset($_SESSION['renren_t'])?
$_SESSION['renren_t']:'';
07 $renren_id=isset($_SESSION['renren_id'])?
$_SESSION['renren_id']:'';
08
09 //检查是否已登录
10 if($renren_t!='' || $renren_id!=''){
11 $renren=new renrenPHP($renren_k, $renren_s, $renren_t);
12
13 //获取登录用户信息
14 $result=$renren->me();
16
17 /**
18 //access token到期后使用refresh token刷新access token