Script Curl Submit Form

Curl form submission to signify automated scripting

Live stream set for 2025-04-30 at 14:00:00 Eastern

Ask questions in the live chat about any programming or lifestyle topic.

This livestream will be on YouTube or you can watch below.

Automate Data Submission

Web forms can be submitted using the command line tool curl. All web forms must be sanitized and validated to prevent hacking and unintended consequences.

Web developers normally test web forms manually or have a team of testers. Automated form submissions ensure proactive security measures, preventing downtime and potential issues.

To submit web form data, curl can be used to submit data as key/value pairs using HTTP methods such as GET, POST, PUT or DELETE.

The focus of this tutorial will be on creating a custom script to submit form data using either command line curl or PHP and curl.

  1. Create a list of form fields.
  2. Choose command line scripting language such as Bash or PHP.
  3. Create a script to fill in form fields and submit destination.
  4. Manually run the script.
  5. Set up a cronjob to automate the script at the desired interval.

Requirements For cURL

Glossary:

URL

Uniform Resource Locator is the web address on the internet.

HTTP Method

Specifies the method used for form submission.

HTML Action

Specified where to send form data.

Tools

Programming Tools
Name Description Example
Text editor For creating and editing source code Apache Netbeans IDE
SSH Secure Shell Client OpenSSH
Shell Access Access to the command line. Terminal
Name Description Example

HTML Form

<!DOCTYPE html>
<html>
<head>
	<title>Form</title>
</head>
<body>
   <form action="form.php" method="POST">
      <input type="text" name="name" placeholder="Enter Your Name" />
      <input type="number" name="age" placeholder="Enter Your Age" />
      <input type="submit" value="Submit" />
   </form>
</body>
</html>

PHP Form Handler

if ( !empty($_POST['name']) && !empty($_POST['age']) ) {
   // TODO sanitization & validation
   echo json_encode(['status' => 'OK']);
}
die();

PHP HTTP Server

# Start PHP Web Server #
php -S localhost:8000

Submit Form Using cURL

# Submit Form Data Via cuRL #
curl 'http://localhost:8000/form.php' -X POST --data-raw 'name=Edward&age=23'

Submit Form Using PHP cURL

/*
 * submit-form.php
 * 
 * Copyright 2025 Edward Ojambo <https://ojambo.com>
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 * 
 * 
 */
$url = 'http://localhost:8000/form.php';
$data = ['name' => 'Edward', 'age' => 23];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
if( !$result )
{
   die('Error: "' . curl_error($ch) . '" - Code: ' . curl_errno($ch));
}
curl_close($ch);
var_dump($result);

Optional Cron Job Running Twice A Month

# Runs At Midnight On The 1st And 15th Every Month #
0 0 1,15 * *	/path/to/your/script.sh

Explanation:

  1. Submit form data using the curl command.
  2. Check the output for successful submission.
  3. The pseudo code is to simply loop for multiple submissions if desired.

The code for finding forms and parsing was not provided in the tutorial. Testing form submissions can be done using curl easily if the form fields are known.

cURL Submitting Form Data
cURL Command Line Tool Submitting Form Data To Destination

PHP cURL Submitting Form Data
PHP cURL Extension Submitting Form Data To Destination


Usage

You can run cURL on the command-line, or integrated into PHP as an extension. For this tutorial, cURL was used to submit form data. The command line tool and library curl can be downloaded from curl is also libcurl. PHP scripting language can be downloaded from PHP. Then the downloaded file is extracted directly on the server or locally before individual files are uploaded if applicable.

Open Source

The command-line tool cURL is licensed under the curl Licensed which is inspired by MIT/X. The permissive license has conditions requiring preservation of copyright and license notices. It allows commercial use, modification, distribution, and allows making derivatives proprietary, consult the license for more specific details.

The PHP scripting language is licensed under the PHP License. The permissive license has conditions requiring preservation of copyright and license notices. Redistribution is permitted in source or binary form with or without modifications, consult the license for more specific details.

Conclusion:

Web developers can test form submissions of known form fields using the command line tool curl or as the library in a scripting language such as PHP. Manage form submission tests manually or automated by cron jobs using cURL on the command line or via PHP.

If you enjoy this article, consider supporting me by purchasing one of my WordPress Ojambo.com Plugins or programming OjamboShop.com Online Courses or publications at Edward Ojambo Programming Books or become a donor here Ojambo.com Donate

References:

Leave a Reply

Your email address will not be published. Required fields are marked *