How to send HTTP_POST and receive results

https://www.modelaircraft.org/membership/verify is an HTML form, but it makes a AJAX request to https://www.modelaircraft.org/membership/verify?ajax_form=1&_wrapper_format=drupal_ajax to actually get the data. So, it’d be ideal to make a request directly there instead.

I don’t think there’s a practical (and possibly TOS-compliant) way to do that, though, because their form generates a form_build_id NONCE, and the handler validates it. It doesn’t seem like the API is meant for public use.

Technically, you could make a GET request to https://www.modelaircraft.org/membership/verify to get a nonce, and then make the request with it, but that’s probably not kosher. You should probably talk to them and ask them to provide a public API instead.

If they do that, then this code is roughly what you would need. It’s impossible to get it exact until they provide the public API, though, so you’d need to adapt it once they do. This is enough to give you a general idea of the WordPress and PHP functions you could use, though.

// Mimic the AJAX request at https://www.modelaircraft.org/membership/verify
parse_str(
    'form_id=membership_verify_form&_triggering_element_name=op&_triggering_element_value=Submit&_drupal_ajax=1&ajax_page_state%5Btheme%5D=nt_ama&ajax_page_state%5Btheme_token%5D=&ajax_page_state%5Blibraries%5D=ama%2Fama.form_validations%2Cclassy%2Fbase%2Cclassy%2Fmessages%2Ccore%2Fdrupal.ajax%2Ccore%2Finternal.jquery.form%2Ccore%2Fnormalize%2Celfinder%2Fdrupal.elfinder%2Cextlink%2Fdrupal.extlink%2Cformtips%2Fformtips%2Cformtips%2Fhoverintent%2Cnt_ama%2Fentity-print%2Cnt_ama%2Fnt_ama%2Comega%2Fomega%2Cresponsive_menu%2Fresponsive_menu.config%2Cresponsive_menu%2Fresponsive_menu.styling%2Csimple_background_image_formatter%2Fglobal-styling%2Csystem%2Fbase%2Ctb_megamenu%2Ftheme.tb_megamenu',
    $args
);

// This probably won't work because `form_build_id` appears to be a unique value generated by Drupal
$args['form_build_id'] = 'form-pAoNX6EXlS_MsxmfsanTAyq8VXBKDqqm9fvgvAntWSQ';
$args['ama_number']    = 759314;
$args['last_name']     = 'LEE';

$response = wp_remote_post(
    'https://www.modelaircraft.org/membership/verify?ajax_form=1&_wrapper_format=drupal_ajax',
    $args
);

if ( is_wp_error( ( $response ) ) ) {
    throw new Exception( $response->get_error_message() );
}

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

if ( ! $body ) {
    throw new Exception( 'Request failed' );
}

$error_message = trim( strip_tags( $body[0]->data ) ) ?? '';

if ( $error_message ) {
    throw new Exception( $error_message );
}

$success_message = $body[1]->data;
preg_match( '(is valid until )(\d{2}\/\d{2}\/\d{4})', $success_message, $matches );
$date = $matches[2];

if ( strtotime( $date ) >= strtotime( '12/31/2023' ) ) {
    wp_redirect( 'your success URL' );
    exit;
} else {
    echo 'Error: date not valid';
}

If you run the code now, you’ll see that their API is returning an error message because the NONCE validation fails.

techhipbettruvabetnorabahisbahis forumueduedusedueduseduedueduedueduedus