Silently POST Submitted Data to Any API or URL

Heads up! This article contains PHP code and is intended for developers. We offer this code as a courtesy but don't provide support for code customizations or 3rd party development.

Would you like to send the submitted data to an external API endpoint or a specific URL that is designed to retrieve them? This is quite useful if you want to automatically sync your submitted data with a 3rd-party service.

Setup

To send your submissions to an API endpoint, copy the code shown below and place it into the PHP Scripts -> After Form Submission area of your form.

// Populate the data that will be sent
$data = [
	'first_name' => $submission->params['first_name'],
	'last_name'  => $submission->params['last_name'],
	'email' 	 => $submission->params['email']
];

// Set the URL that will retrieve the data
$url = 'http://api.example.com/accounts';

$data = json_encode($data);

$response = \Joomla\CMS\Http\HttpFactory::getHttp()->post($url, $data);

// Do something if the data were sent successfully
if ($response->code == 200)
{
	// Your code..
}
Last updated on Nov 26th 2024 12:11