我想登录到一个网站(远程)可以说example.com/login和example.com/login使用请求令牌登录,所以我得到请求令牌从像这样的网址如下如何使用令牌登录而不刷新卷曲
// code for getting token cookies etc
$url = 'http://example.com/login/';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$doc = curl_exec($ch);
curl_close($ch);
// extract __RequestVerificationToken input field
preg_match('#<input name="__RequestVerificationToken" type="hidden" value="(.*?)"#is', $doc, $match);
$token = $match[1];
// code for redirect to dashboard
$postinfo = "Email=".$username."&Password=".$password."&__RequestVerificationToken=".$token;
// var_dump($token); //debug info
$useragent="Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36";
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postinfo);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
$html = curl_exec($ch);
echo $html;
if (curl_errno($ch)) print curl_error($ch);
curl_close($ch);
因此,即时得到令牌,但试图与未来卷曲请求登录时,显然是$令牌不断变化,由于刷新,所以我想知道我可以登录网址example.com /使用相同的curl脚本登录,以便$ token保持相同?
TIA!
,网页上会得到一个令牌是部分登录表单,然后在页面上登录登录表单以获取会话cookie。这些cookie应该工作到会话超时。在Chrome中使用F12,并选中该框以在导航之间保留网络数据,以查看浏览器如何进行操作。 –