如何使用 curl 函数 模拟登录 Laravel 自带的 auth?
遇到一个爬虫模拟登录的问题。使用curl登录laravel自带的auth的时候csrf_token也带上了,但是还是返回419....这是为什么呢?
$cookie= __DIR__.'/cookie.txt';
$url = 'https://xxx.com';
$headers = [
'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36',
'Content-Type: application/x-www-form-urlencoded; charset=utf-8',
'Cache-Control: max-age=0',
'upgrade-insecure-requests: 1',
];
$post = [
'email' => '123@123.com',
'password' => '123xxx',
'_token' => 'xxxx', //这边的token值是在浏览器元素中找到的token值.为了防止token过期,刷新过
];
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));
$content = curl_exec($curl);
curl_close($curl);
返回的$content
:
HTTP/2 419
server: nginx
content-type: text/html; charset=UTF-8
cache-control: no-cache, private
date: Fri, 28 Jun 2019 10:34:13 GMT
set-cookie: _session=xxxxxx %3D%3D; expires=Fri, 28-Jun-2019 16:34:13 GMT; Max-Age=21600; path=/; httponly
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Page Expired</title>
<!-- Fonts -->
<link rel="dns-prefetch" href="//fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css?family=Nunito" rel="stylesheet">
<!-- Styles -->
<style>
html, body {
background-color: #fff;
color: #636b6f;
font-family: 'Nunito', sans-serif;
font-weight: 100;
height: 100vh;
margin: 0;
}
.full-height {
height: 100vh;
}
.flex-center {
align-items: center;
display: flex;
justify-content: center;
}
.position-ref {
position: relative;
}
.code {
border-right: 2px solid;
font-size: 26px;
padding: 0 15px 0 15px;
text-align: center;
}
.message {
font-size: 18px;
text-align: center;
}
</style>
</head>
<body>
<div class="flex-center position-ref full-height">
<div class="code">
419 </div>
<div class="message" style="padding: 10px;">
Page Expired </div>
</div>
</body>
</html>
文中的一些敏感信息使用
xxxx
代替
推荐文章: