首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用API响应填充Contact Form 7发布的数据

使用API响应填充Contact Form 7发布的数据
EN

Stack Overflow用户
提问于 2021-11-20 09:02:20
回答 1查看 77关注 0票数 1

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

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

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

代码语言:javascript
复制
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 );

            }
        }
    }
}
EN

回答 1

Stack Overflow用户

发布于 2021-11-21 16:15:35

听起来您想要做的是首先使用字符串替换来更新邮件正文,而不是使用一些隐藏变量。这样,您就可以在联系人表单的邮件选项卡中执行以下操作。其中,{{api_response}}将替换为您在函数Kiri_cf7_api_sender中定义的任何内容

代码语言:javascript
复制
/** 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

代码语言:javascript
复制
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的响应,可以通过。

代码语言:javascript
复制
<script type="text/javascript">
    document.addEventListener('wpcf7mailsent', function (event) {
        console.log(event.detail.my_api_response);
    }, false);
</script>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70044554

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档