I have a contact form 7 form that’s sending information to an API. I want to display the API response on the front end.
I record the user information, then post it using wp_remote_post, with my example url. I want to be able to show the API response / $body on the front end. I have set it up to capture it in the debug log, but want to know if there is a way to post it to the frontend where my form is.
Normal Var Dump and print_r do not work as contact form 7 is an Ajax function.
add_action('wpcf7_mail_sent','Kiri_cf7_api_sender');
function Kiri_cf7_api_sender ( $contact_form) {
if ( $contact_form->title === 'Quote_form')
{
$submission = WPCF7_Submission::get_instance();
if ($submission)
{
$posted_data = $submission->get_posted_data();
$name = $posted_data["your-name"];
$surname = $posted_data["your-name2"];
$phone = $posted_data["tel-922"];
//example url//
$url = www.mytestapi.com?$name&$surname&$phone;
$response = wp_remote_post ($url);
$body = wp_remote_retrieve_body( $response );
ob_start(); // start buffer capture
var_dump($name);
var_dump($surname);
var_dump($phone);
$contents = ob_get_contents(); // put the buffer into a variable
ob_end_clean(); // end capture
error_log($contents);
}
}
}