简体   繁体   English

PHP cURL表单值已设置,但未设置提交返回值

[英]PHP cURL form value set but submit returns value not set

I am currently using PHP cURL to submit two forms. 我目前正在使用PHP cURL提交两种形式。 Both forms are on different pages (after completing one form, you are redirected to another). 两种形式都在不同的页面上(填写一种形式后,您将被重定向到另一种形式)。 I get cURL to submit the first form and the second. 我得到cURL提交第一个表格和第二个表格。 However, the second form (after submitting) tells me that one value (the required value) is not set. 但是,第二种形式(提交后)告诉我未设置一个值(所需值)。 So obviously, this means to me that the value is not set. 显然,这对我来说意味着没有设置该值。 However, I believe I am setting the variable correctly. 但是,我相信我正确设置了变量。 I cannot paste the entire form here (over the limit..) but here is the value that is not being set for submission: 我无法在此处粘贴整个表单(超出限制..),但是此处未设置要提交的值:

     <select name="sel_subj" size="10" multiple="" id="subj_id">
        <option value="ACAD">Academic Foundations
        </option>
    .. a lot more options
     </select>

Here is my code: 这是我的代码:

    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;
}

Here is the POST variables from when I manually submit the form (from firebug): 这是我手动提交表单(来自Firebug)时的POST变量:

POST variables POST变量

However, when I use the above function, it goes through the first form without any problems, then hits the second form, submits it, and then the result comes back as saying 'You must select at least ONE subject .'. 但是,当我使用上述功能时,它会顺利通过第一种形式,然后单击第二种形式,然后提交,然后返回结果,说“您必须选择至少一个主题”。 As if I did not select the 'sel_subj'. 好像我没有选择'sel_subj'。

I have no idea why it is not submitting with the 'sel_subj' data being what I set it to. 我不知道为什么它不提交“ sel_subj”数据作为我设置的数据。 Any help would be greatly appreciated. 任何帮助将不胜感激。

Link to first form: https://banner.uregina.ca/prod/sct/bwckschd.p_disp_dyn_sched 链接到第一个表格: https : //banner.uregina.ca/prod/sct/bwckschd.p_disp_dyn_sched

Second form cannot be directly linked to, only after completion of the first. 仅在完成第一个表格后,才能直接将第二个表格链接到。

according to the picture, your web browser is using application/x-www-form-urlencoded encoding, but the way you're using curl, curl is doing multipart/form-data encoding. 根据图片,您的Web浏览器正在使用application/x-www-form-urlencoded编码,但是您使用curl的方式是curl进行了multipart/form-data编码。 some websites are sensitive to this. 一些网站对此很敏感。 to make curl use application/x-www-form-urlencoded encoding, use http_build_query like this 要使curl使用application/x-www-form-urlencoded编码,请使用像这样的http_build_query

    curl_setopt ( $curl_connection, CURLOPT_POSTFIELDS, http_build_query($data_fields) );

edit: found another mistake you're doing, when you do 编辑:发现您正在执行的另一个错误

        'sel_subj' => 'ACAD',
        'sel_subj' => 'dummy',

you overwrite sel_subj with just "dummy", which gives you the error. 您仅用“虚拟”覆盖sel_subj,这会给您带来错误。 the correct way to do this is 正确的方法是

        'sel_subj' => array('ACAD','dummy'),

that's why you're getting the subject error, "dummy" is not a valid subject :p 这就是为什么您收到主题错误的原因,“虚拟”不是有效的主题:p

.. shame on PHP for not throwing an error/warning here ..在PHP上感到羞耻,因为这里没有抛出错误/警告

What worked for me was changing the way I sent in the parameters.. (changed into string format like a GET) 对我有用的是更改我发送参数的方式。(更改为像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";

Then used it with the following: 然后将其与以下内容一起使用:

curl_setopt ( $curl_connection, CURLOPT_POSTFIELDS,$data);

As per instructions from this site: 根据本网站的说明:

https://yeehuichan.wordpress.com/2011/08/07/sending-multiple-values-with-the-same-namekey-in-curl-post/ https://yeehuichan.wordpress.com/2011/08/07/sending-multiple-values-with-the-same-namekey-in-curl-post/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM