关于获取微博登录的代码实现今天尝试使用微博登录的接口,也将使用获取微博登录、QQ登录、手机号登录的接口,用户名登录等支持多项操作的问题微博的界面非常简洁明了,文档也很清楚。OAuth2.0用于请求授权-获取code-使用token获取access_token+uid-使用access_token+uid获取用户信息操作流程如下:1)申请网站访问http://open.weibo.com/connect-立即创建-在应用地址中填写你本地测试的地址即可,其余正常操作2)使用文档操作http://open.weibo.com/wiki/%E...-文档中心http://open.weibo.com/wiki/Co...-微博登录详情http://open.weibo.com/wiki/2/...-获取用户信息接口3)代码实现一没有使用内置的微博phpsdk使用https://github.com/guzzle/guzzle来模拟扩展访问其他支付的请求,我分享了一个配置文件return['log'=>['file'=>storage_path('logs/login/'.date('Y-m-d').'.php')],'weibo'=>[//微博登录相关key'w_key'=>ENV('W_KEY',''),'w_secret'=>ENV('W_SECRET',''),'w_get_code_url'=>'https://api.weibo.com/oauth2/authorize?client_id=%d&response_type=code&redirect_uri=%s','w_get_access_token_url'=>'https://api.weibo.com/oauth2/access_token?client_id=%d&client_secret=%s&grant_type=authorization_code&redirect_uri=%s&code=%s','w_user_url'=>'https://api.weibo.com/2/users/show.json']];相关配置url核心代码通过sprintf拼接如下:Controller代码-namespaceApp\Http\Controllers\Auth;使用App\Http\Traits\LoginWeiboHandler;使用Illuminate\Http\Request;类LoginWeiboController扩展BaseController{使用LoginWeiboHandler;/***微博登录*设置微博登录-获取code-携带code请求accessToken-携带token获取用户信息*/publicfunctionlogin(Request$request){$code=$request->code;如果(!$code){返回$this->getCode();}$result=$this->setGetWbAccessToken($code);$access_token=$result['access_token'];$uid=$result['uid'];返回$this->user($access_token,$uid);//获取用户信息}publicfunctionuser($access_token,$uid){$userInfo=$this->getUserInfo($access_token,$uid);//执行登录操作$this->store($uid,'weibo',$userInfo);}}实际类代码-命名空间App\Http\Traits;使用GuzzleHttp\Client;使用GuzzleHttp\Exception\ClientException;使用App\Exceptions\LoginException;/***处理微博登录收藏*ClassLoginWeiboHandler*@packageApp\Http\Traits*/traitLoginWeiboHandler{private$key;私人秘密;私人$getCodeUrl;私人$getAccessTokenUrl;私人$主机;私人$客户;公共函数__construct(){$this->client=newClient();$this->key=config('login.weibo.w_key');$this->secret=config('login.weibo.w_secret');$this->getCodeUrl=config('login.weibo.w_get_code_url');$this->getAccessTokenUrl=config('login.weibo.w_get_access_token_url');$this->host=route('login.weibo');}/***设置获取代码的url*@returnstring*/publicfunctionsetWbCodeUrl(){$url=sprintf($this->getCodeUrl,$this->key,$this->host);返回$url;}/***@param$codestring授权后获取的code值*/publicfunctionsetGetWbAccessToken($code){if(!$code){thrownewLoginException(['message'=>'CODE不存在']);$url=sprintf($this->getAccessTokenUrl,$this->key,$this->secret,$this->host,$code);试试{$res=$this->client->request('POST',$url)->getBody();}catch(ClientException$e){//处理错误thrownewLoginException(['message'=>'CODEhasexpired']);}返回json_decode($res,true);}/***获取代码*@return\Illuminate\Http\RedirectResponse*/publicfunctiongetCode(){$getCodeUrl=$this->setWbCodeUrl();返回重定向()->离开($getCodeUrl);}/***获取用户信息接口*@param$access_token*@param$uid*@returnmixed*@throwsLoginException*@throws\GuzzleHttp\Exception\GuzzleException*/publicfunctiongetUserInfo($access_token,$uid){$arr=['access_token'=>$access_token,'uid'=>$uid];$url=config('login.weibo.w_user_url').“?”.http_build_query($arr);$res=$this->client->request('GET',$url);试试{$res=$this->client->request('GET',$url)->getBody();}catch(ClientException$e){//处理错误thrownewLoginException(['message'=>'请求微博客户端有问题,请选择更改登录方式']);}返回json_decode($res,true);}}4)代码分析controller代码中,方法getCode是用来登录微博的,会进入请求授权的界面。当你第一次授权或者一直登录的时候,会直接忽略授权页面,直接返回代码。代码中有一个逻辑,一个是唤起登录;就是对code进行处理,再次调用获取access_token+uid。当该代码不存在时,表示需要本次授权请求。使用getCode方法。该方法使用GET请求,会自动返回一串信息。你传递的redirect_uri决定了返回到哪个页面(redirect_uri可以在我的申请-申请信息-高级信息中看到),所以需要使用重定向的方式来获取数据。当代码存在时,使用setGetWbAccessToken方法获取access_token+uid的值,setGetWbAccessToken方法使用post请求。返回是一个json参数,需要自己转义,不会自动重定向,直接返回数据。如果要求报错,很容易出错。期待在user表的设计和多字段登录的方式方法上使用errorcapture。明天寄出,重印。请联系我。唯一原创来自:http://surest.cn/article/46