2012-03-03 84 views
0

我一直在尋找一些東西,可以幫助我與此。如何在提交回前自動生成隱藏的輸入

這是我想要的捲曲度

<form action="http://www.website.com/login?from=login" class="lightbox_form" method="post" name="login"> 
<input name="authenticity" type="hidden" value="da1249c71258cd02a8bd038cf3ccca1c9ebef155" /> 
<input type="text" name="login_or_email" size="20" class="input" id="login"> 
<input type="password" size="20" name="login_password" class="input" id="word_user"> 
<button type="submit" class="new_submit"><span class="inner">Log In</span></button> 
</form> 

模擬登錄如果看到你看到下面

<input name="authenticity" type="hidden" value="da1249c71258cd02a8bd038cf3ccca1c9ebef155" /> 

即自動生成,改變了每次重裝網站隱藏價值例子的形式。

我正在使用CURL,但我不知道如何獲取隱藏字段並在同一會話中提交回來。嘗試使用get_meta_tags但沒有工作

set_time_limit(0); 
$http_agent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13 (.NET CLR 3.5.30729)"; 
$header = array(); 
$header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,"; 
$header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"; 
$header[] = "Cache-Control: max-age=0"; 
$header[] = "Connection: keep-alive"; 
$header[] = "Keep-Alive: 300"; 
$header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7"; 
$header[] = "Accept-Language: en-us,en;q=0.5"; 
$header[] = "Pragma: "; // browsers keep this blank. 

curl_setopt($ch, CURLOPT_URL, "http://www.website.com/login?from=login"); 

curl_setopt($ch, CURLOPT_USERAGENT, $http_agent); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 
curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt ($ch, CURLOPT_COOKIEJAR, ROOT_DIR.'cookies.txt'); 
curl_setopt($ch, CURLOPT_AUTOREFERER, true); 
//curl_setopt($ch, CURLOPT_POSTFIELDS, 'login_or_email=mail&login_password=pass&authenticity_token='.$cag.''); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); 


$output = curl_exec($ch); 
$info = curl_getinfo($ch); 
curl_close($ch); 



//print $output; 
print_r ($output); 

任何想法?

回答

1

我相信你必須手動解析由curl_exec調用返回的HTML。 curl庫不會爲你處理HTML,它會處理HTTP的東西,但處理從服務器返回的數據取決於你的應用程序。

1

您可以使用pret_match()來獲得隨機值

//$html is the return of the curl request 
preg_match('/<input name="authenticity" type="hidden" value="([^"]*)" \/>/', $html, $match); 

值將在$匹配[1]。

+0

耶這就是偉大的,但如果我得到隨機值,捲曲和發回的捲曲度,其將與新的再生.. – radiaku 2012-03-05 17:06:25

+0

我不明白您的評論。您具有$ match [1]中隱藏字段的值,因此您可以在下一個curl請求中使用它。 – botzko 2012-03-06 18:07:56

+0

是的,我已經有$ match [1]了,但是當我再次創建CURL時,它會生成一個新的。所以我必須得到隱藏的價值,並將其送回一個CURL,這是否可行? – radiaku 2012-03-06 19:04:18

0
$regex= '/<input name="authenticity" type="hidden" value="([^"]*)" \/>/i'; 
if (preg_match($regex, $html, $list)) 
    $rand= $list[0]; 
else 
    print "Not found"; 
+0

歡迎來到Stack Overflow。請考慮編輯您的答案,以包含關於您的代碼如何工作的解釋,以便對未來的讀者更有用:)。 – Matt 2015-08-09 09:49:07

相關問題