1

I am coding a PHP rest Service. I am sending xml string from client and service parse the xml with SimpleXmlElement. If I send "hello world" instead of xml string, the below piece of code produce warnings in the browser. how can I check the input to be valid xml string not some sentence.

 try
 {
   xmlobj = new SimpleXMLElement($xml_post);
 }
 catch(Exception $e)
 {
   $dat = $e->getMessage();
   return $dat;
 }

The warning is something like this

SimpleXMLElement::__construct() [simplexmlelement.--construct]: Entity: line 1: parser error : Start tag expected, '<' not found

I want to validate the input before calling SimpleXMLElement class construtor.

1 Answer 1

1

php.net has a gret doc about handling libxml errors.

start by adding the following before try-catch block:

libxml_use_internal_errors(true);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.