酒店预订平台
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

3 年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <h1 align="center"> Socialite</h1>
  2. <p align="center">
  3. <a href="https://travis-ci.org/overtrue/socialite"><img src="https://travis-ci.org/overtrue/socialite.svg?branch=master" alt="Build Status"></a>
  4. <a href="https://packagist.org/packages/overtrue/socialite"><img src="https://poser.pugx.org/overtrue/socialite/v/stable.svg" alt="Latest Stable Version"></a>
  5. <a href="https://packagist.org/packages/overtrue/socialite"><img src="https://poser.pugx.org/overtrue/socialite/v/unstable.svg" alt="Latest Unstable Version"></a>
  6. <a href="https://scrutinizer-ci.com/g/overtrue/socialite/build-status/master"><img src="https://scrutinizer-ci.com/g/overtrue/socialite/badges/build.png?b=master" alt="Build Status"></a>
  7. <a href="https://scrutinizer-ci.com/g/overtrue/socialite/?branch=master"><img src="https://scrutinizer-ci.com/g/overtrue/socialite/badges/quality-score.png?b=master" alt="Scrutinizer Code Quality"></a>
  8. <a href="https://scrutinizer-ci.com/g/overtrue/socialite/?branch=master"><img src="https://scrutinizer-ci.com/g/overtrue/socialite/badges/coverage.png?b=master" alt="Code Coverage"></a>
  9. <a href="https://packagist.org/packages/overtrue/socialite"><img src="https://poser.pugx.org/overtrue/socialite/downloads" alt="Total Downloads"></a>
  10. <a href="https://packagist.org/packages/overtrue/socialite"><img src="https://poser.pugx.org/overtrue/socialite/license" alt="License"></a>
  11. </p>
  12. <p align="center">Socialite is an OAuth2 Authentication tool. It is inspired by <a href="https://github.com/laravel/socialite">laravel/socialite</a>, You can easily use it in any PHP project.</p>
  13. # Requirement
  14. ```
  15. PHP >= 5.6
  16. ```
  17. # Installation
  18. ```shell
  19. $ composer require "overtrue/socialite" -vvv
  20. ```
  21. # Usage
  22. For Laravel 5: [overtrue/laravel-socialite](https://github.com/overtrue/laravel-socialite)
  23. `authorize.php`:
  24. ```php
  25. <?php
  26. use Overtrue\Socialite\SocialiteManager;
  27. $config = [
  28. 'github' => [
  29. 'client_id' => 'your-app-id',
  30. 'client_secret' => 'your-app-secret',
  31. 'redirect' => 'http://localhost/socialite/callback.php',
  32. ],
  33. ];
  34. $socialite = new SocialiteManager($config);
  35. $response = $socialite->driver('github')->redirect();
  36. echo $response;// or $response->send();
  37. ```
  38. `callback.php`:
  39. ```php
  40. <?php
  41. use Overtrue\Socialite\SocialiteManager;
  42. $config = [
  43. 'github' => [
  44. 'client_id' => 'your-app-id',
  45. 'client_secret' => 'your-app-secret',
  46. 'redirect' => 'http://localhost/socialite/callback.php',
  47. ],
  48. ];
  49. $socialite = new SocialiteManager($config);
  50. $user = $socialite->driver('github')->user();
  51. $user->getId(); // 1472352
  52. $user->getNickname(); // "overtrue"
  53. $user->getUsername(); // "overtrue"
  54. $user->getName(); // "安正超"
  55. $user->getEmail(); // "anzhengchao@gmail.com"
  56. $user->getProviderName(); // GitHub
  57. ...
  58. ```
  59. ### Configuration
  60. Now we support the following sites:
  61. `facebook`, `github`, `google`, `linkedin`, `outlook`, `weibo`, `taobao`, `qq`, `wechat`, `douyin`, `baidu`, `feishu`, and `douban`.
  62. Each driver uses the same configuration keys: `client_id`, `client_secret`, `redirect`.
  63. Example:
  64. ```
  65. ...
  66. 'weibo' => [
  67. 'client_id' => 'your-app-id',
  68. 'client_secret' => 'your-app-secret',
  69. 'redirect' => 'http://localhost/socialite/callback.php',
  70. ],
  71. ...
  72. ```
  73. ### Scope
  74. Before redirecting the user, you may also set "scopes" on the request using the scope method. This method will overwrite all existing scopes:
  75. ```php
  76. $response = $socialite->driver('github')
  77. ->scopes(['scope1', 'scope2'])->redirect();
  78. ```
  79. ### Redirect URL
  80. You may also want to dynamicly set `redirect`,you can use the following methods to change the `redirect` URL:
  81. ```php
  82. $socialite->redirect($url);
  83. // or
  84. $socialite->withRedirectUrl($url)->redirect();
  85. // or
  86. $socialite->setRedirectUrl($url)->redirect();
  87. ```
  88. > WeChat scopes:
  89. - `snsapi_base`, `snsapi_userinfo` - Used to Media Platform Authentication.
  90. - `snsapi_login` - Used to web Authentication.
  91. ### Additional parameters
  92. To include any optional parameters in the request, call the with method with an associative array:
  93. ```php
  94. $response = $socialite->driver('google')
  95. ->with(['hd' => 'example.com'])->redirect();
  96. ```
  97. ### User interface
  98. #### Standard user api:
  99. ```php
  100. $user = $socialite->driver('weibo')->user();
  101. ```
  102. ```json
  103. {
  104. "id": 1472352,
  105. "nickname": "overtrue",
  106. "name": "安正超",
  107. "email": "anzhengchao@gmail.com",
  108. "avatar": "https://avatars.githubusercontent.com/u/1472352?v=3",
  109. "original": {
  110. "login": "overtrue",
  111. "id": 1472352,
  112. "avatar_url": "https://avatars.githubusercontent.com/u/1472352?v=3",
  113. "gravatar_id": "",
  114. "url": "https://api.github.com/users/overtrue",
  115. "html_url": "https://github.com/overtrue",
  116. ...
  117. },
  118. "token": {
  119. "access_token": "5b1dc56d64fffbd052359f032716cc4e0a1cb9a0",
  120. "token_type": "bearer",
  121. "scope": "user:email"
  122. }
  123. }
  124. ```
  125. You can fetch the user attribute as a array keys like these:
  126. ```php
  127. $user['id']; // 1472352
  128. $user['nickname']; // "overtrue"
  129. $user['name']; // "安正超"
  130. $user['email']; // "anzhengchao@gmail.com"
  131. ...
  132. ```
  133. Or using the method:
  134. ```php
  135. $user->getId();
  136. $user->getNickname();
  137. $user->getName();
  138. $user->getEmail();
  139. $user->getAvatar();
  140. $user->getOriginal();
  141. $user->getToken();// or $user->getAccessToken()
  142. $user->getProviderName(); // GitHub/Google/Facebook...
  143. ```
  144. #### Get original response from OAuth API
  145. The `$user->getOriginal()` method will return an array of the API raw response.
  146. #### Get access token Object
  147. You can get the access token instance of current session by call `$user->getToken()` or `$user->getAccessToken()` or `$user['token']` .
  148. ### Get user with access token
  149. ```php
  150. $accessToken = new AccessToken(['access_token' => $accessToken]);
  151. $user = $socialite->user($accessToken);
  152. ```
  153. ### Custom Session or Request instance.
  154. You can set the request with your custom `Request` instance which instanceof `Symfony\Component\HttpFoundation\Request` before you call `driver` method.
  155. ```php
  156. $request = new Request(); // or use AnotherCustomRequest.
  157. $socialite = new SocialiteManager($config, $request);
  158. ```
  159. Or set request to `SocialiteManager` instance:
  160. ```php
  161. $socialite->setRequest($request);
  162. ```
  163. You can get the request from the `SocialiteManager` instance by `getRequest()`:
  164. ```php
  165. $request = $socialite->getRequest();
  166. ```
  167. #### Set custom session manager.
  168. By default, the `SocialiteManager` uses the `Symfony\Component\HttpFoundation\Session\Session` instance as session manager, you can change it as follows:
  169. ```php
  170. $session = new YourCustomSessionManager();
  171. $socialite->getRequest()->setSession($session);
  172. ```
  173. > Your custom session manager must be implement the [`Symfony\Component\HttpFoundation\Session\SessionInterface`](http://api.symfony.com/3.0/Symfony/Component/HttpFoundation/Session/SessionInterface.html).
  174. Enjoy it! :heart:
  175. # Reference
  176. - [Google - OpenID Connect](https://developers.google.com/identity/protocols/OpenIDConnect)
  177. - [Facebook - Graph API](https://developers.facebook.com/docs/graph-api)
  178. - [Linkedin - Authenticating with OAuth 2.0](https://developer.linkedin.com/docs/oauth2)
  179. - [微博 - OAuth 2.0 授权机制说明](http://open.weibo.com/wiki/%E6%8E%88%E6%9D%83%E6%9C%BA%E5%88%B6%E8%AF%B4%E6%98%8E)
  180. - [QQ - OAuth 2.0 登录QQ](http://wiki.connect.qq.com/oauth2-0%E7%AE%80%E4%BB%8B)
  181. - [微信公众平台 - OAuth文档](http://mp.weixin.qq.com/wiki/9/01f711493b5a02f24b04365ac5d8fd95.html)
  182. - [微信开放平台 - 网站应用微信登录开发指南](https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419316505&token=&lang=zh_CN)
  183. - [微信开放平台 - 代公众号发起网页授权](https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419318590&token=&lang=zh_CN)
  184. - [豆瓣 - OAuth 2.0 授权机制说明](http://developers.douban.com/wiki/?title=oauth2)
  185. - [抖音 - 网站应用开发指南](http://open.douyin.com/platform/doc)
  186. - [飞书 - 授权说明](https://open.feishu.cn/document/ukTMukTMukTM/uMTNz4yM1MjLzUzM)
  187. ## PHP 扩展包开发
  188. > 想知道如何从零开始构建 PHP 扩展包?
  189. >
  190. > 请关注我的实战课程,我会在此课程中分享一些扩展开发经验 —— [《PHP 扩展包实战教程 - 从入门到发布》](https://learnku.com/courses/creating-package)
  191. # License
  192. MIT