|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
学会了PHP,那么学其他的语言,肯定速成,反过来也一样,如果你之前学过其他的语言,那么学PHP肯定快。
大家网的账号登录及api操纵,利用oauth2.0
官方供应的sdk都太甚复杂,这是我本人简化的,供应复杂的账号登录、猎取团体信息、公布微博等功效,假如必要其他功效能够依据官方的api文档自行增加[文件]renren.php
001<?php002/**003*PHPLibraryforrenren.com004*005*@authorPiscDong(http://www.piscdong.com/www.php100.com)006*/007classrenrenPHP008{009function__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}014015functionlogin_url($callback_url,$scope=){016$params=array(017response_type=>code,018client_id=>$this->client_id,019redirect_uri=>$callback_url,020scope=>$scope021);022returnhttps://graph.renren.com/oauth/authorize?.http_build_query($params);023}024025functionaccess_token($callback_url,$code){026$params=array(027grant_type=>authorization_code,028code=>$code,029client_id=>$this->client_id,030client_secret=>$this->client_secret,031redirect_uri=>$callback_url032);033$url=https://graph.renren.com/oauth/token;034return$this->http($url,http_build_query($params),POST);035}036037functionaccess_token_refresh($refresh_token){038$params=array(039grant_type=>refresh_token,040refresh_token=>$refresh_token,041client_id=>$this->client_id,042client_secret=>$this->client_secret043);044$url=https://graph.renren.com/oauth/token;045return$this->http($url,http_build_query($params),POST);046}047048functionme(){049$params=array();050return$this->api(users.getInfo,$params,POST);051}052053functionsetStatus($status){054$params=array(055status=>$status056);057return$this->api(status.set,$params,POST);058}059060functiongetStatus($uid,$count=10,$page=1){061$params=array(062uid=>$uid,063page=>$page,064count=>$count065);066return$this->api(status.gets,$params,POST);067}068069functionaddBlog($title,$content){070$params=array(071title=>$title,072content=>$content073);074return$this->api(blog.addBlog,$params,POST);075}076077functiongetBlog($id,$uid){078$params=array(079id=>$id,080uid=>$uid081);082return$this->api(blog.get,$params,POST);083}084085functiongetComments($id,$uid,$count=10,$page=1){086$params=array(087id=>$id,088uid=>$uid,089page=>$page,090count=>$count091);092return$this->api(blog.getComments,$params,POST);093}094095functionapi($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;100ksort($params);101$sig_str=;102foreach($paramsas$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;107if($method==GET){108$result=$this->http($url.?.http_build_query($params));109}else{110$result=$this->http($url,http_build_query($params),POST);111}112return$result;113}114115functionhttp($url,$postfields=,$method=GET,$headers=array()){116$ci=curl_init();117curl_setopt($ci,CURLOPT_SSL_VERIFYPEER,FALSE);118curl_setopt($ci,CURLOPT_RETURNTRANSFER,1);119curl_setopt($ci,CURLOPT_CONNECTTIMEOUT,30);120curl_setopt($ci,CURLOPT_TIMEOUT,30);121if($method==POST){122curl_setopt($ci,CURLOPT_POST,TRUE);123if($postfields!=)curl_setopt($ci,CURLOPT_POSTFIELDS,$postfields);124}125$headers[]="User-Agent:renrenPHP(piscdong.com)";126curl_setopt($ci,CURLOPT_HTTPHEADER,$headers);127curl_setopt($ci,CURLOPT_URL,$url);128$response=curl_exec($ci);129curl_close($ci);130$json_r=array();131if($response!=)$json_r=json_decode($response,true);132return$json_r;133}134}[文件]config.php
1<?php2//设置文件3header(Content-Type:text/html;charset=UTF-8);45$renren_k=;//大家网使用APIKey6$renren_s=;//大家网使用SecretKey7$callback_url=http://yoururl/callback.php;//受权回调网址8$scope=publish_blogread_user_blog;//权限列表,详细权限请检察官方的api文档9?>[文件]index.php
01<?php02session_start();03require_once(config.php);04require_once(renren.php);0506$renren_t=isset($_SESSION[renren_t])?$_SESSION[renren_t]:;07$renren_id=isset($_SESSION[renren_id])?$_SESSION[renren_id]:;0809//反省是不是已登录10if($renren_t!=$renren_id!=){11$renren=newrenrenPHP($renren_k,$renren_s,$renren_t);1213//猎取登任命户信息14$result=$renren->me();15var_dump($result);1617/**18//accesstoken到期后利用refreshtoken革新accesstoken现在,也有了从事软件工程的想法,在经过了一个月的PHP培训学习之后,发现其实软件工程并没有想像中的那么难。 |
|