1

A 3rd party APP delivers a XML message at a URL. I want to be able to read and operate with that message at WordPress.

I believe that SimpleXMLElement is the key, rather than creating a class. But many errors are returned based on what I put as a parameter.

add_action( 'rest_api_init', function () {
  register_rest_route( 'webhook', '/v1', array(
    'methods'  => 'POST',
    'callback' => 'myfunction',
  ));
});

function myfunction () {

    $mymessage = new SimpleXMLElement($xmlstr);

    $messageType = $mymessage->cgCallback[0]->existingField;

    echo $messageType ;
}

$mymessage = new SimpleXMLElement($xmlstr); -> Returns "Undefined variable: xmlstr"

$mymessage = new SimpleXMLElement($data); -> Returns "Uncaught TypeError: SimpleXMLElement::__construct() expects parameter 1 to be string"

$mymessage = new SimpleXMLElement($message); -> Returns "Uncaught Exception: String could not be parsed as XML"

  • try adding the variable as a parameter to your function - like so: `function myfunction ($xmlstr) {...}` – Professor Abronsius Jul 14 '19 at 09:39
  • Thanks RamRaider. With `function myfunction ($xmlstr) { $mymessage = new SimpleXMLElement($xmlstr);` returns "Uncaught TypeError: SimpleXMLElement::__construct() expects parameter 1 to be string" – Eugenio Calderon Jul 14 '19 at 09:51
  • Can you show the code where you get the value you pass to this function? – Nigel Ren Jul 15 '19 at 05:47
  • Hi Nigel, I do not have any other code than that. I thought it should be enough. – Eugenio Calderon Jul 15 '19 at 11:51
  • Possible duplicate of ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – miken32 Jul 17 '19 at 18:41

1 Answers1

0

Ok, got the solution: This will not work with SimpleXMLElement but with simplexml_load_string and requires file_get_contents('php://input'):

function myfunction () {

    $postData = file_get_contents('php://input');
    $xml = simplexml_load_string($postData);

    $messageType = $xml->header[0]->existingField;

    echo $messageType ;
}