Form submitting to external Vtiger CRM with wp_remote_post unsuccessfully

Question

I have this simple HTML form:

<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<form id="__vtigerWebForm" name="Name 1" action="http://my.crm.ip/bbs/modules/Webforms/capture.php" method="post" accept-charset="utf-8" enctype="multipart/form-data">
    <input type="hidden" name="__vtrftk" value="sid:0000000000000000000000000000000000000000,0123456789" /><input type="hidden" name="publicid" value="0123456789abcdef" />
    <input type="hidden" name="urlencodeenable" value="1" /><input type="hidden" name="name" value="Name 1" />
    <table>
        <tbody>
            <tr>
                <td><label>last_name*</label></td>
                <td><input type="text" name="lastname" data-label="" value="" required=""></td>
            </tr>
            <tr>
                <td><label>first_name</label></td>
                <td><input type="text" name="firstname" data-label="" value=""></td>
            </tr>
            <tr>
                <td><label>main_email</label></td>
                <td><input type="email" name="email" data-label="" value=""></td>
            </tr>
            <tr>
                <td><label>cell_phone</label></td>
                <td><input type="text" name="mobile" data-label="" value=""></td>
            </tr>
            <tr>
                <td><label>Privacy</label></td>
                <td><input type="checkbox" name="privacy_policy" data-label="" value="" required></td>
            </tr>
        </tbody>
    </table>
    <button type="submit">Submit</button>
</form>

Try submitting it, I get response {"success":true,"result":"ok"}
but If in insert in a WordPress page modified this way:

public function get_code() : string {

    $form_action = admin_url( 'admin-post.php' );
    $nonce_field = wp_nonce_field( 'form_submission', 'my_nonce' );

    $html = <<<HTML
<form action="$form_action" method="post">
    $nonce_field
    <input type="hidden" name="action" value="form_submission">
    <table>
        <tbody>
            <tr>
                <td><label>last_name*</label></td>
                <td><input type="text" name="lastname" data-label="" value="" required=""></td>
            </tr>
            <tr>
                <td><label>first_name</label></td>
                <td><input type="text" name="firstname" data-label="" value=""></td>
            </tr>
            <tr>
                <td><label>$main_email</label></td>
                <td><input type="email" name="email" data-label="" value=""></td>
            </tr>
            <tr>
                <td><label>cell_phone</label></td>
                <td><input type="text" name="mobile" data-label="" value=""></td>
            </tr>
            <tr>
                <td><label>Privacy*</label></td>
                <td><input type="checkbox" name="privacy_policy" data-label="" value="" required></td>
            </tr>
        </tbody>
    </table>
    <button type="submit">Submit</button>
</form>
HTML;

    return $html;

}

and in callback function:

public function form_submission_callback() {

    if ( !wp_verify_nonce( $_POST['my_nonce'], 'form_submission' ) ) {
        die( __( 'Check failed.', 'my_nonce' ) );
    }

    status_header(200);

    $last_name = trim( sanitize_text_field( 'lastname' ) );
    $first_name = trim( sanitize_text_field( 'firstname' ) );
    $email = sanitize_email( 'email' );
    $mobile = trim( sanitize_text_field( 'mobile' ) );

    if ( !empty( $last_name ) && ( empty( $email ) || is_email( $email ) ) ) {
        $url = 'http://my.crm.ip/bbs/modules/Webforms/capture.php';
        $payload = array(
            'method'        => 'POST',
            'timeout'       => 30,
            'redirection'   => 5,
            'httpversion'   => '1.0',
            'blocking'      => true,
            'headers'       => array( 'Content-Type' => 'text/html; charset=utf-8' ),
            'body'          => array(
                '__vtrftk'          => 'sid:0000000000000000000000000000000000000000,0123456789',
                'publicid'          => '0123456789abcdef',
                'urlencodeenable'   => '1',
                'name'              => 'Name 1',
                'lastname'          => $last_name,
                'firstname'         => $first_name,
                'email'             => $email,
                'mobile'            => $mobile
            )
        );
        $response = wp_remote_post( $url, $payload );
        $response_body = wp_remote_retrieve_body( $response );

        $response_data = ( ! is_wp_error( $response_body ) ) ? json_decode( $response_body, true ) : null;
        var_dump( $response_data );

    } else {
        echo '<h1 style="color: red;">Data input NOT valid!</h1>';
    }

    die();

}

I get this error: array(2) { ["success"]=> bool(false) ["error"]=> array(1) { ["message"]=> string(18) "Webform not found." } }

What’s wrong? Any help, please?

0
icolumbro 4 months 0 Answers 21 views 0

Leave an answer