
- PHP - Home
- PHP - Roadmap
- PHP - Introduction
- PHP - Installation
- PHP - History
- PHP - Features
- PHP - Syntax
- PHP - Hello World
- PHP - Comments
- PHP - Variables
- PHP - Echo/Print
- PHP - var_dump
- PHP - $ and $$ Variables
- PHP - Constants
- PHP - Magic Constants
- PHP - Data Types
- PHP - Type Casting
- PHP - Type Juggling
- PHP - Strings
- PHP - Boolean
- PHP - Integers
- PHP - Files & I/O
- PHP - Maths Functions
- PHP - Heredoc & Nowdoc
- PHP - Compound Types
- PHP - File Include
- PHP - Date & Time
- PHP - Scalar Type Declarations
- PHP - Return Type Declarations
- PHP - Operators
- PHP - Arithmetic Operators
- PHP - Comparison Operators
- PHP - Logical Operators
- PHP - Assignment Operators
- PHP - String Operators
- PHP - Array Operators
- PHP - Conditional Operators
- PHP - Spread Operator
- PHP - Null Coalescing Operator
- PHP - Spaceship Operator
- PHP Control Statements
- PHP - Decision Making
- PHP - If…Else Statement
- PHP - Switch Statement
- PHP - Loop Types
- PHP - For Loop
- PHP - Foreach Loop
- PHP - While Loop
- PHP - Do…While Loop
- PHP - Break Statement
- PHP - Continue Statement
- PHP Arrays
- PHP - Arrays
- PHP - Indexed Array
- PHP - Associative Array
- PHP - Multidimensional Array
- PHP - Array Functions
- PHP - Constant Arrays
- PHP Functions
- PHP - Functions
- PHP - Function Parameters
- PHP - Call by value
- PHP - Call by Reference
- PHP - Default Arguments
- PHP - Named Arguments
- PHP - Variable Arguments
- PHP - Returning Values
- PHP - Passing Functions
- PHP - Recursive Functions
- PHP - Type Hints
- PHP - Variable Scope
- PHP - Strict Typing
- PHP - Anonymous Functions
- PHP - Arrow Functions
- PHP - Variable Functions
- PHP - Local Variables
- PHP - Global Variables
- PHP Superglobals
- PHP - Superglobals
- PHP - $GLOBALS
- PHP - $_SERVER
- PHP - $_REQUEST
- PHP - $_POST
- PHP - $_GET
- PHP - $_FILES
- PHP - $_ENV
- PHP - $_COOKIE
- PHP - $_SESSION
- PHP File Handling
- PHP - File Handling
- PHP - Open File
- PHP - Read File
- PHP - Write File
- PHP - File Existence
- PHP - Download File
- PHP - Copy File
- PHP - Append File
- PHP - Delete File
- PHP - Handle CSV File
- PHP - File Permissions
- PHP - Create Directory
- PHP - Listing Files
- Object Oriented PHP
- PHP - Object Oriented Programming
- PHP - Classes and Objects
- PHP - Constructor and Destructor
- PHP - Access Modifiers
- PHP - Inheritance
- PHP - Class Constants
- PHP - Abstract Classes
- PHP - Interfaces
- PHP - Traits
- PHP - Static Methods
- PHP - Static Properties
- PHP - Namespaces
- PHP - Object Iteration
- PHP - Encapsulation
- PHP - Final Keyword
- PHP - Overloading
- PHP - Cloning Objects
- PHP - Anonymous Classes
- PHP Web Development
- PHP - Web Concepts
- PHP - Form Handling
- PHP - Form Validation
- PHP - Form Email/URL
- PHP - Complete Form
- PHP - File Inclusion
- PHP - GET & POST
- PHP - File Uploading
- PHP - Cookies
- PHP - Sessions
- PHP - Session Options
- PHP - Sending Emails
- PHP - Sanitize Input
- PHP - Post-Redirect-Get (PRG)
- PHP - Flash Messages
- PHP AJAX
- PHP - AJAX Introduction
- PHP - AJAX Search
- PHP - AJAX XML Parser
- PHP - AJAX Auto Complete Search
- PHP - AJAX RSS Feed Example
- PHP XML
- PHP - XML Introduction
- PHP - Simple XML Parser
- PHP - SAX Parser Example
- PHP - DOM Parser Example
- PHP Login Example
- PHP - Login Example
- PHP - Facebook Login
- PHP - Paypal Integration
- PHP - MySQL Login
- PHP Advanced
- PHP - MySQL
- PHP.INI File Configuration
- PHP - Array Destructuring
- PHP - Coding Standard
- PHP - Regular Expression
- PHP - Error Handling
- PHP - Try…Catch
- PHP - Bugs Debugging
- PHP - For C Developers
- PHP - For PERL Developers
- PHP - Frameworks
- PHP - Core PHP vs Frame Works
- PHP - Design Patterns
- PHP - Filters
- PHP - JSON
- PHP - Exceptions
- PHP - Special Types
- PHP - Hashing
- PHP - Encryption
- PHP - is_null() Function
- PHP - System Calls
- PHP - HTTP Authentication
- PHP - Swapping Variables
- PHP - Closure::call()
- PHP - Filtered unserialize()
- PHP - IntlChar
- PHP - CSPRNG
- PHP - Expectations
- PHP - Use Statement
- PHP - Integer Division
- PHP - Deprecated Features
- PHP - Removed Extensions & SAPIs
- PHP - PEAR
- PHP - CSRF
- PHP - FastCGI Process
- PHP - PDO Extension
- PHP - Built-In Functions
PHP xml_set_external_entity_ref_handler() Function
The PHP XML Parser xml_set_external_entity_ref_handler() function is used to set a user-defined function as the handler for external entity references in an XML parser. This method is used to set a custom function to handle external entity references when processing XML files with the SimpleXML library or other PHP XML parsing libraries.
For managing external entities referenced in an XML file, like importing external data from a database or performing other tasks, the xml_set_external_entity_ref_handler() function is helpful.
Syntax
Below is the syntax of the PHP XML Parser xml_set_external_entity_ref_handler() function −
true xml_set_external_entity_ref_handler ( XMLParser $xml_parser, callable|string|null $handler )
Parameters
Here are the parameters of the xml_set_external_entity_ref_handler() function −
$xml_parser: (Required) It is the name of the user-defined function that will handle references to external entities.
$handler: (Required) It is the XML parser that the handler is set on.
Handler Signature
The signature of the handler is as follows −
bool handler( XMLParser $xml_parser, string $open_entity_names, string|false $base, string $system_id, string|false $public_id )
$xml_parser: It is the XML parser that calls the handler is contained in the variable $parser.
$open_entity_names: It is a variable that holds the external entity's name.
$base: The base used to resolve the external entity's system identifier (system_id). At the moment, this string is always empty.
$system_id: It is the entity declaration's specified system identifier.
$public_id: It is the entity declaration's selected public identifier.
Return Value
The xml_set_external_entity_ref_handler() function returns TRUE on success. And FALSE on failure.
PHP Version
First introduced in core PHP 4, the xml_set_external_entity_ref_handler() function continues to function easily in PHP 5, PHP 7, and PHP 8.
Example 1
Here is the basic example of the PHP XML Parser xml_set_external_entity_ref_handler() function. This program offers an overview of how to create a handler function to manage external entity references. The handler just outputs the name of the external entity each time it is mentioned in the XML document.
<?php // Define a simple handler function function external_entity_handler($xml_parser, $open_entity_names, $base, $system_id, $public_id) { echo "External entity reference: $open_entity_names\n"; // Return true to continue parsing return true; } // Create an XML parser $xml_parser = xml_parser_create(); // Set the external entity handler xml_set_external_entity_ref_handler($xml_parser, 'external_entity_handler'); // Parse a sample XML document xml_parse($xml_parser, '<!DOCTYPE root [ >!ENTITY test SYSTEM "test.txt"< ]><root>&test;</root>'); // Free the XML parser xml_parser_free($xml_parser); ?>
Output
Here is the outcome of the following code −
External entity reference: test
Example 2
This example shows how to handle errors using the handler function xml_set_external_entity_ref_handler(). It checks to see if the external entity file is there before processing. It prints an error message and returns false to stop further parsing if the file is missing.
<?php // Define a handler function function handle_external_entities($xml_parser, $open_entity_names, $base, $system_id, $public_id) { if (!file_exists($system_id)) { echo "Error: External entity '$open_entity_names' not found.\n"; // Return false to stop the parser on error return false; } echo "Entity '$open_entity_names' is valid.\n"; return true; } // Create an XML parser $xml_parser = xml_parser_create(); // Set the handler function xml_set_external_entity_ref_handler($xml_parser, 'handle_external_entities'); // Parse the XML document xml_parse($xml_parser, '<!DOCTYPE root [ <!ENTITY missingEntity SYSTEM "missing.txt"> ]><root>&missingEntity;</root>'); // Free the XML parser xml_parser_free($xml_parser); ?>
Output
This will generate the below output −
Error: External entity 'missingEntity' not found.
Example 3
This example handles a large number of external entities and saves their details, like name, system ID, and public ID, to a file called external_entities.log. It also checks the external entity's integrity and properly fetches its contents. If the entity cannot be found, a warning is displayed.
<?php // Define a complex handler function complex_external_entity_handler($xml_parser, $open_entity_names, $base, $system_id, $public_id) { // Log entity information to a file $logMsg = "Entity: $open_entity_names\nSystem ID: $system_id\nPublic ID: $public_id\n"; file_put_contents('external_entities.log', $logMsg, FILE_APPEND); // Return the content echo "Processing entity '$open_entity_names'...\n"; if (file_exists($system_id)) { return file_get_contents($system_id); } else { echo "Warning: Entity '$open_entity_names' not found!\n"; return false; } } // Create an XML parser $xml_parser = xml_parser_create(); // Set the handler xml_set_external_entity_ref_handler($xml_parser, 'complex_external_entity_handler'); // Parse an XML document $xml_data = '<!DOCTYPE root [ <!ENTITY firstEntity SYSTEM "first.txt"> <!ENTITY secondEntity SYSTEM "second.txt"> ]> <root>&firstEntity;&secondEntity;</root>'; xml_parse($xml_parser, $xml_data); // Free the XML parser xml_parser_free($xml_parser); ?>
Output
This will create the below output −
Processing entity 'firstEntity'... Warning: Entity 'firstEntity' not found!