Fill out form and submit by API / PHP

By: Michael M | Asked: 07/10/2023
ForumsCategory: Code SnippetsFill out form and submit by API / PHP
Michael M asked 10 months ago
Hi! Does anyone know of any example code to demonstrate filling in and submitting forms from PHP, I assume using Formidable_API addon, unless there are other more direct function calls that could be used from same-site ? Example would hopefully demonstrate some or all of these:
  • form id = 1,
  • form is located on the same site as the PHP server code
  • form has 2 text boxes that would be filled out, "name" and "age"
  • form also one dynamic field (random number) that would ideally be generated by the form, but could also be inserted by the PHP code
  The backstory is that a script would iterate over a folder full of text files on the server, grabbing name and age details, and submitting them to the form automatically. A one-time task to save manually re-typing 1000's of existing details into a new Formidable based server application. Might be horribly wrong (and not yet working), but sharing what I've been playing with so far:
$api_key = '1111-2222-3333-4444';         $url = 'https://mysite.com/wp-json/frm/v2/forms/1/entries/'; // change your url         $api_parameters = [             'form_id' => 1, //  entry form id             'name' => 'Bob',             'age' => '42'         ];  
$response = wp_remote_post( $url, array(             'method' => 'POST',              'timeout' => 45,             'headers' => array( 'Authorization' => 'Basic ' . base64_encode( $api_key .':x' ) ),             'body' => json_encode($api_parameters),             )         );  
Question Tags:
Michael M replied 10 months ago

Ah... I might have found a path to solve this more simply...
https://formidableforms.com/knowledgebase/php-examples/#kb-create-an-entry-with-php

Somehow I couldn't find that article initially, but it does seem to be more like what I need.
Experimentation in progress!

1 Answers
Victor Font answered 9 months ago
When I have a requirement to upload data like yours, I create a PHP function using the low level PHP file functions and process each line in the CSV in order. I execute the function at the bottom of functions.php. Doing this, I loaded 480 records in less than 1 second. It took about 30-minutes to write the function. You can also just do a CSV import. It seems to me the API route is overkill.
Michael M replied 9 months ago

Thanks, and I agree that the API route is overkill for what I wanted. I just hadn't found the PHP instructions linked above when asking the question. Since then, having found the PHP public functions, I've written the importer function already and it was indeed simple and fabulous!

Anyone finding this later should definitely consider the PHP guide linked above!

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right