我目前正在使用PHP cURL提交两个表单。两个表单都在不同的页面上(完成一个表单后,您将被重定向到另一个表单)。我得到cURL提交第一个表格和第二个表格。但是,第二种形式(提交后)告诉我没有设置一个值(所需的值)。很明显,这对我来说意味着价值没有设定。但是,我相信我正确设置变量。我不能在这里粘贴整个表单(超过限制..)但是这里没有为提交设置的值:
<select name="sel_subj" size="10" multiple="" id="subj_id">
<option value="ACAD">Academic Foundations
</option>
.. a lot more options
</select>
这是我的代码:
function submitForms() {
//first form...
$data_fields = array (
'p_term' => '201610',
'p_calling_proc' => 'bwckschd.p_disp_dyn_sched'
);
$curl_connection = curl_init ();
curl_setopt ( $curl_connection, CURLOPT_CONNECTTIMEOUT, 30 );
curl_setopt ( $curl_connection, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)" );
curl_setopt ( $curl_connection, CURLOPT_URL, 'https://banner.uregina.ca/prod/sct/bwckgens.p_proc_term_date' );
curl_setopt ( $curl_connection, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $curl_connection, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt ( $curl_connection, CURLOPT_FOLLOWLOCATION, 1 );
curl_setopt ( $curl_connection, CURLOPT_POSTFIELDS, $data_fields );
// perform our request
$result = curl_exec ( $curl_connection );
// show information regarding the request
print_r ( curl_getinfo ( $curl_connection ) );
echo "<br>";
echo curl_errno ( $curl_connection ) . '-' . curl_error ( $curl_connection );
echo "<br>";
echo $result;
//Second form..
$data_fields = array (
'term_in' => '201630',
'sel_to_cred' => '',
'sel_title' => '',
'sel_subj' => 'ACAD',
'sel_subj' => 'dummy',
'sel_sess' => 'dummy',
'sel_schd' => '%',
'sel_schd' => 'dummy',
'sel_ptrm' => '%',
'sel_ptrm' => 'dummy',
'sel_levl' => '%',
'sel_levl' => 'dummy',
'sel_instr' => '%',
'sel_instr' => 'dummy',
'sel_insm' => '%',
'sel_insm' => 'dummy',
'sel_from_cred' => '',
'sel_day' => 'dummy',
'sel_crse' => '',
'sel_camp' => '%',
'sel_camp' => 'dummy',
'sel_attr' => 'dummy',
'end_mi' => '0',
'end_hh' => '0',
'end_ap' => 'a',
'begin_mi' => '0',
'begin_hh' => '0',
'begin_ap' => 'a'
);
curl_setopt ( $curl_connection, CURLOPT_URL, 'https://banner.uregina.ca/prod/sct/bwckschd.p_get_crse_unsec' ); // use the URL that shows up in your <form action="...url..."> tag
curl_setopt ( $curl_connection, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt ( $curl_connection, CURLOPT_POSTFIELDS, $data_fields );
$result = curl_exec ( $curl_connection );
echo "<br>";
echo $result;
return $curl_connection;
}
这是我手动提交表单时的POST变量(来自firebug):
但是,当我使用上面的函数时,它会通过第一个表单没有任何问题,然后点击第二个表单,提交它,然后结果回来说“你必须选择至少一个主题。”。好像我没有选择'sel_subj'。
我不知道为什么它没有提交'sel_subj'数据是我设置的。任何帮助将不胜感激。
链接到第一个表单:https://banner.uregina.ca/prod/sct/bwckschd.p_disp_dyn_sched
第二种形式不能直接链接,只有在完成第一种形式后才能直接链接。
答案 0 :(得分:2)
根据图片,您的网络浏览器正在使用application/x-www-form-urlencoded
编码,但您使用curl的方式,curl正在进行multipart/form-data
编码。有些网站对此很敏感。要使curl使用application/x-www-form-urlencoded
编码,请像这样使用http_build_query
curl_setopt ( $curl_connection, CURLOPT_POSTFIELDS, http_build_query($data_fields) );
编辑:发现你正在做的另一个错误, 当你做的时候
'sel_subj' => 'ACAD',
'sel_subj' => 'dummy',
你只用“虚拟”覆盖sel_subj,这会给你错误。正确的方法是
'sel_subj' => array('ACAD','dummy'),
这就是为什么你得到主题错误,“虚拟”不是一个有效的主题:p
..因为没有在这里抛出错误/警告而感到羞耻
答案 1 :(得分:1)
对我有用的是改变我发送参数的方式..(改成字符串格式,如GET)
$data = "term_in=201630&sel_subj=dummy&sel_day=dummy&sel_schd=dummy&sel_insm=dummy&sel_camp=dummy&sel_levl=dummy&sel_sess=dummy&sel_instr=dummy&sel_ptrm=dummy&sel_attr=dummy&sel_subj=ADMN&sel_crse=&sel_title=&sel_schd=%25&sel_insm=%25&sel_from_cred=&sel_to_cred=&sel_camp=%25&sel_levl=%25&sel_ptrm=%25&sel_instr=%25&begin_hh=0&begin_mi=0&begin_ap=a&end_hh=0&end_mi=0&end_ap=a";
然后使用以下内容:
curl_setopt ( $curl_connection, CURLOPT_POSTFIELDS,$data);
根据本网站的说明: