简体   繁体   中英

PHP cURL form value set but submit returns value not set

I am currently using PHP cURL to submit two forms. 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. 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):

POST variables

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'.

I have no idea why it is not submitting with the 'sel_subj' data being what I set it to. Any help would be greatly appreciated.

Link to first form: 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. some websites are sensitive to this. to make curl use application/x-www-form-urlencoded encoding, use http_build_query like this

    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. 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

.. shame on PHP for not throwing an error/warning here

What worked for me was changing the way I sent in the parameters.. (changed into string format like a 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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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