访问微信公众平台开发者文档,开始开发-接口测试号申请,填写相关资料申请到一个接口,获取appid和appsecret。 在下面的体验接口权限表中,找到“网页授权获取用户基本信息”,点击修改设置域名。请注意,这里填写的是域名(是一个字符串),而不是URL,因此请勿加http://等协议头。测试接口允许写ip(实测用ip的话会有问题,建议用域名),但注意在用手机测试的时候要把apache的权限设置对。注意根目录和当前项目的目录权限都要设置允许。下面是参考设置。
<VirtualHost *:80> #www目录 ServerName localhost DocumentRoot D:/site <Directory "D:/site"> Options +Indexes +FollowSymLinks +MultiViews AllowOverride All Require all granted #允许访问 </Directory> </VirtualHost> <VirtualHost *:80> DocumentRoot "d:/site/xxx/public" #项目所在目录 ServerName dev.xxx.com ServerAlias *.xxx.com ErrorLog "logs/dev.xxx.com-error.log" CustomLog "logs/dev.xxx.com-access.log" common <Directory "d:/site/xxx/public"> Options Indexes FollowSymLinks AllowOverride all Order Allow,Deny Allow from all Require all granted #允许访问 Satisfy any </Directory> </VirtualHost>首先在composer里添加以下内容。
"require": { "laravel/socialite": "^2.0", "socialiteproviders/weixin": "3.0.*" },使用composer install安装以上的库。
添加Provider
'providers' => [ SocialiteProviders\Manager\ServiceProvider::class, ],添加Alias
'aliases' => [ 'Socialite' => Laravel\Socialite\Facades\Socialite::class, ],设置微信接口的appid和appsecret
'weixin' => [ 'client_id' => env('WEIXIN_KEY'), 'client_secret' => env('WEIXIN_SECRET'), 'redirect' => env('WEIXIN_REDIRECT_URI'), ],添加事件监听
use SocialiteProviders\Manager\SocialiteWasCalled; protected $listen = [ SocialiteWasCalled::class => [ \SocialiteProviders\Weixin\WeixinExtendSocialite::class, ], ];Laravel5.3关于路由有如下变更
By default, fresh Laravel 5.3 applications contain two HTTP route files in a new top-level routes directory.
在routes/web.php添加如下两行
Route::get('auth/weixin', 'Auth\WeixinController@redirectToProvider'); Route::get('auth/weixin/callback', 'Auth\WeixinController@handleProviderCallback');代码实现如下
<?php /** * Created by PhpStorm. * User: weiling * Date: 1/6/2017 * Time: 11:34 AM */ namespace App\Http\Controllers\Auth; use App\Http\Controllers\Controller; use App\User; use Auth; use Log; use Exception; use Illuminate\Http\Request; use Socialite; use SocialiteProviders\Weixin\Provider; class WeixinController extends Controller{ public function redirectToProvider(Request $request) { return Socialite::with('weixin')->redirect(); } public function handleProviderCallback(Request $request) { $user_data = Socialite::with('weixin')->user(); //todo whatever } }作者:兮嘉 链接:https://www.jianshu.com/p/9be317865c9e 來源:简书 简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。