我正在编写一个PHP rest服务。我从客户端发送xml字符串和服务解析xml与SimpleXmlElement。如果我发送"hello world"而不是xml字符串,下面的代码段会在浏览器中产生警告。我如何检查输入是有效的xml字符串而不是一些句子。
try
{
xmlobj = new SimpleXMLElement($xml_post);
}
catch(Exception $e)
{
$dat = $e->getMessage();
return $dat;
}
警告是这样的
SimpleXMLElement: [SimpleXMLElement __construct()。实体:第1行:解析器错误:期望开始标记,'<'未找到
我想在调用SimpleXMLElement类构造函数之前验证输入
php.net有一个关于处理libxml错误的很好的文档。
在try-catch块之前添加以下内容:
libxml_use_internal_errors(true);