当前位置: 首页 > 后端技术 > PHP

一个简单的PHP微信OAuth2.0登录客户端

时间:2023-03-30 04:18:03 PHP

想法最近公司需要接入微信和支付宝三向登录,于是萌生了一个想法,贡献一个开箱即用的OAuth2.0登录客户端为了以后方便使用。基本思想通过这次经历学到了很多,包括git、github的使用、面向对象编程、单元测试等等,感觉受益匪浅。我也意识到整合所有知识并创建一个好的项目并不容易。实现支付宝、微信等OAuth服务器等基本的抽象Provider服务提供者还是需要多练习providerinterface*/interfaceProvider{/***获取授权地址*/publicfunctiongetBaseAuthorizationUrl();/***获取访问令牌地址**@paramarray*/publicfunctiongetBaseAccessTokenUrl(array$params);/***获取用户详情地址**@paramAccessToken访问令牌*/publicfunctiongetResourceOwnerDetailsUrl(AccessToken$token);/***获取默认作用域*/publicfunctiongetDefaultScopes();/***检查提供者响应是否有错误。**@throwsIdentityProviderException*@paramarray$response*@paramarray|string$data解析后的响应数据*@returnvoid*/publicfunctioncheckResponse($response,$data);/***生成用户详情对象**@paramarray$response*@paramAccessessToken$token*@returnResourceOwnerInterface*/publicfunctioncreateResourceOwner(array$response,AccessToken$token);}授权类型先在分区各种不同的授权类型如授权、密码等getName();$provided=array_merge($defaults,$options);$this->checkRequiredParameters($provided);返回$pr提供;}abstractprotectedfunctioncheckRequiredParameters(array$options);}使用redirect.php'你的客户端id','clientSecret'=>'你的客户秘密','redirectUri'=>'redirecturi']);$url=$wechat->getAuthorizationUrl(['scope'=>$this->getDefaultScopes(),'state'=>'state']);header('Location:'.$url);callback.php#微信$wechat=new\CnOAuth\Provider\WechatOfficialAccount(['clientId'=>'你的客户端id','clientSecret'=>'你的客户端密码','redirectUri'=>'重定向uri']);$grant=$wechat->getGrant('授权');if($grant->getCode()){$access_token=$wechat->getAccessToken($grant);$owner=$wechat->getResourceOwner($access_token);print_r($owner->toArray());}else{//用户取消授权}希望有相同想法的可以一起完善,建议项目地址CnOAuth