我正在嘗試使用curl登錄網站,但是無法使用它進行工作。
這是我正在使用的代碼。以curl登錄php
首先,我從網站獲取csrf標記以確保它匹配。
但我得到的結果總是登錄表單。
$url="http://www.example.com/accounts/login";
$postinfo = 'csrfmiddlewaretoken='.$key.'&[email protected]&password=mypassword';
$cookie = "cookies.txt";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postinfo);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt ($ch, CURLOPT_POST, 1);
$result = curl_exec($ch);
return $result;
有誰知道我爲什麼不能登錄?
表單字段是正確的,當我進行登錄嘗試cookie.txt文件已更改。
在打開PHP標記 (例如'<?php error_reporting(E_ALL);)後立即向文件頂部添加錯誤報告。 ini_set('display_errors',1);'然後你的代碼的其餘部分,看看它是否產生任何東西。 –
@ Fred-ii-我沒有看到任何錯誤,只是爲了讓您知道我正在使用laravel 5 – Szenis
您可能需要爲它添加標籤,這可能與它有一些相關性。我無法確定它是否有影響,因爲我對拉拉維爾一無所知。 –