我想登錄到一個網站(遠程)可以說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,並選中該框以在導航之間保留網絡數據,以查看瀏覽器如何進行操作。 –