簡體   English   中英

如何在單個cURL請求中獲取和發布信息?

[英]How to get and post information in a single cURL request?

是否可以獲取inputvalue並在同一cURL請求中post信息?

原因:我試圖使用cURL登錄此處 ,但是登錄表單會在名為form_keyinput value中生成一個唯一鍵。 因此,我想首先獲取該值,然后發布用戶名和密碼以成功登錄。

PHP(無法正常工作,在空白頁中沒有源代碼):

$ch2 = curl_init();
curl_setopt($ch2, CURLOPT_URL, "https://b2b.chiemsee.com/customer/account/login/");
curl_setopt($ch2, CURLOPT_FOLLOWLOCATION, true); // to allow redirections
//curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, FALSE); // to avoid error
curl_setopt($ch2, CURLOPT_POST, true); // to send info
curl_setopt($ch2, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch2, CURLOPT_COOKIEJAR, 'cookie.txt'); // to save cookie data for login
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true); // to get the html
$response2 = curl_exec($ch2);
curl_close($ch2);

$html = new simple_html_dom();
$html->load($response2);

$val = $html->find('input[name=form_key]');
$form_key = $val[0]->value;

$data = array(
    'form_key' => $form_key,
    'login[username]' => 'myusername',
    'login[password]' => 'mypassword',
    'send' => '',
);

PS當我手動登錄並檢查瀏覽器控制台->網絡->發布->標頭/參數時,得到以下信息:

form_key: viiRqZigH0YPC9wu
login[username]: myusername
login[password]: mypassword
send: 

UPDATE。 兩個單獨的cURL請求(現在仍然有效):

包括( 'simple_html_dom.php');

$ch2 = curl_init();
curl_setopt($ch2, CURLOPT_URL, "https://b2b.chiemsee.com/customer/account/login/");
curl_setopt($ch2, CURLOPT_FOLLOWLOCATION, true); // to allow redirections
curl_setopt($ch2, CURLOPT_COOKIEJAR, 'cookie.txt'); // to save cookie data for login
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true); // to get the html
$response2 = curl_exec($ch2);
curl_close($ch2);

$html = new simple_html_dom();
$html->load($response2);

$val = $html->find('input[name=form_key]');
$form_key = $val[0]->value;

$data = array(
    'form_key' => $form_key,
    'login[username]' => 'myusername',
    'login[password]' => 'mypassword',
    'send' => 'Anmelden'
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://b2b.chiemsee.com/customer/account/login/");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // to allow redirections
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // to avoid error
curl_setopt($ch, CURLOPT_POST, true); // to send info
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt'); // to read data
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // to get the html
$response = curl_exec($ch);
if (curl_error($ch)) {
    $error_msg = curl_error($ch);
    var_dump($error_msg);
    exit;
}
curl_close($ch);
echo $response;

因此,介紹一下正在發生的事情。

form_key似乎用作CSRF保護的一種形式,特別是它是一個同步令牌

簡而言之,發生的情況是,用戶訪問網頁,該網頁為該用戶創建會話並生成唯一令牌。 然后,服務器將該唯一令牌作為隱藏字段附加到表單。 當用戶提交該表單時,它將與該隱藏字段一起提交,並且服務器可以交叉引用接收到的令牌與發送的令牌相同。

但是,關鍵部分是服務器需要知道用戶是誰,而這是通過會話完成的。 通過會話cookie維護會話,因此需要會話cookie。

為了正確模擬真實用戶,您需要存儲服務器在第一次訪問時發送給您的cookie,這可以使用CURLOPT_COOKIEJAR來完成:

$ch2 = curl_init();
curl_setopt($ch2, CURLOPT_URL, "https://b2b.chiemsee.com/customer/account/login/");
curl_setopt($ch2, CURLOPT_FOLLOWLOCATION, true); // to allow redirections
//curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, FALSE); // to avoid error
curl_setopt($ch2, CURLOPT_POST, true); // to send info
curl_setopt($ch2, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch2, CURLOPT_COOKIEJAR, 'cookie.txt'); // to save session cookie
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true); // to get the html
$response2 = curl_exec($ch2);
curl_close($ch2);

完成此步驟並通過抓取頁面檢索CSRF令牌后,您需要將其與相應的Cookie一起提交:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://b2b.chiemsee.com/customer/account/loginPost/"); //Make sure you have the correct URL
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // to allow redirections
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // to avoid error
curl_setopt($ch, CURLOPT_POST, true); // to send info
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt'); // to read cookie data
curl_setopt($ch2, CURLOPT_COOKIEJAR, 'cookie.txt'); // optional, this will update existing cookies and add new ones if needed.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // to get the html
$response = curl_exec($ch);
if (curl_error($ch)) {
    $error_msg = curl_error($ch);
    var_dump($error_msg);
    exit;
}
curl_close($ch);
echo $response;

這應該允許服務器首先加載為其創建CSRF令牌的正確會話,然后驗證您要發送的令牌。

附帶說明:令牌的原因很簡單。 如果我制作的網頁欺騙用戶直接發布到另一個網頁,則該令牌是我永遠無法訪問的數據,因為它僅與用戶直接共享,因此第三者無法訪問它。 它可能會使像您這樣的自動化系統難以實施,但對於用戶安全性非常有效。

您的要求是不可能的。 不是因為cURL或PHP的限制,而是因為因果關系。 考慮正在執行的邏輯步驟集。 為了發送登錄請求,您必須首先知道form_key 為了知道form_key您必須首先請求登錄頁面。

簡而言之,您無法利用尚未擁有的信息。

  • 發出第一個HTTP請求以獲取表單信息。 (特別是您需要的form_key值。)
  • 發出第二個HTTP請求以發送登錄信息,包括您從第一個請求中獲取的數據。

沒有理由試圖一步一步地做到這一點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM