简体   繁体   中英

how to auto fill and submit html form with curl

I want to auto fill an html form and submit the form and display the result. I used below code got from here .

<?php
//create array of data to be posted
$post_data['email'] = 'myemail';
$post_data['pass'] = 'mypassword';

//traverse array and prepare data for posting (key1=value1)
foreach ( $post_data as $key => $value) {
    $post_items[] = $key . '=' . $value;
}

//create the final string to be posted using implode()
$post_string = implode ('&', $post_items);

//create cURL connection
$curl_connection = 
  curl_init('http://m.facebook.com/');

//set options
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_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);

//set data to be posted
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);
//perform our request
$result = curl_exec($curl_connection);

print $result;
//show information regarding the request
echo curl_errno($curl_connection) . '-' . 
                curl_error($curl_connection);

//close the connection
curl_close($curl_connection);
?>

and I used Facebook mobile site and gmail to test this code.

I placed the url of login page of those sites in curl_init function, gave the value of name attributes of username and password fields of login page into keys of $post_data array and saved this code as my.php file and placed it in xampp htdocs directory in local machine.

when I browse the my.php, it displays the login page with username field is filled and password field is not filled. according to the code, The expected result is, It should return the successfully logged page because I have provided the correct username and password. also the curl_errno returns 0. that means no error occurred. Then why I can't get the expected result? and Why password field is not filled although username field is filled?

Inspecting the code at http://m.facebook.com/ i see there are some hidden fields that you may (should) try to send. Usually those are there to prevent automated POST.

First get http://m.facebook.com/ and get the hidden fields using some DOM parser and build the query to post them to the action url.

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