Autofill webform php

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.

Show us the code that you’ve tried without success.

<?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);
?>

I just posted the code

Sorry, I meant to also add, can you expand on this? Does it not post, does it throw an error, exactly what happens in your code? For example you have the code at the end to show the information, what does that give you?

And

$curl_connection = 
  curl_init('http://www.exmaple.com/form.php/');

should the trailing / be present here in the URL?

i mean to say that there is no output and nothing gets filled on the text box on the form. Is there a way to rather directly put the information in the database?

it doesn’t matter to have the trailing / in the url

If you add some echo() statements throughout the code, can you see how far through it gets before it stops?

That depends. Who has control over the site that hosts the form? If it’s yours, then yes, you could just take the data and by-pass the form entirely, just do whatever it is your code would do when the user processes the form. I imagine it’s outside of your control, though, otherwise you wouldn’t be doing it this way. If it’s not your site, have you asked the site owner whether they have an API you can use instead of doing it this way?

Is it possible that the form processing software is rejecting the data you are posting, for example as your code above has an email address that’s not valid. Or is that sanitised for the forum post?

Thanks for the input. Yes it is our company site and i have control over it. It is hosted in godaddy and it is a wordpress site. I have been able to log into through php but when I try to access the database after, I am not able to fetch it. But sending the data directly to the database would be ideal to do. The form is a gravity form embedded in the webpage. Also, I do not understand why this works in VBA, but when I apply the same logic in php, it doesn’t. I did sanitise the url for the forum post.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.