1

I am new to WordPress API's and API's in general. I have an external app that is sending a GET request to my WordPress site with an XML string as the only parameter. I created a custom API that responds back with it's own XML string. I am successfully sending my XML string to the app and the app is displaying the data correctly. The problem is that I have to first get the XML string coming from the GET request from the app and extract some information that I then have to add to my XML string, such as a ConversationID. I have no idea how to do this and I have searched everywhere on the web for information on this, but have not found anything yet. Here is my custom API PHP code in WordPress:

function my_awesome_func( WP_REST_Request $request ) {
    
    $menu = "<?xml version='1.0' encoding='utf-8' ?><UssdMessage><MessageType></MessageType><ConversationID>42B29890-4404-4E26-B1AD-7C487C9F373F</ConversationID><SessionID>0000000159CB2EFC00</SessionID><TransactionID>2358dfa3-0e3c-40ad-96ce-756cb1d6ae3f</TransactionID><MSISDN></MSISDN><MessageString> +TestCompany%0d%0a%0d%0a+Are+You+At+Work%0d%0a1.+Yes%0d%0a2.+No%0d%0a4.+Leave+Application%0d%0a0.+Exit</MessageString><Success>True</Success><IsFinal>False</IsFinal><DateTimeReceived>06/06/2015 14:50:11</DateTimeReceived><MNO>MTN</MNO></UssdMessage>";
    
    return $menu;
}

add_action( 'rest_api_init', function () {
  register_rest_route( 'freedom/v1', '/ussd-api', array(
    'methods' => WP_REST_Server::READABLE,
    'callback' => 'my_awesome_func',
  ) );
} );

function maybe_ussd_feed( $served, $result, $request, $server ) {

    if ( '/freedom/v1/ussd-api/' !== $request->get_route() ||
        
        'my_awesome_func' !== $request->get_attributes()['callback'] ) {
        return $served;
    }

    $server->send_header( 'Content-Type', 'text/xml' );
    echo $result->get_data();

    exit;
}
add_filter( 'rest_pre_serve_request', 'maybe_ussd_feed', 10, 4 );

I have tried several ideas, but I honestly have no idea what I am doing.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.