Get api and change vaue in a form

By: Thompson Fiuzzi | Asked: 02/23/2024
ForumsCategory: Code SnippetsGet api and change vaue in a form
Thompson Fiuzzi asked 3 months ago

Hi, I'm implementing some POST and GET functions for my site with formidable.
When sending the form I send the data as POST and I was also able to intercept the response and write it in a field.

Now my goal is the following:
After sending the data and saving the response: Correct sending.

I need to perform an action that queries a portal via GET API to check the status of my request. I tried like this but it doesn't work:

function updateFormFieldWithAPI() {
$api_url = 'https://urlgetapi xxxx;
$api_username = 'xxxx';
$api_password = 'xxxxx'; $response = wp_remote_get( $api_url, array(
'headers' => array(
'Authorization' => 'Basic' . base64_encode( $api_username . ':' . $api_password )
)
) ); if ( is_wp_error( $response ) ) {
// Handle error
} else {
$body = wp_remote_retrieve_body( $response );
$data = json_decode( $body, true ); $returned_id = $body["status"];// this line will change based on the API you are sending to
if ( $returned_id ) {
FrmProEntryMeta::update_single_field( array(
'entry_id' => $entry->id,
'field_id' => 951, // change to the ID of the field to change
'value' => $returned_id,
) );
}
}
}

function schedule_update_status() {
if ( ! wp_next_scheduled( 'updateFormFieldWithAPI' ) ) {
wp_schedule_event( time(), 'every_15_seconds', 'updateFormFieldWithAPI' );
}
}

add_action( 'wp', 'schedule_status_update' ); // Callback for the 15 second interval
function run_update_status() {
updateFormFieldWithAPI();
}
add_action( 'updateFormFieldWithAPI', 'execute_update_status' );
Define your custom 15 second interval:
add_filter( 'cron_schedules', 'add_interval_15_seconds' );
function add_interval_15_seconds( $schedules ) {
$schedules['every_15_seconds'] = array(
'interval' => 15,
'display' => __( 'Every 15 Seconds', 'textdomain' ),
);
return $schedules;
}

Question Tags:
1 Answers
Victor Font answered 2 months ago
Have you tried using the frm_after_create_entries hook? WordPress cron is highly unreliable because it's only triggered when someone visits your site.

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