2012-03-03 41 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

相关问题