|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
终于理解了数据库的概念,而且让你兴奋不已的是你终于可以通过PHP来连接数据库了,这期间你是怎么学会的,我们不去考证了,但是事实证明,你已经可以了。
QQ的账号登录及api操纵,利用oauth2.0
官方供应的sdk都太甚复杂,这是我本人简化的,供应复杂的账号登录、猎取团体信息、公布分享等功效,假如必要其他功效能够依据官方的api文档自行增加[文件]qq.php
001<?php002/**003*PHPLibraryforqq.com004*005*@authorphp100(http://www.php100.com/)006*/007classqqPHP008{009function__construct($appid,$appkey,$access_token=NULL){010$this->appid=$appid;011$this->appkey=$appkey;012$this->access_token=$access_token;013}014015functionlogin_url($callback_url,$scope=){016$params=array(017client_id=>$this->appid,018redirect_uri=>$callback_url,019response_type=>code,020scope=>$scope021);022returnhttps://graph.qq.com/oauth2.0/authorize?.http_build_query($params);023}024025functionaccess_token($callback_url,$code){026$params=array(027grant_type=>authorization_code,028client_id=>$this->appid,029client_secret=>$this->appkey,030code=>$code,031state=>,032redirect_uri=>$callback_url033);034$url=https://graph.qq.com/oauth2.0/token?.http_build_query($params);035$result_str=$this->http($url);036$json_r=array();037if($result_str!=)parse_str($result_str,$json_r);038return$json_r;039}040041/**042functionaccess_token_refresh($refresh_token){043}044**/045046functionget_openid(){047$params=array(048access_token=>$this->access_token049);050$url=https://graph.qq.com/oauth2.0/me?.http_build_query($params);051$result_str=$this->http($url);052$json_r=array();053if($result_str!=){054preg_match(/callback(s+(.*?)s+)/i,$result_str,$result_a);055$json_r=json_decode($result_a[1],true);056}057return$json_r;058}059060functionget_user_info($openid){061$params=array(062openid=>$openid063);064$url=https://graph.qq.com/user/get_user_info;065return$this->api($url,$params);066}067068functionadd_share($openid,$title,$url,$site,$fromurl,$images=,$summary=){069$params=array(070openid=>$openid,071title=>$title,072url=>$url,073site=>$site,074fromurl=>$fromurl,075images=>$images,076summary=>$summary077);078$url=https://graph.qq.com/share/add_share;079return$this->api($url,$params,POST);080}081082functionapi($url,$params,$method=GET){083$params[access_token]=$this->access_token;084$params[oauth_consumer_key]=$this->appid;085$params[format]=json;086if($method==GET){087$result_str=$this->http($url.?.http_build_query($params));088}else{089$result_str=$this->http($url,http_build_query($params),POST);090}091$result=array();092if($result_str!=)$result=json_decode($result_str,true);093return$result;094}095096functionhttp($url,$postfields=,$method=GET,$headers=array()){097$ci=curl_init();098curl_setopt($ci,CURLOPT_SSL_VERIFYPEER,FALSE);099curl_setopt($ci,CURLOPT_RETURNTRANSFER,1);100curl_setopt($ci,CURLOPT_CONNECTTIMEOUT,30);101curl_setopt($ci,CURLOPT_TIMEOUT,30);102if($method==POST){103curl_setopt($ci,CURLOPT_POST,TRUE);104if($postfields!=)curl_setopt($ci,CURLOPT_POSTFIELDS,$postfields);105}106$headers[]="User-Agent:qqPHP(piscdong.com)";107curl_setopt($ci,CURLOPT_HTTPHEADER,$headers);108curl_setopt($ci,CURLOPT_URL,$url);109$response=curl_exec($ci);110curl_close($ci);111return$response;112}113}[文件]config.php
1<?php2//设置文件3header(Content-Type:text/html;charset=UTF-8);45$qq_k=;//QQ使用APPID6$qq_s=;//QQ使用APPKEY7$callback_url=http://yoururl/callback.php;//受权回调网址8$scope=get_user_info,add_share;//权限列表,详细权限请检察官方的api文档9?>[文件]index.php
01<?php02session_start();03require_once(config.php);04require_once(qq.php);0506$qq_t=isset($_SESSION[qq_t])?$_SESSION[qq_t]:;0708//反省是不是已登录09if($qq_t!=){10$qq=newqqPHP($qq_k,$qq_s,$qq_t);11$qq_oid=$qq->get_openid();12$openid=$qq_oid[openid];//猎取登任命户openid1314//猎取登任命户信息15$result=$qq->get_user_info($openid);16var_dump($result);1718/**19//公布分享20$title=开源中国;//分享页面题目21$url=http://www.oschina.net/;//分享页面网址22$site=;//QQ使用称号23$fromurl=;//QQ使用网址24$result=$qq->add_share($openid,$title,$url,$site,$fromurl);25var_dump($result);26**/2728}else{29//天生登录链接30$qq=newqqPHP($qq_k,$qq_s);31$login_url=$qq->login_url($callback_url,$scope);32echo<ahref=",$login_url,">点击进进受权页面</a>;33}34?>[文件]callback.php
01<?php02//受权回调页面,即设置文件中的$callback_url03session_start();04require_once(config.php);05require_once(qq.php);0607if(isset($_GET[code])&&trim($_GET[code])!=){08$qq=newqqPHP($qq_k,$qq_s);09$result=$qq->access_token($callback_url,$_GET[code]);10}11if(isset($result[access_token])&&$result[access_token]!=){12echo受权完成,请纪录<br/>accesstoken:<inputsize="50"value=",$result[access_token],">;1314//保留登录信息,此示例中利用session保留15$_SESSION[qq_t]=$result[access_token];//accesstoken16}else{17echo受权失利;18}19echo<br/><ahref="./">前往</a>;20?>可以在书上很方便地做标记,及时记下自己的心得体会。 |
|