|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
《PHP+MYSQLWEB开发(第三版)》号称圣经级,(也许是个不错的选择(声明:作者没给我啥好处费,我也不是书托,隔着大老远,我连他老兄的面都没见过的说-_-)
豆瓣的账号登录及api操纵,利用oauth2.0
官方供应的sdk都太甚复杂,这是我本人简化的,供应复杂的账号登录、猎取团体信息、公布分享等功效,假如必要其他功效能够依据官方的api文档自行增加[文件]douban.php
01<?php02/**03*PHPLibraryfordouban.com04*05*@authorphp100(http://www.php100.com/)06*/07classdoubanPHP08{09function__construct($client_id,$client_secret,$access_token=NULL){10$this->client_id=$client_id;11$this->client_secret=$client_secret;12$this->access_token=$access_token;13}1415functionlogin_url($callback_url,$scope=){16$params=array(17response_type=>code,18client_id=>$this->client_id,19redirect_uri=>$callback_url,20scope=>$scope,21state=>md5(time())22);23returnhttps://www.douban.com/service/auth2/auth?.http_build_query($params);24}2526functionaccess_token($callback_url,$code){27$params=array(28grant_type=>authorization_code,29code=>$code,30client_id=>$this->client_id,31client_secret=>$this->client_secret,32redirect_uri=>$callback_url33);34$url=https://www.douban.com/service/auth2/token;35return$this->http($url,http_build_query($params),POST);36}3738functionaccess_token_refresh($callback_url,$refresh_token){39$params=array(40grant_type=>refresh_token,41refresh_token=>$refresh_token,42client_id=>$this->client_id,43client_secret=>$this->client_secret,44redirect_uri=>$callback_url45);46$url=https://www.douban.com/service/auth2/token;47return$this->http($url,http_build_query($params),POST);48}4950functionme(){51$params=array();52$url=https://api.douban.com/v2/user/~me;53return$this->api($url,$params);54}5556functionshare($text,$title,$url,$description=,$pic=){57$params=array(58text=>$text,59rec_title=>$title,60rec_url=>$url,61rec_desc=>$description,62rec_image=>$pic63);64$url=https://api.douban.com/shuo/v2/statuses/;65return$this->api($url,$params,POST);66}6768functionapi($url,$params,$method=GET){69$headers[]="Authorization:Bearer".$this->access_token;70if($method==GET){71$result=$this->http($url.?.http_build_query($params),,GET,$headers);72}else{73$result=$this->http($url,http_build_query($params),POST,$headers);74}75return$result;76}7778functionhttp($url,$postfields=,$method=GET,$headers=array()){79$ci=curl_init();80curl_setopt($ci,CURLOPT_SSL_VERIFYPEER,FALSE);81curl_setopt($ci,CURLOPT_RETURNTRANSFER,1);82curl_setopt($ci,CURLOPT_CONNECTTIMEOUT,30);83curl_setopt($ci,CURLOPT_TIMEOUT,30);84if($method==POST){85curl_setopt($ci,CURLOPT_POST,TRUE);86if($postfields!=)curl_setopt($ci,CURLOPT_POSTFIELDS,$postfields);87}88$headers[]="User-Agent:doubanPHP(piscdong.com)";89curl_setopt($ci,CURLOPT_HTTPHEADER,$headers);90curl_setopt($ci,CURLOPT_URL,$url);91$response=curl_exec($ci);92curl_close($ci);93$json_r=array();94if($response!=)$json_r=json_decode($response,true);95return$json_r;96}97}[文件]config.php
1<?php2//设置文件3header(Content-Type:text/html;charset=UTF-8);45$douban_k=;//豆瓣使用APIKey6$douban_s=;//豆瓣使用Secret7$callback_url=http://yoururl/callback.php;//受权回调网址8$scope=douban_basic_common,shuo_basic_w;//权限列表,详细权限请检察官方的api文档9?>[文件]index.php
01<?php02session_start();03require_once(config.php);04require_once(douban.php);0506$douban_t=isset($_SESSION[douban_t])?$_SESSION[douban_t]:;0708//反省是不是已登录09if($douban_t!=){10$douban=newdoubanPHP($douban_k,$douban_s,$douban_t);1112//猎取登任命户信息13$result=$douban->me();14var_dump($result);1516/**17//accesstoken到期后利用refreshtoken革新accesstoken18$result=$douban->access_token_refresh($callback_url,$_SESSION[douban_r]);19var_dump($result);20**/2122/**23//公布分享24$text=分享内容;25$title=分享题目;26$url=http://www.oschina.net/;27$result=$douban->share($text,$title,$url);28var_dump($result);29**/3031}else{32//天生登录链接33$douban=newdoubanPHP($douban_k,$douban_s);34$login_url=$douban->login_url($callback_url,$scope);35echo<ahref=",$login_url,">点击进进受权页面</a>;36}37?>[文件]callback.php
01<?php02//受权回调页面,即设置文件中的$callback_url03session_start();04require_once(config.php);05require_once(douban.php);0607if(isset($_GET[code])&&$_GET[code]!=){08$douban=newdoubanPHP($douban_k,$douban_s);09$result=$douban->access_token($callback_url,$_GET[code]);10}11if(isset($result[access_token])&&$result[access_token]!=){12echo受权完成,请纪录<br/>accesstoken:<inputsize="50"value=",$result[access_token],"><br/>refreshtoken:<inputsize="50"value=",$result[refresh_token],">;1314//保留登录信息,此示例中利用session保留15$_SESSION[douban_t]=$result[access_token];//accesstoken16$_SESSION[douban_r]=$result[refresh_token];//refreshtoken17}else{18echo受权失利;19}20echo<br/><ahref="./">前往</a>;21?>在这里想谈谈自己这六个多月的PHP学习心得,希望对给比我还新的新手们有所帮助,讲的不是很深刻,甚至有的想法可能是错误的,希望不要误导新人才好,大家要有自己的主见。 |
|