Hi,
I am trying to autofill a webform and submit data using php. I have been able to do this in VBA. however I am new to php and tried using php-cURL combination to do, but it doesn’t work. I could log into a wordpress site with it, so I tried replicating the same to submit a webform but not able to do so. Please help me out.
Here is the code that I have tried:
[php]
$post_data[‘firstname’] = ‘test’;
$post_data[‘lastname’] = ‘test’;
$post_data[‘email’] = ‘test’;
$post_data[‘phone’] = ‘4839842293’;
$post_data[‘city’] = ‘NewYork’;
//this is the important part - must put the submit button in the array
$post_data[‘gform_submit_button_1’] = ‘Submit’;
//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://www.exmaple.com/form.php/’);
//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 request1
$result = curl_exec($curl_connection);
//show information regarding the request
print_r(curl_getinfo($curl_connection));
echo curl_errno($curl_connection) . ‘-’ .
curl_error($curl_connection);
//close the connection
curl_close($curl_connection);
[/php]
What site? Do they have an API?
[member=72272]astonecipher[/member] Right now, I am trying it for a wordpress site, but later on the goal is to do it for lyft.com and they do have API I am sure. However, at the very basic level, I am able to prefill the form on the wordpress site through vba, but when I apply the same logic in PHP, nothing happens
I bet the wordpress site is reliant on the submit button being passed. Add a post value for the name of the submit button and check.
[member=72272]astonecipher[/member] I already have the post value for the name of the submit button in the code
Didn’t notice that. Are you getting anything back from dumping the curl request?
Why are you submitting to a contact form? Looking thru all the code, this looks like a spam bot.
It is not to spam anyone, it is done for our company, we have a list of 100s of people who we want to sign up on our site, and doing it manually for each one will take forever.
Then why hit the form at all? Write a script to directly hit the database and loop thru dropping the values in that way?
Oh I didn’t know that was possible. That would actually be better to do. I am able to log in to the website through a php script I wrote, however when I try to access the elements of the website , they don’t work.
Here is the code
[php]
$username = ‘user’;
$password = ‘pass’;
$url = “http://example/wp-login.php”;
$cookie="cookie.txt";
$postdata = “log=”.$username ."&pwd=".$password ;//. “&wp-submit=Log%20In&redirect_to=” . $url . “blog/wordpress/wp-admin/&testcookie=1”;
$ch = curl_init();
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec ($ch);
// 3. Continue loging into wordpress
$login = curl_init ($url);
curl_setopt($login, CURLOPT_COOKIESESSION, 1);
curl_setopt($login, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($login, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($login, CURLOPT_TIMEOUT, 40);
curl_setopt($login, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($login, CURLOPT_HEADER, 1);
curl_setopt($login, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($login, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($login, CURLOPT_POST, 1);
curl_setopt($login, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
echo curl_exec($login);
curl_close($ch);
exit;
[/php]
In this code if you see as soon as I put the " wp-submit=Log%20In&redirect_to=" . $url ." part it, doesn’t log in, however works otherwise. But still it doesn’t go to the site’s actual url but shows the localhost/test.php in the web browser. Please know I am using PHP first time, and just using online resources and examples to accomplish the goal.
The database is the best route to take for your end goal.
Check the tables for which table you need to insert the data into. Then work on a script that will loop through your list and insert n number of entries.
[member=72272]astonecipher[/member] I did, but as I said I am not able to access the contents on the website after I log in through php.