使用Guzzle POST进行Laravel身份验证

时间:2018-09-27 04:16:09

标签: php laravel guzzle

我有一个使用username,password,role进行身份验证的API。结果就像在json中这样

{
    "success": true,
    "message": "Welcome !",
    "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczovL2FwaS5hbGhpa21haGJvZ29yLm9yZy92MS9tb2JpbGUvYXV0aC9sb2dpbiIsImlhdCI6MTUzODAxOTg2NCwiZXhwIjoxNTM4MTA2MjY0LCJuYmYiOjE1MzgwMTk4NjQsImp0aSI6IjZuTlNLSUdFeDdaT1l2bTkiLCJzdWIiOjY1fQ.N0rtAsA2cTWfsYulRsrdkiso3dXHe_7W16BSYrdVwa8",
    "data": [
        {
            "id": 65,
            "username": "0110-04-005",
            "email": "yourmail@gmail.com",
        }
    ]
}

而且吃惊的应该是这样

$requestResponse = $client->request('POST',$productionUrl,[
                        'auth' => ['username','password'],
                        'form_params' => [
                            'username' => $request->nis,
                            'password' => $request->sandi,
                            'role' => 'santri'
                        ],
                        'cookies' => $cookieJar,
                        'content-type' => 'application/json',
                        'connect_timeout' => 3.14
                    ]);
            $res = json_decode($requestResponse->getBody(),true);

并且每次我使用auth以这种身份验证方法Auth::login($res)auth()->login($res)登录时,它都会答复这样的错误

Type error: Argument 1 passed to Illuminate\Auth\SessionGuard::login() must be an instance of Illuminate\Contracts\Auth\Authenticatable, array given, called in /Applications/XAMPP/xamppfiles/htdocs/my.alhikmahbogor/core/vendor/laravel/framework/src/Illuminate/Auth/AuthManager.php on line 294

我如何使用guzzle http登录使用laravel身份验证的方法?

0 个答案:

没有答案