欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

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

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

renren.php

<?php
/**
* PHP Library for renren.com
*
* @author PiscDong (http://www.piscdong.com/ www.php100.com)
*/
class renrenPHP
{
function __construct($client_id, $client_secret, $access_token=NULL){
$this->client_id=$client_id;
$this->client_secret=$client_secret;
$this->access_token=$access_token;
}
 
function login_url($callback_url, $scope=''){
$params=array(
'response_type'=>'code',
'client_id'=>$this->client_id,
'redirect_uri'=>$callback_url,
'scope'=>$scope
);
return 'https://graph.renren.com/oauth/authorize?'.http_build_query($params);
}
 
function access_token($callback_url, $code){
$params=array(
'grant_type'=>'authorization_code',
'code'=>$code,
'client_id'=>$this->client_id,
'client_secret'=>$this->client_secret,
'redirect_uri'=>$callback_url
);
$url='https://graph.renren.com/oauth/token';
return $this->http($url, http_build_query($params), 'POST');
}
 
function access_token_refresh($refresh_token){
$params=array(
'grant_type'=>'refresh_token',
'refresh_token'=>$refresh_token,
'client_id'=>$this->client_id,
'client_secret'=>$this->client_secret
);
$url='https://graph.renren.com/oauth/token';
return $this->http($url, http_build_query($params), 'POST');
}
 
function me(){
$params=array();
return $this->api('users.getInfo', $params, 'POST');
}
 
function setStatus($status){
$params=array(
'status'=>$status
);
return $this->api('status.set', $params, 'POST');
}
 
function getStatus($uid, $count=10, $page=1){
$params=array(
'uid'=>$uid,
'page'=>$page,
'count'=>$count
);
return $this->api('status.gets', $params, 'POST');
}
 
function addBlog($title, $content){
$params=array(
'title'=>$title,
'content'=>$content
);
return $this->api('blog.addBlog', $params, 'POST');
}
 
function getBlog($id, $uid){
$params=array(
'id'=>$id,
'uid'=>$uid
);
return $this->api('blog.get', $params, 'POST');
}
 
function getComments($id, $uid, $count=10, $page=1){
$params=array(
'id'=>$id,
'uid'=>$uid,
'page'=>$page,
'count'=>$count
);
return $this->api('blog.getComments', $params, 'POST');
}
 
function api($method_name, $params, $method='GET'){
$params['method']=$method_name;
$params['v']='1.0';
$params['access_token']=$this->access_token;
$params['format']='json';
ksort($params);
$sig_str='';
foreach($params as $k=>$v)$sig_str.=$k.'='.$v;
$sig_str.=$this->client_secret;
$sig=md5($sig_str);
$params['sig']=$sig;
$url='http://api.renren.com/restserver.do';
if($method=='GET'){
$result=$this->http($url.'?'.http_build_query($params));
}else{
$result=$this->http($url, http_build_query($params), 'POST');
}
return $result;
}
 
function http($url, $postfields='', $method='GET', $headers=array()){
$ci=curl_init();
curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ci, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ci, CURLOPT_TIMEOUT, 30);
if($method=='POST'){
curl_setopt($ci, CURLOPT_POST, TRUE);
if($postfields!='')curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
}
$headers[]="User-Agent: renrenPHP(piscdong.com)";
curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ci, CURLOPT_URL, $url);
$response=curl_exec($ci);
curl_close($ci);
$json_r=array();
if($response!='')$json_r=json_decode($response, true);
return $json_r;
}
}

config.php

<?php
//配置文件
header('Content-Type: text/html; charset=UTF-8');
 
$renren_k=''; //人人网应用API Key
$renren_s=''; //人人网应用Secret Key
$callback_url='http://yoururl/callback.php'; //授权回调网址
$scope='publish_blog read_user_blog'; //权限列表,具体权限请查看官方的api文档
?>

index.php

<?php
session_start();
require_once('config.php');
require_once('renren.php');
 
$renren_t=isset($_SESSION['renren_t'])?$_SESSION['renren_t']:'';
$renren_id=isset($_SESSION['renren_id'])?$_SESSION['renren_id']:'';
 
//检查是否已登录
if($renren_t!='' || $renren_id!=''){
$renren=new renrenPHP($renren_k, $renren_s, $renren_t);
 
//获取登录用户信息
$result=$renren->me();
var_dump($result);
 
/**
//access token到期后使用refresh token刷新access token

上一篇: linux下配置mysql

下一篇: