|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
聪明的你,显然已经逐渐的开悟了,慢慢的理解了编程的概念,那么祝贺你,你已经迈出了成功的第一步。
新浪微博的账号登录及api操纵,利用oauth2.0
官方供应的sdk都太甚复杂,这是我本人简化的,供应复杂的账号登录、猎取团体信息、公布微博等功效,假如必要其他功效能够依据官方的api文档自行增加[文件]sina.php
001<?php002/**003*PHPLibraryforweibo.com004*005*@authorPiscDong(http://www.piscdong.com/)006*/007classsinaPHP008{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){016$params=array(017response_type=>code,018client_id=>$this->client_id,019redirect_uri=>$callback_url020);021returnhttps://api.weibo.com/oauth2/authorize?.http_build_query($params);022}023024functionaccess_token($callback_url,$code){025$params=array(026grant_type=>authorization_code,027code=>$code,028client_id=>$this->client_id,029client_secret=>$this->client_secret,030redirect_uri=>$callback_url031);032$url=https://api.weibo.com/oauth2/access_token;033return$this->http($url,http_build_query($params),POST);034}035036/**037functionaccess_token_refresh($refresh_token){038}039**/040041functionget_uid(){042$params=array();043$url=https://api.weibo.com/2/account/get_uid.json;044return$this->api($url,$params);045}046047functionshow_user_by_id($uid){048$params=array(049uid=>$uid050);051$url=https://api.weibo.com/2/users/show.json;052return$this->api($url,$params);053}054055functionstatuses_count($ids){056$params=array(057ids=>$ids058);059$url=https://api.weibo.com/2/statuses/count.json;060return$this->api($url,$params);061}062063functionget_comments_by_sid($id,$count=10,$page=1){064$params=array(065id=>$id,066page=>$page,067count=>$count068);069$url=https://api.weibo.com/2/comments/show.json;070return$this->api($url,$params);071}072073functionrepost_timeline($id,$count=10,$page=1){074$params=array(075id=>$id,076page=>$page,077count=>$count078);079$url=https://api.weibo.com/2/statuses/repost_timeline.json;080return$this->api($url,$params);081}082083functionupdate($img_c,$pic=){084$params=array(085status=>$img_c086);087if($pic!=&&is_array($pic)){088$url=https://api.weibo.com/2/statuses/upload.json;089$params[pic]=$pic;090}else{091$url=https://api.weibo.com/2/statuses/update.json;092}093return$this->api($url,$params,POST);094}095096functionuser_timeline($uid,$count=10,$page=1){097$params=array(098uid=>$uid,099page=>$page,100count=>$count101);102$url=https://api.weibo.com/2/statuses/user_timeline.json;103return$this->api($url,$params);104}105106functionquerymid($id,$type=1,$is_batch=0){107$params=array(108id=>$id,109type=>$type,110is_batch=>$is_batch111);112$url=https://api.weibo.com/2/statuses/querymid.json;113return$this->api($url,$params);114}115116functionapi($url,$params,$method=GET){117$params[access_token]=$this->access_token;118if($method==GET){119$result=$this->http($url.?.http_build_query($params));120}else{121if(isset($params[pic])){122uksort($params,strcmp);123$str_b=uniqid(------------------);124$str_m=--.$str_b;125$str_e=$str_m.--;126$body=;127foreach($paramsas$k=>$v){128if($k==pic){129if(is_array($v)){130$img_c=$v[2];131$img_n=$v[1];132}elseif($v{0}==@){133$url=ltrim($v,@);134$img_c=file_get_contents($url);135$url_a=explode(?,basename($url));136$img_n=$url_a[0];137}138$body.=$str_m."
";139$body.=Content-Disposition:form-data;name=".$k.";filename=".$img_n."."
";140$body.="Content-Type:image/unknown
";141$body.=$img_c."
";142}else{143$body.=$str_m."
";144$body.=Content-Disposition:form-data;name=".$k.""
";145$body.=$v."
";146}147}148$body.=$str_e;149$headers[]="Content-Type:multipart/form-data;boundary=".$str_b;150$result=$this->http($url,$body,POST,$headers);151}else{152$result=$this->http($url,http_build_query($params),POST);153}154}155return$result;156}157158functionhttp($url,$postfields=,$method=GET,$headers=array()){159$ci=curl_init();160curl_setopt($ci,CURLOPT_SSL_VERIFYPEER,FALSE);161curl_setopt($ci,CURLOPT_RETURNTRANSFER,1);162curl_setopt($ci,CURLOPT_CONNECTTIMEOUT,30);163curl_setopt($ci,CURLOPT_TIMEOUT,30);164if($method==POST){165curl_setopt($ci,CURLOPT_POST,TRUE);166if($postfields!=)curl_setopt($ci,CURLOPT_POSTFIELDS,$postfields);167}168$headers[]="User-Agent:sinaPHP(piscdong.com)";169curl_setopt($ci,CURLOPT_HTTPHEADER,$headers);170curl_setopt($ci,CURLOPT_URL,$url);171$response=curl_exec($ci);172curl_close($ci);173$json_r=array();174if($response!=)$json_r=json_decode($response,true);175return$json_r;176}177}[文件]config.php
1<?php2//设置文件3header(Content-Type:text/html;charset=UTF-8);45$sina_k=;//新浪微博使用AppKey6$sina_s=;//新浪微博使用AppSecret7$callback_url=http://yoururl/callback.php;//受权回调网址8?>[文件]index.php
01<?php02require_once(config.php);03require_once(sina.php);0405functiongetimgp($u){06//图片处置07$c=@file_get_contents($u);08$name=md5($u)..jpg;09$mime=image/unknown;10returnarray($mime,$name,$c);11}1213$sina_t=isset($_SESSION[sina_t])?$_SESSION[sina_t]:;1415//反省是不是已登录16if($sina_t!=){17$sina=newsinaPHP($sina_k,$sina_s,$sina_t);1819//猎取登任命户id20$sina_uid=$c->get_uid();21$uid=$sina_uid[uid];2223//猎取登任命户信息24$result=$sina->show_user_by_id($uid);25var_dump($result);2627/**28//公布微博29$content=微博内容;30$img=http://www.baidu.com/img/baidu_sylogo1.gif;31$img_a=getimgp($img);32if($img_a[2]!=){33$result=$sina->update($content,$img_a);34//公布带图片微博35}else{36$result=$sina->update($content);37//公布纯笔墨微博38}39var_dump($result);40**/4142/**43//微博列表44$result=$sina->user_timeline($uid);45var_dump($result);46**/4748}else{49//天生登录链接50$sina=newsinaPHP($sina_k,$sina_s);51$login_url=$sina->login_url($callback_url);52echo<ahref=",$login_url,">点击进进受权页面</a>;53}54?>[文件]callback.php
01<?php02//受权回调页面,即设置文件中的$callback_url03require_once(config.php);04require_once(sina.php);0506if(isset($_GET[code])&&$_GET[code]!=){07$o=newsinaPHP($sina_k,$sina_s);08$result=$o->access_token($callback_url,$_GET[code]);09}10if(isset($result[access_token])&&$result[access_token]!=){11echo受权完成,请纪录<br/>accesstoken:<inputsize="50"value=",$result[access_token],">;1213//保留登录信息,此示例中利用session保留14$_SESSION[sina_t]=$result[access_token];//accesstoken15}else{16echo受权失利;17}18echo<br/><ahref="./">前往</a>;19?>phpmanual(PHP手册)肯定是要从网上下载一个的,它很权威,也很全面,我自己认为它是一本很好的参考书,但是不适合新手当教材使用。 |
|