Home > php教程 > PHP源码 > body text

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

PHP中文网
Release: 2016-05-25 17:06:54
Original
1358 people have browsed it

人人网 的账号登录及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=&#39;&#39;){
$params=array(
&#39;response_type&#39;=>&#39;code&#39;,
&#39;client_id&#39;=>$this->client_id,
&#39;redirect_uri&#39;=>$callback_url,
&#39;scope&#39;=>$scope
);
return &#39;https://graph.renren.com/oauth/authorize?&#39;.http_build_query($params);
}
 
function access_token($callback_url, $code){
$params=array(
&#39;grant_type&#39;=>&#39;authorization_code&#39;,
&#39;code&#39;=>$code,
&#39;client_id&#39;=>$this->client_id,
&#39;client_secret&#39;=>$this->client_secret,
&#39;redirect_uri&#39;=>$callback_url
);
$url=&#39;https://graph.renren.com/oauth/token&#39;;
return $this->http($url, http_build_query($params), &#39;POST&#39;);
}
 
function access_token_refresh($refresh_token){
$params=array(
&#39;grant_type&#39;=>&#39;refresh_token&#39;,
&#39;refresh_token&#39;=>$refresh_token,
&#39;client_id&#39;=>$this->client_id,
&#39;client_secret&#39;=>$this->client_secret
);
$url=&#39;https://graph.renren.com/oauth/token&#39;;
return $this->http($url, http_build_query($params), &#39;POST&#39;);
}
 
function me(){
$params=array();
return $this->api(&#39;users.getInfo&#39;, $params, &#39;POST&#39;);
}
 
function setStatus($status){
$params=array(
&#39;status&#39;=>$status
);
return $this->api(&#39;status.set&#39;, $params, &#39;POST&#39;);
}
 
function getStatus($uid, $count=10, $page=1){
$params=array(
&#39;uid&#39;=>$uid,
&#39;page&#39;=>$page,
&#39;count&#39;=>$count
);
return $this->api(&#39;status.gets&#39;, $params, &#39;POST&#39;);
}
 
function addBlog($title, $content){
$params=array(
&#39;title&#39;=>$title,
&#39;content&#39;=>$content
);
return $this->api(&#39;blog.addBlog&#39;, $params, &#39;POST&#39;);
}
 
function getBlog($id, $uid){
$params=array(
&#39;id&#39;=>$id,
&#39;uid&#39;=>$uid
);
return $this->api(&#39;blog.get&#39;, $params, &#39;POST&#39;);
}
 
function getComments($id, $uid, $count=10, $page=1){
$params=array(
&#39;id&#39;=>$id,
&#39;uid&#39;=>$uid,
&#39;page&#39;=>$page,
&#39;count&#39;=>$count
);
return $this->api(&#39;blog.getComments&#39;, $params, &#39;POST&#39;);
}
 
function api($method_name, $params, $method=&#39;GET&#39;){
$params[&#39;method&#39;]=$method_name;
$params[&#39;v&#39;]=&#39;1.0&#39;;
$params[&#39;access_token&#39;]=$this->access_token;
$params[&#39;format&#39;]=&#39;json&#39;;
ksort($params);
$sig_str=&#39;&#39;;
foreach($params as $k=>$v)$sig_str.=$k.&#39;=&#39;.$v;
$sig_str.=$this->client_secret;
$sig=md5($sig_str);
$params[&#39;sig&#39;]=$sig;
$url=&#39;http://api.renren.com/restserver.do&#39;;
if($method==&#39;GET&#39;){
$result=$this->http($url.&#39;?&#39;.http_build_query($params));
}else{
$result=$this->http($url, http_build_query($params), &#39;POST&#39;);
}
return $result;
}
 
function http($url, $postfields=&#39;&#39;, $method=&#39;GET&#39;, $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==&#39;POST&#39;){
curl_setopt($ci, CURLOPT_POST, TRUE);
if($postfields!=&#39;&#39;)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!=&#39;&#39;)$json_r=json_decode($response, true);
return $json_r;
}
}
Copy after login

config.php

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

index.php

<?php
session_start();
require_once(&#39;config.php&#39;);
require_once(&#39;renren.php&#39;);
 
$renren_t=isset($_SESSION[&#39;renren_t&#39;])?$_SESSION[&#39;renren_t&#39;]:&#39;&#39;;
$renren_id=isset($_SESSION[&#39;renren_id&#39;])?$_SESSION[&#39;renren_id&#39;]:&#39;&#39;;
 
//检查是否已登录
if($renren_t!=&#39;&#39; || $renren_id!=&#39;&#39;){
$renren=new renrenPHP($renren_k, $renren_s, $renren_t);
 
//获取登录用户信息
$result=$renren->me();
var_dump($result);
 
/**
//access token到期后使用refresh token刷新access token
Copy after login
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template