How to submit a form via curl in php?

How to submit a form by curl
Lets take and example this is your submit form

<form action="website.com/signup" method="post">
<input type="text" name="email" />
<input type="submit" name="submit" value="Submit" />
</form>

and you want to submit this form by curl
So,

Simple copy and paste this in you php file and change field name and form action url and run the file
This code will help you submit a form via curl




<?php $fields_string ='';
$url = 'website.com/signup';
$fields = array(
'EMAIL' => $_POST['email']
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
?>

Comments