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;
}