├── .gitignore ├── LICENSE ├── README.md ├── composer.json └── src ├── GetInfo.php ├── Oauth.php ├── config └── config.php └── sdk ├── Baidu.php ├── Dingtalk.php ├── Douyin.php ├── Facebook.php ├── Gitee.php ├── Github.php ├── Google.php ├── Oschina.php ├── Qq.php ├── Sina.php ├── Taobao.php ├── Weixin.php └── Xiaomi.php /.gitignore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/liliuwei/thinkphp-social/2067fc2c2cc3b3d109602bc19c3e5a99c5f4c970/.gitignore -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## thinkphp-social 2 | 适用于thinkphp5.1 thinkphp6.0的社会化登录扩展 3 | 4 | 目前已支持:QQ、微信、新浪、百度、Gitee、Github、Oschina、Google、Facebook、淘宝、抖音、小米、钉钉 5 | 6 | 欢迎提交pr或者建议! 7 | 8 | 有问题可以联系邮箱:974829947@qq.com 9 | 10 | ## 安装(扩展包) 11 | ```php 12 | composer require liliuwei/thinkphp-social 13 | ``` 14 | 15 | # 配置Config信息 16 | ```php 17 | // 安装之后会在config目录里自动生成social.php配置文件 18 | [ 22 | 'app_key' => '*******', //应用注册成功后分配的 APP ID 23 | 'app_secret' => '*******', //应用注册成功后分配的KEY 24 | 'callback' => 'http://www.youquanya.com/oauth/callback/type/qq', // 应用回调地址 25 | ], 26 | //微信扫码登录配置 27 | 'weixin' => [ 28 | 'app_key' => '*******', //应用注册成功后分配的 APP ID 29 | 'app_secret' => '*******', //应用注册成功后分配的KEY 30 | 'callback' => 'http://www.youquanya.com/oauth/callback/type/weixin', // 应用回调地址 31 | ], 32 | ]; 33 | 34 | ``` 35 | 36 | ## 用法示例 37 | ```` 38 | QQ登录 39 | 新浪微博登录 40 | 微信登录 41 | 百度登录 42 | gitee登录 43 | github登录 44 | oschaina登录 45 | google登录 46 | facebook登录 47 | 淘宝登录 48 | 抖音登录 49 | 小米登录 50 | 钉钉登录 51 | ```` 52 | ```php 53 | //设置路由 54 | Route::get('oauth/callback','index/oauth/callback'); 55 | ``` 56 | 57 | ```php 58 | error('参数错误'); 69 | } 70 | // 获取对象实例 71 | $sns = \liliuwei\social\Oauth::getInstance($type); 72 | //跳转到授权页面 73 | $this->redirect($sns->getRequestCodeURL()); 74 | } 75 | 76 | //授权回调地址 77 | public function callback($type = null, $code = null) 78 | { 79 | if ($type == null || $code == null) { 80 | $this->error('参数错误'); 81 | } 82 | $sns = \liliuwei\social\Oauth::getInstance($type); 83 | // 获取TOKEN 84 | $token = $sns->getAccessToken($code); 85 | //获取当前第三方登录用户信息 86 | if (is_array($token)) { 87 | $user_info = \liliuwei\social\GetInfo::getInstance($type, $token); 88 | dump($user_info);// 获取第三方用户资料 89 | $sns->openid();//统一使用$sns->openid()获取openid 90 | //$sns->unionid();//QQ和微信、淘宝可以获取unionid 91 | dump($sns->openid()); 92 | echo '登录成功!!'; 93 | echo '正在持续开发中,敬请期待!!'; 94 | } else { 95 | echo "获取第三方用户的基本信息失败"; 96 | } 97 | } 98 | } 99 | ``` 100 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "liliuwei/thinkphp-social", 3 | "description": "适用于thinkphp5.1和6.0的社会化登录扩展", 4 | "keywords": ["第三方授权登录","QQ登录","微信登录","新浪登录","百度登录","谷歌登录","Google登录","Facebook登录","Gitee登录","Github登录","Oschina登录","淘宝登录","抖音登录","小米登录","钉钉登录","think-social","thinkphp-social","oauth", "thinkphp","thinkphp5.1","thinkphp6.0"], 5 | "license": "Apache-2.0", 6 | "type": "think-extend", 7 | "require": { 8 | "php": ">=5.6.0", 9 | "topthink/think-installer": "^2.0", 10 | "topthink/framework": "^5.1 || ^6.0" 11 | }, 12 | "authors": [ 13 | { 14 | "name": "liliuwei", 15 | "email": "974829947@qq.com" 16 | } 17 | ], 18 | "autoload" : { 19 | "psr-4" : { 20 | "liliuwei\\social\\" : "src/" 21 | } 22 | }, 23 | "extra": { 24 | "think-config": { 25 | "social": "src/config/config.php" 26 | }, 27 | "think": { 28 | "config": { 29 | "social": "src/config/config.php" 30 | } 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/GetInfo.php: -------------------------------------------------------------------------------- 1 | call('user/get_user_info'); 19 | if ($data['ret'] == 0) { 20 | $userInfo['type'] = 'qq'; 21 | $userInfo['name'] = $data['nickname']; 22 | $userInfo['nickname'] = $data['nickname']; 23 | $userInfo['avatar'] = $data['figureurl_2']; 24 | $userInfo['gender'] = $data['gender']; 25 | return $userInfo; 26 | } else { 27 | throw new \Exception("获取腾讯QQ用户信息失败:{$data['msg']}"); 28 | } 29 | } 30 | 31 | //微信用户信息 32 | public static function weixin($token) 33 | { 34 | $weixin = Oauth::getInstance('weixin', $token); 35 | $data = $weixin->call('sns/userinfo'); 36 | if (isset($data['errcode'])) { 37 | throw new \Exception("获取微信用户信息失败:errcode:{$data['errcode']} errmsg: {$data['errmsg']}"); 38 | } 39 | if ($data['openid']) { 40 | $userInfo['type'] = 'weixin'; 41 | $userInfo['name'] = $data['nickname']; 42 | $userInfo['nickname'] = $data['nickname']; 43 | $userInfo['avatar'] = $data['headimgurl']; 44 | $userInfo['openid'] = $data['openid']; 45 | $userInfo['unionid'] = $data['unionid']; 46 | $userInfo['province'] = $data['province']; 47 | $userInfo['city'] = $data['city']; 48 | $userInfo['country'] = $data['country']; 49 | $userInfo['sex'] = $data['sex']==1?'男':'女'; 50 | return $userInfo; 51 | } else { 52 | throw new \Exception("获取微信用户信息失败"); 53 | } 54 | } 55 | 56 | //新浪微博用户信息 57 | public static function sina($token) 58 | { 59 | $sina = Oauth::getInstance('sina', $token); 60 | $data = $sina->call('users/show',"uid={$sina->openid()}"); 61 | if ($data['id']) { 62 | $userInfo['type'] = 'sina'; 63 | $userInfo['name'] = $data['name']; 64 | $userInfo['nickname'] = $data['screen_name']; 65 | $userInfo['avatar'] = $data['avatar_large']; 66 | $userInfo['openid'] = $data['id']; 67 | $userInfo['idstr'] = $data['idstr']; 68 | $userInfo['province'] = $data['province']; 69 | $userInfo['city'] = $data['city']; 70 | $userInfo['location'] = $data['location']; 71 | $userInfo['created_at'] = $data['created_at']; 72 | $userInfo['gender'] = $data['gender']=='m'?'男':($data['gender']=='f'?'女':'未知'); 73 | $userInfo['followers_count'] = $data['followers_count']; 74 | $userInfo['friends_count'] = $data['friends_count']; 75 | $userInfo['statuses_count'] = $data['statuses_count']; 76 | $userInfo['favourites_count'] = $data['favourites_count']; 77 | $userInfo['description'] = $data['description']; 78 | $userInfo['url'] = $data['url']; 79 | $userInfo['profile_url'] = $data['profile_url']; 80 | return $userInfo; 81 | } else { 82 | throw new \Exception("获取新浪微博用户信息失败:{$data['error']}"); 83 | } 84 | } 85 | 86 | //Baidu用户信息 87 | public static function baidu($token) 88 | { 89 | $baidu = Oauth::getInstance('baidu', $token); 90 | $data = $baidu->call('passport/users/getInfo'); 91 | if (isset($data['userid'])) { 92 | $userInfo['type'] = 'baidu'; 93 | $userInfo['name'] = $data['username']; 94 | $userInfo['nickname'] = isset($data['realname'])?$data['realname']:''; 95 | $userInfo['avatar'] = 'http://tb.himg.baidu.com/sys/portrait/item/'.$data['portrait']; 96 | $userInfo['openid'] = $data['userid']; 97 | $userInfo['sex'] = $data['sex']==1?'男':'女'; 98 | return $userInfo; 99 | } else { 100 | throw new \Exception("获取Baidu用户信息失败"); 101 | } 102 | } 103 | 104 | //Gitee用户信息 105 | public static function gitee($token) 106 | { 107 | $google = Oauth::getInstance('gitee', $token); 108 | $data = $google->call('user'); 109 | if (isset($data['id'])) { 110 | $userInfo['type'] = 'gitee'; 111 | $userInfo['name'] = $data['name']; 112 | $userInfo['nickname'] = $data['login']; 113 | $userInfo['avatar'] = $data['avatar_url']; 114 | $userInfo['openid'] = $data['id']; 115 | $userInfo['html_url'] = $data['html_url']; 116 | $userInfo['blog'] = $data['blog']; 117 | $userInfo['email'] = $data['email']; 118 | return $userInfo; 119 | } else { 120 | throw new \Exception("获取Gitee用户信息失败"); 121 | } 122 | } 123 | 124 | //Github用户信息 125 | public static function github($token) 126 | { 127 | $google = Oauth::getInstance('github', $token); 128 | $data = $google->call('user'); 129 | if (isset($data['id'])) { 130 | $userInfo['type'] = 'github'; 131 | $userInfo['name'] = $data['name']; 132 | $userInfo['nickname'] = $data['login']; 133 | $userInfo['avatar'] = $data['avatar_url']; 134 | $userInfo['openid'] = $data['id']; 135 | $userInfo['html_url'] = $data['html_url']; 136 | $userInfo['blog'] = $data['blog']; 137 | $userInfo['email'] = $data['email']; 138 | return $userInfo; 139 | } else { 140 | throw new \Exception("获取Gitee用户信息失败"); 141 | } 142 | } 143 | 144 | //Google用户信息 145 | public static function google($token) 146 | { 147 | $google = Oauth::getInstance('google', $token); 148 | $data = $google->call('userinfo'); 149 | if (isset($data['id'])) { 150 | $userInfo['type'] = 'google'; 151 | $userInfo['name'] = $data['name']; 152 | $userInfo['nickname'] = $data['name']; 153 | $userInfo['avatar'] = $data['picture']; 154 | $userInfo['openid'] = $data['id']; 155 | $userInfo['given_name'] = $data['given_name']; 156 | $userInfo['family_name'] = $data['family_name']; 157 | $userInfo['locale'] = $data['locale']; 158 | $userInfo['email'] = $data['email']; 159 | return $userInfo; 160 | } else { 161 | throw new \Exception("获取Google用户信息失败"); 162 | } 163 | } 164 | 165 | //Facebook用户信息 166 | public static function facebook($token) 167 | { 168 | $facebook = Oauth::getInstance('facebook', $token); 169 | $data = $facebook->call('me', 'fields=name,picture,first_name,last_name,short_name,email'); 170 | if (isset($data['id'])) { 171 | $userInfo['type'] = 'facebook'; 172 | $userInfo['name'] = $data['name']; 173 | $userInfo['nickname'] = $data['name']; 174 | $userInfo['avatar'] = $data['picture']['data']['url']; 175 | $userInfo['openid'] = $data['id']; 176 | $userInfo['first_name'] = $data['first_name']; 177 | $userInfo['last_name'] = $data['last_name']; 178 | $userInfo['short_name'] = $data['short_name']; 179 | $userInfo['email'] = $data['email']; 180 | return $userInfo; 181 | } else { 182 | throw new \Exception("获取Facebook用户信息失败"); 183 | } 184 | } 185 | 186 | //Oschina用户信息 187 | public static function oschina($token) 188 | { 189 | $oschina = Oauth::getInstance('oschina', $token); 190 | $data = $oschina->call('action/openapi/user'); 191 | if (isset($data['id'])) { 192 | $userInfo['type'] = 'gitee'; 193 | $userInfo['name'] = $data['name']; 194 | $userInfo['nickname'] = $data['name']; 195 | $userInfo['avatar'] = $data['avatar']; 196 | $userInfo['openid'] = $data['id']; 197 | $userInfo['email'] = $data['email']; 198 | $userInfo['location'] = $data['location']; 199 | return $userInfo; 200 | } else { 201 | throw new \Exception("获取Gitee用户信息失败"); 202 | } 203 | } 204 | 205 | //Taobao用户信息 206 | public static function taobao($token) 207 | { 208 | $data = $token; 209 | if (isset($data['taobao_user_id'])) { 210 | $userInfo['type'] = 'taobao'; 211 | $userInfo['name'] = urldecode($data['taobao_user_nick']); 212 | $userInfo['nickname'] = urldecode($data['taobao_user_nick']); 213 | $userInfo['avatar'] = ''; 214 | $userInfo['openid'] = $data['taobao_user_id']; 215 | $userInfo['taobao_open_uid'] = $data['taobao_open_uid']; 216 | return $userInfo; 217 | } else { 218 | throw new \Exception("获取淘宝用户信息失败"); 219 | } 220 | } 221 | 222 | //Douyin用户信息 223 | public static function douyin($token) 224 | { 225 | $douyin = Oauth::getInstance('douyin', $token); 226 | $data = $douyin->call('oauth/userinfo'); 227 | $data = $data['data']; 228 | if (isset($data['open_id'])) { 229 | $userInfo['type'] = 'douyin'; 230 | $userInfo['name'] = $data['nickname']; 231 | $userInfo['nickname'] = $data['nickname']; 232 | $userInfo['avatar'] = $data['avatar']; 233 | $userInfo['openid'] = $data['open_id']; 234 | $userInfo['unionid'] = $data['union_id']; 235 | $userInfo['gender'] = $data['sex']==1?'男':'女'; 236 | $userInfo['city'] = $data['city']; 237 | $userInfo['province'] = $data['province']; 238 | $userInfo['country'] = $data['country']; 239 | return $userInfo; 240 | } else { 241 | throw new \Exception("获取抖音用户信息失败"); 242 | } 243 | } 244 | 245 | //Xiaomi用户信息 246 | public static function xiaomi($token) 247 | { 248 | $xiaomi = Oauth::getInstance('xiaomi', $token); 249 | $data = $xiaomi->call('user/profile'); 250 | $data = $data['data']; 251 | if (isset($data['unionId'])) { 252 | $userInfo['type'] = 'xiaomi'; 253 | $userInfo['name'] = $data['miliaoNick']; 254 | $userInfo['nickname'] = $data['miliaoNick']; 255 | $userInfo['avatar'] = $data['miliaoIcon']; 256 | $userInfo['openid'] = array_key_exists('userId',$data)?$data['userId']:$data['unionId']; 257 | $userInfo['unionid'] = $data['unionId']; 258 | return $userInfo; 259 | } else { 260 | throw new \Exception("获取小米用户信息失败"); 261 | } 262 | } 263 | 264 | //Dingtalk用户信息 265 | public static function dingtalk($token) 266 | { 267 | $data = $token; 268 | if ($data['openid']) { 269 | $userInfo['type'] = 'dingtalk'; 270 | $userInfo['name'] = $data['nick']; 271 | $userInfo['nickname'] = $data['nick']; 272 | $userInfo['avatar'] = ''; 273 | $userInfo['openid'] = $data['openid']; 274 | $userInfo['unionid'] = $data['unionid']; 275 | return $userInfo; 276 | } else { 277 | throw new \Exception("获取钉钉用户信息失败"); 278 | } 279 | } 280 | } -------------------------------------------------------------------------------- /src/Oauth.php: -------------------------------------------------------------------------------- 1 | Type = strtolower($re_str); 92 | 93 | //获取应用配置 94 | $config = config("social.{$this->Type}"); 95 | if (empty($config['app_key']) || empty($config['app_secret'])) { 96 | throw new \Exception('请配置您申请的app_key和app_secret'); 97 | } else { 98 | $this->AppKey = $config['app_key']; 99 | $this->AppSecret = $config['app_secret']; 100 | $this->Token = $token; //设置获取到的TOKEN 101 | } 102 | } 103 | 104 | /** 105 | * 取得Oauth实例 106 | * @static 107 | * @return mixed 返回Oauth 108 | */ 109 | public static function getInstance($type, $token = null) 110 | { 111 | $sdk = [ 112 | 'Qq', 'Weixin', 'Sina', 'Baidu', 'Gitee', 'Github', 'Google', 'Facebook', 'Taobao', 'Oschina', 'Douyin', 'Xiaomi', 'Dingtalk' 113 | ]; 114 | $name = ucfirst(strtolower($type)); 115 | if (in_array($name, $sdk)) { 116 | $class = '\\liliuwei\\social\\sdk\\' . $name; 117 | return new $class($token); 118 | } else { 119 | header('content-type:text/html;charset=utf-8'); 120 | throw new \Exception('暂时还不支持该' . $name . '的扩展'); 121 | } 122 | } 123 | 124 | /** 125 | * 初始化配置 126 | */ 127 | protected function config() 128 | { 129 | $config = config("social.{$this->Type}"); 130 | if (!empty($config['authorize'])) 131 | $this->Authorize = $config['authorize']; 132 | if (!empty($config['callback'])) 133 | $this->Callback = $config['callback']; 134 | else 135 | throw new \Exception('请配置回调页面地址'); 136 | } 137 | 138 | /** 139 | * 请求code 140 | */ 141 | public function getRequestCodeURL() 142 | { 143 | $this->config(); 144 | //Oauth 标准参数 145 | $params = array( 146 | 'client_id' => $this->AppKey, 147 | 'redirect_uri' => $this->Callback, 148 | 'response_type' => $this->ResponseType, 149 | ); 150 | 151 | //获取额外参数 152 | if ($this->Authorize) { 153 | parse_str($this->Authorize, $_param); 154 | if (is_array($_param)) { 155 | $params = array_merge($params, $_param); 156 | } else { 157 | throw new \Exception('AUTHORIZE配置不正确!'); 158 | } 159 | } 160 | return $this->GetRequestCodeURL . '?' . http_build_query($params); 161 | } 162 | 163 | /** 164 | * 获取access_token 165 | * @param string $code 上一步请求到的code 166 | */ 167 | public function getAccessToken($code, $extend = null) 168 | { 169 | $this->config(); 170 | $params = array( 171 | 'client_id' => $this->AppKey, 172 | 'client_secret' => $this->AppSecret, 173 | 'grant_type' => $this->GrantType, 174 | 'code' => $code, 175 | 'redirect_uri' => $this->Callback, 176 | ); 177 | 178 | $data = $this->http($this->GetAccessTokenURL, $params, 'POST'); 179 | $this->Token = $this->parseToken($data, $extend); 180 | return $this->Token; 181 | } 182 | 183 | /** 184 | * 合并默认参数和额外参数 185 | * @param array $params 默认参数 186 | * @param array/string $param 额外参数 187 | * @return array: 188 | */ 189 | protected function param($params, $param) 190 | { 191 | if (is_string($param)) 192 | parse_str($param, $param); 193 | return array_merge($params, $param); 194 | } 195 | 196 | /** 197 | * 获取指定API请求的URL 198 | * @param string $api API名称 199 | * @param string $fix api后缀 200 | * @return string 请求的完整URL 201 | */ 202 | protected function url($api = '', $fix = '') 203 | { 204 | return $this->ApiBase . $api . $fix; 205 | } 206 | 207 | /** 208 | * 发送HTTP请求方法,目前只支持CURL发送请求 209 | * @param string $url 请求URL 210 | * @param array $params 请求参数 211 | * @param string $method 请求方法GET/POST 212 | * @return array $data 响应数据 213 | */ 214 | protected function http($url, $params, $method = 'GET', $header = array(), $multi = false) 215 | { 216 | $opts = array( 217 | CURLOPT_TIMEOUT => 30, 218 | CURLOPT_RETURNTRANSFER => 1, 219 | CURLOPT_SSL_VERIFYPEER => false, 220 | CURLOPT_SSL_VERIFYHOST => false, 221 | CURLOPT_HTTPHEADER => $header 222 | ); 223 | 224 | /* 根据请求类型设置特定参数 */ 225 | switch (strtoupper($method)) { 226 | case 'GET': 227 | $opts[CURLOPT_URL] = $url . '?' . http_build_query($params); 228 | break; 229 | case 'POST': 230 | //判断是否传输文件 231 | $params = $multi ? $params : http_build_query($params); 232 | $opts[CURLOPT_URL] = $url; 233 | $opts[CURLOPT_POST] = 1; 234 | $opts[CURLOPT_POSTFIELDS] = $params; 235 | break; 236 | default: 237 | throw new \Exception('不支持的请求方式!'); 238 | } 239 | 240 | /* 初始化并执行curl请求 */ 241 | $ch = curl_init(); 242 | curl_setopt_array($ch, $opts); 243 | $data = curl_exec($ch); 244 | $error = curl_error($ch); 245 | curl_close($ch); 246 | if ($error) throw new \Exception('请求发生错误:' . $error); 247 | return $data; 248 | } 249 | 250 | /** 251 | * 抽象方法,在SNSSDK中实现 252 | * 组装接口调用参数 并调用接口 253 | */ 254 | abstract protected function call($api, $param = '', $method = 'GET', $multi = false); 255 | 256 | /** 257 | * 抽象方法,在SNSSDK中实现 258 | * 解析access_token方法请求后的返回值 259 | */ 260 | abstract protected function parseToken($result, $extend); 261 | 262 | /** 263 | * 抽象方法,在SNSSDK中实现 264 | * 获取当前授权用户的SNS标识 265 | */ 266 | abstract public function openid(); 267 | } -------------------------------------------------------------------------------- /src/config/config.php: -------------------------------------------------------------------------------- 1 | [ 9 | 'app_key' => '', //应用注册成功后分配的 APP ID 10 | 'app_secret' => '', //应用注册成功后分配的KEY 11 | 'callback' => '', // 应用回调地址 12 | ], 13 | //微信扫码登录配置 14 | 'weixin' => [ 15 | 'app_key' => '', //应用注册成功后分配的 APP ID 16 | 'app_secret' => '', //应用注册成功后分配的KEY 17 | 'callback' => '', // 应用回调地址 18 | ], 19 | //新浪登录配置 20 | 'sina' => [ 21 | 'app_key' => '', //应用注册成功后分配的 APP ID 22 | 'app_secret' => '', //应用注册成功后分配的KEY 23 | 'callback' => '', // 应用回调地址 24 | ], 25 | //Baidu登录配置 26 | 'baidu' => [ 27 | 'app_key' => '', //应用注册成功后分配的 APP ID 28 | 'app_secret' => '', //应用注册成功后分配的KEY 29 | 'callback' => '', // 应用回调地址 30 | ], 31 | //Gitee登录配置 32 | 'gitee' => [ 33 | 'app_key' => '', //应用注册成功后分配的 APP ID 34 | 'app_secret' => '', //应用注册成功后分配的KEY 35 | 'callback' => '', // 应用回调地址 36 | ], 37 | //Github登录配置 38 | 'github' => [ 39 | 'app_key' => '', //应用注册成功后分配的 APP ID 40 | 'app_secret' => '', //应用注册成功后分配的KEY 41 | 'callback' => '', // 应用回调地址 42 | ], 43 | //Google登录配置 44 | 'google' => [ 45 | 'app_key' => '', //应用注册成功后分配的 APP ID 46 | 'app_secret' => '', //应用注册成功后分配的KEY 47 | 'callback' => '', // 应用回调地址 48 | ], 49 | //Facebook登录配置 50 | 'facebook' => [ 51 | 'app_key' => '', //应用注册成功后分配的 APP ID 52 | 'app_secret' => '', //应用注册成功后分配的KEY 53 | 'callback' => '', // 应用回调地址 54 | ], 55 | //Oschina登录配置 56 | 'oschina' => [ 57 | 'app_key' => '', //应用注册成功后分配的 APP ID 58 | 'app_secret' => '', //应用注册成功后分配的KEY 59 | 'callback' => '', // 应用回调地址 60 | ], 61 | //Taobao登录配置 62 | 'taobao' => [ 63 | 'app_key' => '', //应用注册成功后分配的 APP ID 64 | 'app_secret' => '', //应用注册成功后分配的KEY 65 | 'callback' => '', // 应用回调地址 66 | ], 67 | //Douyin登录配置 68 | 'douyin' => [ 69 | 'app_key' => '', //应用注册成功后分配的 APP ID 70 | 'app_secret' => '', //应用注册成功后分配的KEY 71 | 'callback' => '', // 应用回调地址 72 | ], 73 | //Xiaomi登录配置 74 | 'xiaomi' => [ 75 | 'app_key' => '', //应用注册成功后分配的 APP ID 76 | 'app_secret' => '', //应用注册成功后分配的KEY 77 | 'callback' => '', // 应用回调地址 78 | ], 79 | //Dingtalk登录配置 80 | 'dingtalk' => [ 81 | 'app_key' => '', //应用注册成功后分配的 APP ID 82 | 'app_secret' => '', //应用注册成功后分配的KEY 83 | 'callback' => '', // 应用回调地址 84 | ] 85 | ]; 86 | -------------------------------------------------------------------------------- /src/sdk/Baidu.php: -------------------------------------------------------------------------------- 1 | $this->Token['access_token'], 42 | ); 43 | $data = $this->http($this->url($api), $this->param($params, $param), $method); 44 | return json_decode($data, true); 45 | } 46 | 47 | /** 48 | * 解析access_token方法请求后的返回值 49 | * @param string $result 获取access_token的方法的返回值 50 | */ 51 | protected function parseToken($result, $extend) 52 | { 53 | $data = json_decode($result, true); 54 | if ($data['access_token'] && $data['expires_in'] && $data['refresh_token']) { 55 | $this->Token = $data; 56 | $data['openid'] = $this->openid(); 57 | return $data; 58 | } else 59 | throw new \Exception("获取百度ACCESS_TOKEN出错:{$data['error']}"); 60 | } 61 | 62 | /** 63 | * 获取当前授权应用的openid 64 | * @return string 65 | */ 66 | public function openid() 67 | { 68 | $data = $this->call('passport/users/getInfo'); 69 | if (isset($data['userid'])) 70 | return $data['userid']; 71 | else 72 | throw new \Exception('没有获取到百度用户ID!'); 73 | } 74 | } -------------------------------------------------------------------------------- /src/sdk/Dingtalk.php: -------------------------------------------------------------------------------- 1 | getMicrotime(); 46 | // 根据timestamp, appSecret计算签名值 47 | $s = hash_hmac('sha256', $time, $this->AppSecret, true); 48 | $signature = base64_encode($s); 49 | $urlencode_signature = urlencode($signature); 50 | /* Dingtalk 调用公共参数 */ 51 | $params = array( 52 | 'tmp_auth_code'=>$param['code'], 53 | ); 54 | $data = $this->http($this->url($api.'?signature='.$urlencode_signature.'×tamp='.$time.'&accessKey='.$this->AppKey), $this->param($params, $param), $method); 55 | return json_decode($data, true); 56 | } 57 | 58 | /** 59 | * 发送HTTP请求方法,目前只支持CURL发送请求 60 | * @param string $url 请求URL 61 | * @param array $params 请求参数 62 | * @param string $method 请求方法GET/POST 63 | * @return array $data 响应数据 64 | */ 65 | public function http($url, $params, $method = 'POST', $header = array(), $multi = false) 66 | { 67 | $data_string = json_encode($params); 68 | $ch = curl_init($url); 69 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); 70 | curl_setopt($ch, CURLOPT_POSTFIELDS,$data_string); 71 | curl_setopt($ch, CURLOPT_RETURNTRANSFER,true); 72 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 73 | curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); 74 | curl_setopt($ch, CURLOPT_HTTPHEADER, array( 75 | 'Content-Type: application/json', 76 | 'Content-Length: ' . strlen($data_string)) 77 | ); 78 | 79 | $result = curl_exec($ch); 80 | return $result; 81 | } 82 | 83 | /** 84 | * 时间戳 - 精确到毫秒 85 | * @return float 86 | */ 87 | public function getMicrotime() 88 | { 89 | list($t1, $t2) = explode(' ', microtime()); 90 | return (float)sprintf('%.0f', (floatval($t1) + floatval($t2)) * 1000); 91 | } 92 | 93 | /** 94 | * 请求code 95 | */ 96 | public function getRequestCodeURL() 97 | { 98 | $this->config(); 99 | //Oauth 标准参数 100 | $params = array( 101 | 'appid' => $this->AppKey, 102 | 'redirect_uri' => $this->Callback, 103 | 'response_type' => $this->ResponseType, 104 | ); 105 | 106 | //获取额外参数 107 | if ($this->Authorize) { 108 | parse_str($this->Authorize, $_param); 109 | if (is_array($_param)) { 110 | $params = array_merge($params, $_param); 111 | } else { 112 | throw new \Exception('AUTHORIZE配置不正确!'); 113 | } 114 | } 115 | return $this->GetRequestCodeURL . '?' . http_build_query($params); 116 | } 117 | 118 | /** 119 | * 获取access_token 120 | * @param string $code 上一步请求到的code 121 | */ 122 | public function getAccessToken($code, $extend = null) 123 | { 124 | $result = $this->call('sns/getuserinfo_bycode',['code'=>$code]); 125 | $data = $result['user_info']; 126 | $this->Token = [ 127 | 'nick'=>$data['nick'], 128 | 'openid'=>$data['openid'], 129 | 'unionid'=>$data['unionid'], 130 | ]; 131 | return $this->Token; 132 | } 133 | 134 | /** 135 | * 解析access_token方法请求后的返回值 136 | * @param string $result 获取access_token的方法的返回值 137 | */ 138 | protected function parseToken($result, $extend){} 139 | 140 | /** 141 | * 获取当前授权应用的openid 142 | * @return string 143 | */ 144 | public function openid($unionid=false) 145 | { 146 | if ($unionid){ 147 | return $this->unionid(); 148 | } 149 | $data = $this->Token; 150 | if (isset($data['openid'])){ 151 | return $data['openid']; 152 | }else{ 153 | throw new \Exception('没有获取到 Dingtalk 用户openid!'); 154 | } 155 | } 156 | 157 | /** 158 | * 获取当前授权应用的unionid 159 | */ 160 | public function unionid() 161 | { 162 | $data = $this->Token; 163 | if (isset($data['unionid'])){ 164 | return $data['unionid']; 165 | }else{ 166 | throw new \Exception('没有获取到 Dingtalk 用户unionid!'); 167 | } 168 | } 169 | 170 | } -------------------------------------------------------------------------------- /src/sdk/Douyin.php: -------------------------------------------------------------------------------- 1 | config(); 42 | $params = array( 43 | 'client_key ' => $this->AppKey, 44 | 'redirect_uri' => $this->Callback, 45 | 'response_type' => $this->ResponseType, 46 | ); 47 | 48 | //获取额外参数 49 | if ($this->Authorize) { 50 | parse_str($this->Authorize, $_param); 51 | if (is_array($_param)) { 52 | $params = array_merge($params, $_param); 53 | } else { 54 | throw new \Exception('AUTHORIZE配置不正确!'); 55 | } 56 | } 57 | return $this->GetRequestCodeURL . '?' . http_build_query($params); 58 | } 59 | 60 | /** 61 | * 获取access_token 62 | * @param string $code 上一步请求到的code 63 | */ 64 | public function getAccessToken($code, $extend = null) 65 | { 66 | $this->config(); 67 | $params = array( 68 | 'client_key' => $this->AppKey, 69 | 'client_secret ' => $this->AppSecret, 70 | 'grant_type' => $this->GrantType, 71 | 'code' => $code, 72 | ); 73 | $data = $this->http($this->GetAccessTokenURL, $params, 'GET'); 74 | $this->Token = $this->parseToken($data, $extend); 75 | return $this->Token; 76 | } 77 | 78 | /** 79 | * 组装接口调用参数 并调用接口 80 | * @param string $api Douyin API 81 | * @param string $param 调用API的额外参数 82 | * @param string $method HTTP请求方法 默认为GET 83 | * @return json 84 | */ 85 | public function call($api, $param = '', $method = 'GET', $multi = false) 86 | { 87 | /* Douyin 调用公共参数 */ 88 | $params = array( 89 | 'access_token' => $this->Token['access_token'], 90 | 'open_id' => $this->openid(), 91 | ); 92 | $data = $this->http($this->url($api), $this->param($params, $param), $method); 93 | return json_decode($data, true); 94 | } 95 | 96 | /** 97 | * 解析access_token方法请求后的返回值 98 | * @param string $result 获取access_token的方法的返回值 99 | */ 100 | protected function parseToken($result, $extend) 101 | { 102 | $data = json_decode($result, true); 103 | $data = $data['data']; 104 | if ($data['access_token'] && $data['expires_in']) { 105 | $this->Token = $data; 106 | $data['openid'] = $this->openid(); 107 | return $data; 108 | } else 109 | throw new \Exception("获取 抖音 ACCESS_TOKEN出错:未知错误"); 110 | } 111 | 112 | /** 113 | * 获取当前授权应用的openid 114 | * @return string 115 | */ 116 | public function openid($unionid=false) 117 | { 118 | if ($unionid){ 119 | return $this->unionid(); 120 | } 121 | $data = $this->call('oauth/userinfo'); 122 | $data = $data['data']; 123 | if (isset($data['open_id'])) 124 | return $data['open_id']; 125 | else 126 | throw new \Exception('没有获取到 抖音 用户openid!'); 127 | } 128 | 129 | /** 130 | * 获取当前授权应用的unionid 131 | */ 132 | public function unionid() 133 | { 134 | $data = $this->call('oauth/userinfo'); 135 | $data = $data['data']; 136 | if (isset($data['union_id'])) 137 | return $data['union_id']; 138 | else 139 | exit('没有获取到 抖音 用户unionid!'); 140 | } 141 | } -------------------------------------------------------------------------------- /src/sdk/Facebook.php: -------------------------------------------------------------------------------- 1 | $this->Token['access_token']); 48 | $data = $this->http($this->url($api), $this->param($params, $param), $method); 49 | return json_decode($data, true); 50 | } 51 | 52 | /** 53 | * 解析access_token方法请求后的返回值 54 | * @param string $result 获取access_token的方法的返回值 55 | * @param mix $extend 56 | * @return array 57 | */ 58 | protected function parseToken($result, $extend) 59 | { 60 | $data = json_decode($result, true); 61 | if (is_array($data) && $data['access_token'] && $data['expires_in']) { 62 | $this->Token = $data; 63 | $data['openid'] = $this->openid(); 64 | return $data; 65 | } else { 66 | throw new \Exception("获取 facebook ACCESS_TOKEN出错:未知错误"); 67 | } 68 | 69 | } 70 | 71 | /** 72 | * 获取当前授权应用的openid 73 | * @return string 74 | */ 75 | public function openid() 76 | { 77 | $data = $this->call('me'); 78 | if (isset($data['id'])) 79 | return $data['id']; 80 | else 81 | throw new \Exception('没有获取到 facebook 用户ID!'); 82 | } 83 | } -------------------------------------------------------------------------------- /src/sdk/Gitee.php: -------------------------------------------------------------------------------- 1 | Token['access_token']}"); 48 | $data = $this->http($this->url($api), $this->param($params, $param), $method, $header); 49 | return json_decode($data, true); 50 | } 51 | 52 | /** 53 | * 解析access_token方法请求后的返回值 54 | * @param string $result 获取access_token的方法的返回值 55 | */ 56 | protected function parseToken($result, $extend) 57 | { 58 | $data = json_decode($result, true); 59 | if ($data['access_token'] && $data['token_type']) { 60 | $this->Token = $data; 61 | $data['openid'] = $this->openid(); 62 | return $data; 63 | } else 64 | throw new \Exception("获取 Gitee ACCESS_TOKEN出错:未知错误"); 65 | } 66 | 67 | /** 68 | * 获取当前授权应用的openid 69 | * @return string 70 | */ 71 | public function openid() 72 | { 73 | $data = $this->call('user'); 74 | if (isset($data['id'])) 75 | return $data['id']; 76 | else 77 | throw new \Exception('没有获取到 Gitee 用户ID!'); 78 | } 79 | } -------------------------------------------------------------------------------- /src/sdk/Github.php: -------------------------------------------------------------------------------- 1 | Token['access_token']}","User-Agent:thinkphp-social"); 42 | $data = $this->http($this->url($api), $this->param($params, $param), $method, $header); 43 | return json_decode($data, true); 44 | } 45 | 46 | /** 47 | * 解析access_token方法请求后的返回值 48 | * @param string $result 获取access_token的方法的返回值 49 | */ 50 | protected function parseToken($result, $extend) 51 | { 52 | parse_str($result, $data); 53 | if ($data['access_token'] && $data['token_type']) { 54 | $this->Token = $data; 55 | $data['openid'] = $this->openid(); 56 | return $data; 57 | } else 58 | throw new \Exception("获取 Github ACCESS_TOKEN出错:未知错误"); 59 | } 60 | 61 | /** 62 | * 获取当前授权应用的openid 63 | * @return string 64 | */ 65 | public function openid() 66 | { 67 | $data = $this->call('user'); 68 | if (isset($data['id'])) 69 | return $data['id']; 70 | else 71 | throw new \Exception('没有获取到 Github 用户ID!'); 72 | } 73 | } -------------------------------------------------------------------------------- /src/sdk/Google.php: -------------------------------------------------------------------------------- 1 | $this->Token['access_token'], 48 | ); 49 | $data = $this->http($this->url($api), $this->param($params, $param), $method); 50 | return json_decode($data, true); 51 | } 52 | 53 | /** 54 | * 解析access_token方法请求后的返回值 55 | * @param string $result 获取access_token的方法的返回值 56 | */ 57 | protected function parseToken($result, $extend) 58 | { 59 | $data = json_decode($result, true); 60 | if ($data['access_token'] && $data['expires_in']) { 61 | $this->Token = $data; 62 | $data['openid'] = $this->openid(); 63 | return $data; 64 | } else 65 | throw new \Exception("获取Google ACCESS_TOKEN 出错:{$result}"); 66 | } 67 | 68 | /** 69 | * 获取当前授权应用的openid 70 | * @return string 71 | */ 72 | public function openid() 73 | { 74 | $data = $this->call('userinfo'); 75 | if (isset($data['id'])) 76 | return $data['id']; 77 | else 78 | throw new \Exception('没有获取到 Google 用户ID!'); 79 | } 80 | } -------------------------------------------------------------------------------- /src/sdk/Oschina.php: -------------------------------------------------------------------------------- 1 | $this->Token['access_token'], 48 | 'dataType' => 'json' 49 | ); 50 | $data = $this->http($this->url($api), $this->param($params, $param), $method); 51 | return json_decode($data, true); 52 | } 53 | 54 | /** 55 | * 解析access_token方法请求后的返回值 56 | * @param string $result 获取access_token的方法的返回值 57 | */ 58 | protected function parseToken($result, $extend) 59 | { 60 | $data = json_decode($result, true); 61 | if ($data['access_token'] && $data['token_type']) { 62 | $this->Token = $data; 63 | $data['openid'] = $this->openid(); 64 | return $data; 65 | } else 66 | throw new \Exception("获取 Oschina ACCESS_TOKEN出错:未知错误"); 67 | } 68 | 69 | /** 70 | * 获取当前授权应用的openid 71 | * @return string 72 | */ 73 | public function openid() 74 | { 75 | $data = $this->call('action/openapi/user'); 76 | if (isset($data['id'])) 77 | return $data['id']; 78 | else 79 | throw new \Exception('没有获取到 Oschina 用户ID!'); 80 | } 81 | } -------------------------------------------------------------------------------- /src/sdk/Qq.php: -------------------------------------------------------------------------------- 1 | $this->AppKey, 48 | 'access_token' => $this->Token['access_token'], 49 | 'openid' => $this->openid(), 50 | 'format' => 'json' 51 | ); 52 | $data = $this->http($this->url($api), $this->param($params, $param), $method); 53 | return json_decode($data, true); 54 | } 55 | 56 | /** 57 | * 解析access_token方法请求后的返回值 58 | * @param string $result 获取access_token的方法的返回值 59 | */ 60 | protected function parseToken($result, $extend) 61 | { 62 | parse_str($result, $data); 63 | if ($data['access_token'] && $data['expires_in']) { 64 | $this->Token = $data; 65 | $data['openid'] = $this->openid(); 66 | return $data; 67 | } else 68 | throw new \Exception("获取腾讯QQ ACCESS_TOKEN 出错:{$result}"); 69 | } 70 | 71 | /** 72 | * 获取当前授权应用的openid 73 | * @return string 74 | */ 75 | public function openid($unionid=false) 76 | { 77 | if ($unionid){ 78 | return $this->unionid(); 79 | } 80 | $data = $this->Token; 81 | if ($data['access_token']) { 82 | $data = $this->http($this->url('oauth2.0/me'), array('access_token' => $data['access_token'])); 83 | $data = json_decode(trim(substr($data, 9), " );\n"), true); 84 | if (isset($data['openid'])) 85 | return $data['openid']; 86 | else 87 | throw new \Exception("获取用户openid出错:{$data['error_description']}"); 88 | } else { 89 | throw new \Exception('没有获取到openid!'); 90 | } 91 | } 92 | 93 | /** 94 | * 获取当前授权应用的unionid 95 | * @return string 96 | */ 97 | public function unionid() 98 | { 99 | $data = $this->Token; 100 | if ($data['access_token']) { 101 | $data = $this->http($this->url('oauth2.0/me'), array('access_token' => $data['access_token'],'unionid'=>1)); 102 | $data = json_decode(trim(substr($data, 9), " );\n"), true); 103 | if (isset($data['unionid'])) 104 | return $data['unionid']; 105 | else 106 | throw new \Exception("获取用户unionid出错:{$data['error_description']}"); 107 | } else { 108 | throw new \Exception('没有获取到unionid!'); 109 | } 110 | } 111 | } -------------------------------------------------------------------------------- /src/sdk/Sina.php: -------------------------------------------------------------------------------- 1 | $this->Token['access_token'], 42 | ); 43 | $data = $this->http($this->url($api, '.json'), $this->param($params, $param), $method); 44 | return json_decode($data, true); 45 | } 46 | 47 | /** 48 | * 解析access_token方法请求后的返回值 49 | * @param string $result 获取access_token的方法的返回值 50 | */ 51 | protected function parseToken($result, $extend) 52 | { 53 | $data = json_decode($result, true); 54 | if ($data['access_token'] && $data['expires_in'] && $data['remind_in'] && $data['uid']) { 55 | $this->Token = $data; 56 | $data['openid'] = $this->openid(); 57 | return $data; 58 | } else 59 | throw new \Exception("获取新浪微博ACCESS_TOKEN出错:{$data['error']}"); 60 | } 61 | 62 | /** 63 | * 获取当前授权应用的openid 64 | * @return string 65 | */ 66 | public function openid() 67 | { 68 | $data = $this->Token; 69 | if (isset($data['uid'])) 70 | return $data['uid']; 71 | else 72 | throw new \Exception('没有获取到新浪微博用户ID!'); 73 | } 74 | } -------------------------------------------------------------------------------- /src/sdk/Taobao.php: -------------------------------------------------------------------------------- 1 | $api, 42 | 'access_token' => $this->Token['access_token'], 43 | 'format' => 'json', 44 | 'v' => '2.0', 45 | ); 46 | $paramsAll = $this->param($params, $param); 47 | ksort($paramsAll); 48 | $str = ''; 49 | foreach ($paramsAll as $key => $value) { 50 | $str .= $key . $value; 51 | } 52 | $sign = strtoupper(md5($this->AppSecret . $str . $this->AppSecret)); 53 | $params_sign = array( 54 | 'sign' => $sign, 55 | ); 56 | $data = $this->http($this->url(), array_merge($paramsAll, $params_sign), $method); 57 | return json_decode($data, true); 58 | } 59 | 60 | /** 61 | * 解析access_token方法请求后的返回值 62 | * @param string $result 获取access_token的方法的返回值 63 | */ 64 | protected function parseToken($result, $extend) 65 | { 66 | $data = json_decode($result, true); 67 | if ($data['access_token'] && $data['expires_in'] && $data['taobao_user_id']) { 68 | $this->Token = $data; 69 | $data['openid'] = $this->openid(); 70 | return $data; 71 | } else { 72 | throw new \Exception("获取淘宝网ACCESS_TOKEN出错:{$data['error']}"); 73 | } 74 | } 75 | 76 | /** 77 | * 获取当前授权应用的openid 78 | * @return string 79 | */ 80 | public function openid($unionid = false) 81 | { 82 | if ($unionid) { 83 | return $this->unionid(); 84 | } 85 | $data = $this->Token; 86 | if (isset($data['taobao_user_id'])) { 87 | return $data['taobao_user_id']; 88 | } else { 89 | throw new \Exception('没有获取到淘宝网用户openid!'); 90 | } 91 | } 92 | 93 | /** 94 | * 获取当前授权应用的unionid 95 | * @return string 96 | */ 97 | public function unionid() 98 | { 99 | $data = $this->Token; 100 | if (isset($data['taobao_open_uid'])) { 101 | return $data['taobao_open_uid']; 102 | } else { 103 | throw new \Exception('没有获取到淘宝网用户unionid!'); 104 | } 105 | } 106 | } -------------------------------------------------------------------------------- /src/sdk/Weixin.php: -------------------------------------------------------------------------------- 1 | config(); 42 | $params = array( 43 | 'appid' => $this->AppKey, 44 | 'redirect_uri' => $this->Callback, 45 | 'response_type' => $this->ResponseType, 46 | ); 47 | 48 | //获取额外参数 49 | if ($this->Authorize) { 50 | parse_str($this->Authorize, $_param); 51 | if (is_array($_param)) { 52 | $params = array_merge($params, $_param); 53 | } else { 54 | throw new \Exception('AUTHORIZE配置不正确!'); 55 | } 56 | } 57 | return $this->GetRequestCodeURL . '?' . http_build_query($params) . "#wechat_redirect"; 58 | } 59 | 60 | /** 61 | * 获取access_token 62 | * @param string $code 上一步请求到的code 63 | */ 64 | public function getAccessToken($code, $extend = null) 65 | { 66 | $this->config(); 67 | $params = array( 68 | 'appid' => $this->AppKey, 69 | 'secret' => $this->AppSecret, 70 | 'grant_type' => $this->GrantType, 71 | 'code' => $code, 72 | ); 73 | $data = $this->http($this->GetAccessTokenURL, $params, 'POST'); 74 | $this->Token = $this->parseToken($data, $extend); 75 | return $this->Token; 76 | } 77 | 78 | /** 79 | * 组装接口调用参数 并调用接口 80 | * @param string $api 微信 API 81 | * @param string $param 调用API的额外参数 82 | * @param string $method HTTP请求方法 默认为GET 83 | * @return json 84 | */ 85 | public function call($api, $param = '', $method = 'GET', $multi = false) 86 | { 87 | /* 微信调用公共参数 */ 88 | $params = array( 89 | 'access_token' => $this->Token['access_token'], 90 | 'openid' => $this->openid(), 91 | 'lang' => 'zh_CN', 92 | ); 93 | $data = $this->http($this->url($api), $this->param($params, $param), $method); 94 | return json_decode($data, true); 95 | } 96 | 97 | /** 98 | * 解析access_token方法请求后的返回值 99 | */ 100 | protected function parseToken($result, $extend) 101 | { 102 | $data = json_decode($result, true); 103 | if ($data['access_token'] && $data['expires_in']) { 104 | $this->Token = $data; 105 | $data['openid'] = $this->openid(); 106 | return $data; 107 | } else 108 | throw new \Exception("获取微信 ACCESS_TOKEN 出错:{$result}"); 109 | } 110 | 111 | /** 112 | * 获取当前授权应用的openid 113 | */ 114 | public function openid($unionid=false) 115 | { 116 | if ($unionid){ 117 | return $this->unionid(); 118 | } 119 | $data = $this->Token; 120 | if (isset($data['openid'])) 121 | return $data['openid']; 122 | else 123 | exit('没有获取到微信用户openid!'); 124 | } 125 | 126 | /** 127 | * 获取当前授权应用的unionid 128 | */ 129 | public function unionid() 130 | { 131 | $data = $this->Token; 132 | if (isset($data['unionid'])) 133 | return $data['unionid']; 134 | else 135 | exit('没有获取到微信用户unionid!'); 136 | } 137 | } -------------------------------------------------------------------------------- /src/sdk/Xiaomi.php: -------------------------------------------------------------------------------- 1 | $this->Token['access_token'], 48 | 'clientId' => $this->AppKey 49 | ); 50 | $data = $this->http($this->url($api), $this->param($params, $param), $method); 51 | return json_decode($data, true); 52 | } 53 | 54 | /** 55 | * 解析access_token方法请求后的返回值 56 | * @param string $result 获取access_token的方法的返回值 57 | */ 58 | protected function parseToken($result, $extend) 59 | { 60 | $data = json_decode($result, true); 61 | if ($data['access_token'] && $data['token_type']) { 62 | $this->Token = $data; 63 | $data['openid'] = $this->openid(); 64 | return $data; 65 | } else 66 | throw new \Exception("获取 Xiaomi ACCESS_TOKEN出错:未知错误"); 67 | } 68 | 69 | /** 70 | * 获取当前授权应用的openid 71 | * @return string 72 | */ 73 | public function openid($unionid=false) 74 | { 75 | if ($unionid){ 76 | return $this->unionid(); 77 | } 78 | $data = $this->call('user/profile'); 79 | if ($data['result']=='ok'){ 80 | $data = $data['data']; 81 | if (isset($data['userId'])){ 82 | return $data['userId']; 83 | }else{ 84 | return $this->unionid(); 85 | } 86 | }else{ 87 | throw new \Exception('没有获取到 Xiaomi 用户openid!'); 88 | } 89 | } 90 | 91 | /** 92 | * 获取当前授权应用的unionid 93 | * @return string 94 | */ 95 | public function unionid() 96 | { 97 | $data = $this->call('user/profile'); 98 | $data = $data['data']; 99 | if (isset($data['unionId'])) 100 | return $data['unionId']; 101 | else 102 | throw new \Exception('没有获取到 Xiaomi 用户unionid!'); 103 | } 104 | } --------------------------------------------------------------------------------