|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
咱们就开始学习动态语言的概念吧,刚一接触动态语言,可能很多人都会蒙了,怎么这乱七八糟的工具,在网页里显示的时候却是另外一码事?其实这并不算乱七八糟,你写的HTML代码不也一样是一堆堆的字符吗?毕竟,代码并不是作为直接输出的,而是经过处理的,说白了,HTML是经过HTML解析器,而PHP当然也就通过PHP解析器了,跟学习HTML一样的道理,想让任何的解析器完成操作,就必须使用它们专用的语法结构,所以PHP长相奇怪也就不足为奇了。高兴网的账号登录及api操纵,利用oauth2.0
官方供应的sdk都太甚复杂,这是我本人简化的,供应复杂的账号登录、猎取团体信息、公布纪录等功效,假如必要其他功效能够依据官方的api文档自行增加001<?php002/**003*PHPLibraryforkaixin001.com004*005*@authorPiscDong(http://www.piscdong.com/http://wwww.php100.com)006*/007classkaixinPHP008{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);022returnhttp://api.kaixin001.com/oauth2/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://api.kaixin001.com/oauth2/access_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://api.kaixin001.com/oauth2/access_token;045return$this->http($url,http_build_query($params),POST);046}047048functionme(){049$params=array();050$url=https://api.kaixin001.com/users/me.json;051return$this->api($url,$params);052}053054functionrecords_add($content,$picurl=){055$params=array(056content=>$content057);058if($picurl!=)$params[picurl]=$picurl;059$url=https://api.kaixin001.com/records/add.json;060return$this->api($url,$params,POST);061}062063functionrecords_me($num=10,$start=0){064$params=array(065start=>$start,066num=>$num067);068$url=https://api.kaixin001.com/records/me.json;069return$this->api($url,$params);070}071072functioncomment_list($id,$uid,$num=10,$start=0){073$params=array(074objtype=>records,075objid=>$id,076ouid=>$uid,077start=>$start,078num=>$num079);080$url=https://api.kaixin001.com/comment/list.json;081return$this->api($url,$params);082}083084functionforward_list($id,$uid,$num=10,$start=0){085$params=array(086objtype=>records,087objid=>$id,088ouid=>$uid,089start=>$start,090num=>$num091);092$url=https://api.kaixin001.com/forward/list.json;093return$this->api($url,$params);094}095096functionlike_show($id,$uid,$num=10,$start=0){097$params=array(098objtype=>records,099objid=>$id,100ouid=>$uid,101start=>$start,102num=>$num103);104$url=https://api.kaixin001.com/like/show.json;105return$this->api($url,$params);106}107108functionapi($url,$params,$method=GET){109$params[access_token]=$this->access_token;110if($method==GET){111$result=$this->http($url.?.http_build_query($params));112}else{113$result=$this->http($url,http_build_query($params),POST);114}115return$result;116}117118functionhttp($url,$postfields=,$method=GET,$headers=array()){119$ci=curl_init();120curl_setopt($ci,CURLOPT_SSL_VERIFYPEER,FALSE);121curl_setopt($ci,CURLOPT_RETURNTRANSFER,1);122curl_setopt($ci,CURLOPT_CONNECTTIMEOUT,30);123curl_setopt($ci,CURLOPT_TIMEOUT,30);124if($method==POST){125curl_setopt($ci,CURLOPT_POST,TRUE);126if($postfields!=)curl_setopt($ci,CURLOPT_POSTFIELDS,$postfields);127}128$headers[]="User-Agent:kaixinPHP(piscdong.com)";129curl_setopt($ci,CURLOPT_HTTPHEADER,$headers);130curl_setopt($ci,CURLOPT_URL,$url);131$response=curl_exec($ci);132curl_close($ci);133$json_r=array();134if($response!=)$json_r=json_decode($response,true);135return$json_r;136}137}[文件]config.php
1<?php2//设置文件3header(Content-Type:text/html;charset=UTF-8);45$kaixin_k=;//高兴网使用APIKey6$kaixin_s=;//高兴网使用SecretKey7$callback_url=http://yoururl/callback.php;//受权回调网址8$scope=create_records;//权限列表,详细权限请检察官方的api文档9?>[文件]index.php
01<?php02session_start();03require_once(config.php);04require_once(kaixin.php);0506$kaixin_t=isset($_SESSION[kaixin_t])?$_SESSION[kaixin_t]:;0708//反省是不是已登录0910if($kaixin_t!=){11$kaixin=newkaixinPHP($kaixin_k,$kaixin_s,$kaixin_t);1213//猎取登任命户信息14$result=$kaixin->me();15var_dump($result);1617/**18//accesstoken到期后利用refreshtoken革新accesstoken19$result=$kaixin->access_token_refresh($_SESSION[kaixin_r]);20var_dump($result);21**/2223/**24//公布纪录25$img=http://www.baidu.com/img/baidu_sylogo1.gif;26$result=$kaixin->records_add(纪录内容,$img);27var_dump($result);28**/2930}else{31//天生登录链接32$kaixin=newkaixinPHP($kaixin_k,$kaixin_s);33$login_url=$kaixin->login_url($callback_url,$scope);34echo<ahref=",$login_url,">点击进进受权页面</a>;35}36?>[文件]callback.php
01<?php02//受权回调页面,即设置文件中的$callback_url03session_start();04require_once(config.php);05require_once(kaixin.php);0607if(isset($_GET[code])&&$_GET[code]!=){08$kaixin=newkaixinPHP($kaixin_k,$kaixin_s);09$result=$kaixin->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[kaixin_t]=$result[access_token];//accesstoken16$_SESSION[kaixin_r]=$result[refresh_token];//refreshtoken17}else{18echo受权失利;19}20echo<br/><ahref="./">前往</a>;21?>要想从事软件开发工作,那么,还有很多的知识要学习,其实,不管是以后想去从事哪个工作,都需要自己去利用空闲的时间去不断的学习新的知识,不断的充实自己。 |
|