[英]cURL POST not submitting the form
我正在做一個項目,要求之一是自動將表單提交到不定期指定的隨機站點
這是我的cURL代碼:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url_post);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $dati_post);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
獲取POST參數的代碼:
foreach ($html->find('form[method=post],form[method=POST]') as $forms_post) {
$n_formPOST++;
$formPOST_action = $forms_post->action;
foreach ($forms_post->find('input') as $input) {
if ($input->type == 'text') {
$dati_testo[$input->name] = "text";
} else if ($input->type == 'email' || $input->name == 'email') {
$dati_random[$input->name] = "emailrandom@gmail.com";
} else if ($input->type == 'hidden') {
$dati_random[$input->name] = $input->value;
} else {
$dati_random[$input->name] = "random";
}
}
foreach ($forms_post->find('textarea') as $textarea) {
if ($textarea->disabled != true) {
$dati_testo[$textarea->name] = "text";
}
}
foreach ($forms_post->find('button') as $bottone) {
if ($bottone->type == 'submit') {
$dati_random[$bottone->name] = "random";
}
}
問題是在某些站點中POST正確完成,並且我收到正確的答案,該答案與手動完成操作會收到的答案相對應。 在其他站點上,似乎未提交該表單。 我已經反復檢查了我在cURL中插入的URL,以及通過的數據,如果我手動使用它們,它將起作用。 我什至嘗試使用執行POST / GET的在線工具,並傳遞與我在項目中獲得的相同的URL和相同的數據,並且該工具可以正常工作。
url_post由url host + form動作組成。 我不知道我的curl代碼中是否有問題,因為我很確定我傳遞給curl的數據正確無誤,可以完成POST。
資料:
您需要使用curl_exec()
函數才能執行cURL。 它需要$ch
參數,如下所示:
// Execute cURL
curl_exec($ch);
// Close cURL
curl_close($ch);
更多信息在這里
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.