php编程 6个月前(2019-06-25 22:57) 晓峰哥 583 5
第三方登陆插件,网上也有一大堆,我是借用以前项目文件中的WeiboConnect.class.php进行的改造,比用微博的SDK方便得多,希望对大家有用,不多说,直接上代码
一句话简单介绍,改session,改请求方式get,post,其他的请求方式在WeiboConnect.php里面已有,代码如下
<?php namespace app\common\Third; // +---------------------------------------------------------------------- // +---------------------------------------------------------------------- // | Copyright (c) 2018 xf.fengzhiya.cn. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- // | Author: @ Xf 改 // +---------------------------------------------------------------------- class WeiboConnect { private function get_access_token($appkey, $appsecretkey, $code, $callback, $state=null) { $url = "https://api.weibo.com/oauth2/access_token"; $param = array( "grant_type" => "authorization_code", "client_id" => $appkey, "client_secret" => $appsecretkey, "code" => $code, "redirect_uri" => $callback ); $param = http_build_query($param); $response = $this->post_url($url, $param); if($response == false) { return false; } $params = json_decode($response, true); return $params["access_token"]; } private function get_url($url, $param = null) { if($param != null) { $query = http_build_query($param); $url = $url . '?' . $query; } $ch = curl_init(); if(stripos($url, "https://") !== false){ curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); } curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 ); $content = curl_exec($ch); $status = curl_getinfo($ch); curl_close($ch); if(intval($status["http_code"]) == 200) { return $content; }else{ echo $status["http_code"]; return false; } } /* * HTTP POST Request */ private function post_url($url, $params) { $ch = curl_init(); if(stripos($url, "https://") !== false) { curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); } curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $params); $content = curl_exec($ch); $status = curl_getinfo($ch); curl_close($ch); if(intval($status["http_code"]) == 200) { return $content; } else { return false; } } private function get_openid($access_token) { $url = "https://api.weibo.com/oauth2/get_token_info"; $param = array( "access_token" => $access_token ); $param = http_build_query($param); $response = $this->post_url($url, $param); if($response == false) { return false; } $params = json_decode($response, true); return $params['uid']; } public function get_user_info($token, $openid, $appkey=null, $format = "json") { $url = "https://api.weibo.com/2/users/show.json"; $param = array( "access_token" => $token, "uid" => $openid ); $response =$this->get_url($url, $param); if($response == false) { return false; } $user = json_decode($response, true); return $user; } public function login($appkey, $callback, $scope='') { $login_url = "https://api.weibo.com/oauth2/authorize?response_type=code&client_id=" . $appkey . "&scope=$scope&redirect_uri=" . urlencode($callback); header('Location:'.$login_url); } public function callback($appkey, $appsecretkey, $callback) { $code = input('code'); $token = $this->get_access_token($appkey, $appsecretkey, $code, $callback); $openid = $this->get_openid($token); if(!$token || !$openid) { exit('get token or openid error!'); } return array('openid' => $openid, 'token' => $token); } }
1.从前端进入的时候参照上期的QQ第三方登陆,这里只贴图 前端地址http://you domain/index/auth/third/type/weibo.html
2.新浪后台请求的回调 回调地址http://your domain/index/auth/index/type/weibo.html
全部发表完毕 有疑问可以在文章下面给我留言或者加我为好友私聊我
好用么?评论于 kfinder3及其3.x版本 for PHP版本...
评论于 国无人机畅销中东 美媒称"这帮土炮也就是用不起美货...@bulezdf 国庆同乐,假期愉快!
十月一日国庆快乐评论于 国无人机畅销中东 美媒称"这帮土炮也就是用不起美货...
很不错,谢谢功能,借走了评论于 P5.1实现邮件发送功能[简单]...
很棒的视频,合成不错,赞一个评论于 苹果-天涯明月刀 画江湖啊 小视频...
评论列表(共条)