php_ref_libxml
PHP libxml Functions
Section titled “PHP libxml Functions”Introduction to PHP libxml
Section titled “Introduction to PHP libxml”The libxml extension provides a comprehensive set of functions and constants for working with XML data in PHP. It forms the foundation for other PHP XML extensions like SimpleXML, DOM (Document Object Model), and XSLT. Understanding libxml is helpful for advanced XML manipulation and error handling.
These functions allow you to control various aspects of XML parsing and manipulation, including error handling, document loading options, and accessing libxml library information.
Installation / Enabling
Section titled “Installation / Enabling”The libxml extension is enabled by default in modern PHP installations. You typically do not need to install it separately.
If you compiled PHP from source or are using a minimal environment, you might need to ensure the libxml development package (e.g., libxml2-dev on Debian/Ubuntu, libxml2-devel on CentOS/Fedora) was available during compilation. For most users relying on pre-built PHP packages, this is handled automatically.
PHP libxml Functions
Section titled “PHP libxml Functions”Here are some key functions provided by the libxml extension:
| Function | Description |
|---|---|
| libxml_clear_errors() | Clears the libxml error buffer. Useful when processing multiple documents or operations. |
| libxml_get_errors() | Retrieves an array of LibXMLError objects, detailing errors encountered during the last XML operation. |
| libxml_get_last_error() | Retrieves the last error encountered as a LibXMLError object. |
| libxml_set_streams_context() | Sets the stream context for the next libxml document load or write operation. Useful for controlling network access or other stream behaviors. |
| libxml_use_internal_errors(bool $use_errors = false) | Enables or disables the standard libxml error reporting. When set to true, errors are suppressed, and you must use libxml_get_errors() to check for issues. |
| libxml_disable_entity_loader(bool $disable = true) | Disables or enables the loading of external entities. It’s crucial to disable this (true) when parsing untrusted XML data to prevent XML External Entity (XXE) attacks. |
PHP libxml Constants
Section titled “PHP libxml Constants”These constants are used as flags, often passed to functions like simplexml_load_file(), simplexml_load_string(), DOMDocument::load(), etc., to control parsing behavior.
| Constant | Description |
|---|---|
| LIBXML_COMPACT | Activates small node allocation optimization. May improve performance on smaller documents. |
| LIBXML_DTDATTR | Sets default DTD attributes. |
| LIBXML_DTDLOAD | Loads an external subset (like DTD). |
| LIBXML_DTDVALID | Validates the document against its DTD. |
| LIBXML_NOBLANKS | Removes blank nodes (ignorable whitespace). |
| LIBXML_NOCDATA | Merges CDATA sections into text nodes. |
| LIBXML_NOEMPTYTAG | Expands empty tags (e.g., <tag/> to <tag></tag>). Affects saving with DOMDocument. |
| LIBXML_NOENT | Substitutes entities. Warning: Can be dangerous with untrusted XML due to entity expansion vulnerabilities. Consider LIBXML_NONET and libxml_disable_entity_loader(). |
| LIBXML_NOERROR | Suppresses error reports (use with libxml_use_internal_errors(true)). |
| LIBXML_NONET | Disables network access during document loading. Important for security when dealing with potentially malicious external entities or schemas. |
| LIBXML_NOWARNING | Suppresses warning reports. |
| LIBXML_NOXMLDECL | Drops the XML declaration (e.g., <?xml version='1.0'?>) when saving a document with DOMDocument. |
| LIBXML_NSCLEAN | Removes redundant namespace declarations. |
| LIBXML_XINCLUDE | Implements XInclude substitution. |
| LIBXML_PARSEHUGE | Relaxes hardcoded limits of the parser. Use with caution, as it might impact performance or security. |
| LIBXML_BIGLINES | Allows storing line numbers greater than 65535 by reporting them differently. |
| LIBXML_ERR_ERROR | Represents recoverable errors. |
| LIBXML_ERR_FATAL | Represents fatal errors. |
| LIBXML_ERR_NONE | Represents no errors. |
| LIBXML_ERR_WARNING | Represents simple warnings. |
| LIBXML_VERSION | The underlying libxml library version as an integer (e.g., 20901 for 2.9.1). |
| LIBXML_DOTTED_VERSION | The underlying libxml library version as a string (e.g., “2.9.1”). |