PHP wordpress wp_safe_remote_post 将服务器响应写入文本文件

发布于 2025-01-12 13:49:12 字数 1439 浏览 1 评论 0原文

在 Wordpress (PHP) 中,我使用 Contact Form 7 (CF7) 作为 UI,并通过 API 使用下面的 on_submit 函数发送数据。希望记录成功或失败的响应数据以确保一切正常,因此我想使用函数 custom_logs 将其写入一个简单的文本文件。它确实会写入文本文件,但该过程永远不会继续到 CF7 插件来指示 POST 是成功还是失败,它只是在 UI 端点击提交后挂起。我相信写入文件的某些事情会中断流程,然后就从 CF7 的 POV 停止。我很少用 PHP 写任何东西,所以不能 100% 确定问题是什么?任何人都知道如何解决它,我找不到任何东西。谢谢

function on_submit( $form, &$abort, $submission )
{
    $data = $submission->get_posted_data();

    $firstname = sanitize_text_field($data['first-name']);

    $response = wp_safe_remote_post("https://www.api.com", [
        'body' => json_encode([
            'firstname' => $firstname,
        ]),
    ]);

    if ( is_wp_error($response) ) {
        $abort = TRUE;

        $body = wp_remote_retrieve_body($response);
        $result = json_decode($body);

        $submission->set_response($result->error);
        $submission->set_status('api_failed');
    } else {
        $abort = FALSE;

        $body_success = wp_remote_retrieve_body($response);
        $result_success = json_decode($body_success);
        
        custom_logs("WP API: " . $result_success);
    }
}
add_action('wpcf7_before_send_mail', 'on_submit', 10, 3);


function custom_logs($message) { 
    if(is_array($message)) { 
        $message = json_encode($message); 
    } 
    $file = fopen("custom_logs.log","a"); 
    echo fwrite($file, "\n" . date('Y-m-d h:i:s') . " :: " . $message); 
    fclose($file); 
}

In Wordpress (PHP) i'm using Contact Form 7 (CF7) as the UI and sending the data via an API with the function on_submit below. Would like to have the success or fail response data logged to make sure everything is working so thought i'd write that to a simple text file using the function custom_logs. It does write to the text file but then the process never continues on to the CF7 plugin to indicate the POST was a success or fail, it just hangs after hitting submit on the UI side. I believe something about writing to the file is interrupting the flow and then is just stops from CF7's POV. I rarely write anything in PHP so not 100% sure what the issue is? Anyone have an idea how to solve it, I can't find anything. Thanks

function on_submit( $form, &$abort, $submission )
{
    $data = $submission->get_posted_data();

    $firstname = sanitize_text_field($data['first-name']);

    $response = wp_safe_remote_post("https://www.api.com", [
        'body' => json_encode([
            'firstname' => $firstname,
        ]),
    ]);

    if ( is_wp_error($response) ) {
        $abort = TRUE;

        $body = wp_remote_retrieve_body($response);
        $result = json_decode($body);

        $submission->set_response($result->error);
        $submission->set_status('api_failed');
    } else {
        $abort = FALSE;

        $body_success = wp_remote_retrieve_body($response);
        $result_success = json_decode($body_success);
        
        custom_logs("WP API: " . $result_success);
    }
}
add_action('wpcf7_before_send_mail', 'on_submit', 10, 3);


function custom_logs($message) { 
    if(is_array($message)) { 
        $message = json_encode($message); 
    } 
    $file = fopen("custom_logs.log","a"); 
    echo fwrite($file, "\n" . date('Y-m-d h:i:s') . " :: " . $message); 
    fclose($file); 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文