Heim > php教程 > php手册 > 人人网的账号登录及 PHP api操作

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

WBOY
Freigeben: 2016-06-21 08:50:01
Original
1594 Leute haben es durchsucht

 

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

 

[文件] 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=''){

016 $params=array(

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){

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://graph.renren.com/oauth/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://graph.renren.com/oauth/token';

045 return $this->http($url, http_build_query($params), 'POST');

046 }

047  

048 function me(){

049 $params=array();

050 return $this->api('users.getInfo', $params, 'POST');

051 }

052  

053 function setStatus($status){

054 $params=array(

055 'status'=>$status

056 );

057 return $this->api('status.set', $params, 'POST');

058 }

059  

060 function getStatus($uid, $count=10, $page=1){

061 $params=array(

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){

070 $params=array(

071 'title'=>$title,

072 'content'=>$content

073 );

074 return $this->api('blog.addBlog', $params, 'POST');

075 }

076  

077 function getBlog($id, $uid){

078 $params=array(

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){

086 $params=array(

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';

100 ksort($params);

101 $sig_str='';

102 foreach($params as $k=>$v)$sig_str.=$k.'='.$v;

103 $sig_str.=$this->client_secret;

104 $sig=md5($sig_str);

105 $params['sig']=$sig;

106 $url='http://api.renren.com/restserver.do';

107 if($method=='GET'){

108 $result=$this->http($url.'?'.http_build_query($params));

109 }else{

110 $result=$this->http($url, http_build_query($params), 'POST');

111 }

112 return $result;

113 }

114  

115 function http($url, $postfields='', $method='GET', $headers=array()){

116 $ci=curl_init();

117 curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE);

118 curl_setopt($ci, CURLOPT_RETURNTRANSFER, 1);

119 curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30);

120 curl_setopt($ci, CURLOPT_TIMEOUT, 30);

121 if($method=='POST'){

122 curl_setopt($ci, CURLOPT_POST, TRUE);

123 if($postfields!='')curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);

124 }

125 $headers[]="User-Agent: renrenPHP(piscdong.com)";

126 curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);

127 curl_setopt($ci, CURLOPT_URL, $url);

128 $response=curl_exec($ci);

129 curl_close($ci);

130 $json_r=array();

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

02 session_start();

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();

15 var_dump($result);

16  

17 /**

18 //access token到期后使用refresh token刷新access token

 



Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage