企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
这些函数/常量从PHP 5.1.0开始可用,以下核心扩展依赖于此libxml扩展:[DOM](https://www.php.net/manual/en/book.dom.php),[libxml](https://www.php.net/manual/en/book.libxml.php),[SimpleXML](https://www.php.net/manual/en/book.simplexml.php),[SOAP](https://www.php.net/manual/en/book.soap.php),[WDDX](https://www.php.net/manual/en/book.wddx.php),[XSL](https://www.php.net/manual/en/book.xsl.php),[XML](https://www.php.net/manual/en/book.xml.php),[XMLReader](https://www.php.net/manual/en/book.xmlreader.php),[XMLRPC](https://www.php.net/manual/en/book.xmlrpc.php)和[XMLWriter](https://www.php.net/manual/en/book.xmlwriter.php) * [预定义常量](https://www.php.net/manual/en/libxml.constants.php) * [libXMLError](https://www.php.net/manual/en/class.libxmlerror.php)— libXMLError类 ## [**libxml函数**](https://www.php.net/manual/en/ref.libxml.php) 1、[libxml\_clear\_errors( void ) : void](https://www.php.net/manual/en/function.libxml-clear-errors.php)—清除libxml错误缓冲区 2、[libxml\_disable\_entity\_loader([ bool $disable = TRUE ] ) : bool](https://www.php.net/manual/en/function.libxml-disable-entity-loader.php)—禁用加载外部实体的功能,使用此功能可以防止本地和远程文件包含攻击(,这还会禁用simplexml_load_file()中的url加载,并可能禁用处理URL的其他基于libxml的函数;此函数不是线程安全的。 因此,这可能会影响同一服务器上的php脚本) ``` 文件包含例子 <!DOCTYPE scan [<!ENTITY test SYSTEM "php://filter/read=convert.base64-encode/resource=/etc/passwd">]> <scan>&test;</scan> ``` 3、[libxml\_get\_errors( void ) : array](https://www.php.net/manual/en/function.libxml-get-errors.php)—检索错误数组 此示例演示如何构建简单的libxml错误处理程序。 ``` libxml_use_internal_errors(true); $xmlstr = <<< XML <?xml version='1.0' standalone='yes'?> <movies> <movie> <titles>PHP: Behind the Parser</title> </movie> </movies> XML; $doc = simplexml_load_string($xmlstr); $xml = explode("\n", $xmlstr); if ($doc === false) { $errors = libxml_get_errors(); foreach ($errors as $error) { echo display_xml_error($error, $xml); } libxml_clear_errors(); } function display_xml_error($error, $xml) { $return = $xml[$error->line - 1] . "\n"; $return .= str_repeat('-', $error->column) . "^\n"; switch ($error->level) { case LIBXML_ERR_WARNING: $return .= "Warning $error->code: "; break; case LIBXML_ERR_ERROR: $return .= "Error $error->code: "; break; case LIBXML_ERR_FATAL: $return .= "Fatal Error $error->code: "; break; } $return .= trim($error->message) . "\n Line: $error->line" . "\n Column: $error->column"; if ($error->file) { $return .= "\n File: $error->file"; } return "$return\n\n--------------------------------------------\n\n"; } ``` 输出 ``` <titles>PHP: Behind the Parser</title> ----------------------------------------------^ Fatal Error 76: Opening and ending tag mismatch: titles line 4 and title Line: 4 Column: 46 -------------------------------------------- ``` 4、[libxml\_get\_last\_error(void):LibXMLError](https://www.php.net/manual/en/function.libxml-get-last-error.php)—从libxml检索最后一个错误 5、 [libxml\_set\_external\_entity\_loader](https://www.php.net/manual/en/function.libxml-set-external-entity-loader.php)—更改默认的外部实体加载器 ``` $xml = <<<XML <!DOCTYPE foo PUBLIC "-//FOO/BAR" "http://example.com/foobar"> <foo>bar</foo> XML; $dtd = <<<DTD <!ELEMENT foo (#PCDATA)> DTD; libxml_set_external_entity_loader( function ($public, $system, $context) use($dtd) { var_dump($public); var_dump($system); var_dump($context); $f = fopen("php://temp", "r+"); fwrite($f, $dtd); rewind($f); return $f; } ); $dd = new DOMDocument; $r = $dd->loadXML($xml); var_dump($dd->validate()); ``` 返回值: ``` string(10) "-//FOO/BAR" string(25) "http://example.com/foobar" array(4) { ["directory"] => NULL ["intSubName"] => NULL ["extSubURI"] => NULL ["extSubSystem"] => NULL } bool(true) ``` 6、 [libxml\_set\_streams\_context](https://www.php.net/manual/en/function.libxml-set-streams-context.php)—设置下一个libxml文档加载或写入的流上下文 ``` $opts = array( 'http' => array( 'user_agent' => 'PHP libxml agent', ) ); $context = stream_context_create($opts); libxml_set_streams_context($context); // request a file through HTTP $doc = DOMDocument::load('http://www.example.com/file.xml'); ``` 7、[libxml\_use\_internal\_errors](https://www.php.net/manual/en/function.libxml-use-internal-errors.php)—禁用libxml错误,并允许用户根据需要获取错误信息 ``` //此示例演示libxml错误的基本用法以及此函数返回的值。 // 启用用户自定义错误处理 var_dump(libxml_use_internal_errors(true)); // 开始加载文件 $doc = new DOMDocument; if (!$doc->load('file.xml')) {     foreach (libxml_get_errors() as $error) {         // 在这处理接受到的错误     }     libxml_clear_errors(); } ``` The above example will output: ~~~ bool(false) ~~~