使用API响应填充联系人表格7发布的数据



我正在尝试用PHP变量$body$body填充$posted_data["dynamichidden-458"];。这是一个API响应,我想将其添加到数据中,然后将其发送到数据层以稍后捕获,下面是它如何发送和接收API信息的示例代码。使用wpcf7_before_send_mail,我发送信息以从API检索响应,然后我将在其中添加wpcf7_mail_sent以发布所有所需的信息。

我用调试器捕获了$body变量,它允许我调试对日志的响应,但它无法填充$posted_data["dynamichidden-458"];,我尝试过的其他变体包括$posted_data["dynamichidden-458"] = $body;,但它仍然不会在dynamichidden-458"下发布$body变量。

还可以做些什么来尝试用我的PHP变量填充发布的数据?

add_action( 'wpcf7_before_send_mail', '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'];
$urltest = $posted_data['dynamichidden-739'];
$body    = $posted_data['dynamichidden-458'];
if ( strpos( $urltest, '?phone' ) !== false ) {
$url = 'api string';
} elseif ( strpos( $urltest, '?email' ) !== false ) {
$url = 'api string';
} else {
$url = 'api string';
$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 );
var_dump( $url );
var_dump( $urltest );
var_dump( $body );
$contents = ob_get_contents();  // put the buffer into a variable
ob_end_clean();                 // end capture
error_log( $contents );
return;
print_r( $submission );
}
}
}
}

听起来你想做的是首先用字符串替换来更新邮件正文,而不是使用一些隐藏的变量。有了这个,你可以在联系人表单的邮件选项卡中做这样的事情。其中{{api_response}}将被你在函数Kiri_cf7_api_sender中定义的任何内容所取代

/** Contact Form 7 Mail Tab in Editor **/
Dear [your-name],
This is the email body...
{{api_response}}
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."

所以现在对于函数Kiri_cf7_api_sender

add_action( 'wpcf7_before_send_mail', 'Kiri_cf7_api_sender' );
function Kiri_cf7_api_sender( $contact_form ) {
if ( 'Quote_form' === $contact_form->title ) {
$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'];
$urltest = $posted_data['dynamichidden-739']; // Not sure if this should be a form field, or just some kind of option field.

if ( strpos( $urltest, '?phone' ) !== false ) {
$url = 'api string';
} elseif ( strpos( $urltest, '?email' ) !== false ) {
$url = 'api string';
} else {
$url = 'api string';
$response = wp_remote_post( $url );
$body     = wp_remote_retrieve_body( $response );
}
}
// Get the email tab from the contact form.
$mail = $contact_form->prop( 'mail' );
// Retreive the mail body, and string replace our placeholder with the field from the API Response.
// Whatever the api response is within the $body - if you have to json decode or whatever to get it.
$mail['body'] = str_replace( '{{api_response}}', $body['field'] , $mail['body'] );
// Update the email with the replaced text, before sending.
$contact_form->set_properties( array( 'mail' => $mail ) );
// Push a response to the event listener wpcf7mailsent.
$submission->add_result_props( array( 'my_api_response' => $body ) );
}
}

这至少应该发送电子邮件,并允许您将API响应添加到电子邮件中。

它还推送对javascript事件wpcf7mailsent的响应,该响应可由.找到

<script type="text/javascript">
document.addEventListener('wpcf7mailsent', function (event) {
console.log(event.detail.my_api_response);
}, false);
</script>

最新更新