PHP DOMDocument error handling

前端 未结 5 1671
予麋鹿
予麋鹿 2020-12-03 05:01

In my application I am loading xml from url in order to parse it. But sometimes this url may not be valid. In this case I need to handle errors. I have the following code:

5条回答
  •  南笙
    南笙 (楼主)
    2020-12-03 05:50

    From php.net

    If an empty string is passed as the filename or an empty file is named, a warning will be generated. This warning is not generated by libxml and cannot be handled using libxml's error handling functions.

    In your production environment you shouldn't have errors displayed to the user. They don't need to see them so taking this into account you can use...

    $xdoc = new DOMDocument();
    if ( $xdoc->load($url) ) {
        // valid
    }
    else {
        // invalid
    }
    

提交回复
热议问题