• PHP Video Tutorials

PHP - libxml_set_streams_context() Function



Definition and Usage

XML is a mark-up language to share the data across the web, XML is for both human read-able and machine read-able. The libXMLError class contains the errors thrown by the libxml library.

The libxml_set_streams_context() function is used to set context for the next libxml document.

Syntax

libxml_set_streams_context($streams_context);

Parameters

Sr.No Parameter & Description
1

streams_context (Mandatory)

This is a stream context resource created by the stream_context_create() function.

Return Values

This function does not return any values.

PHP Version

This function was first introduced in PHP Version 5 and works in all the later versions.

Example

Following example demonstrates the usage of the libxml_get_last_error() function −

<html>
   <head>
      <body>
         <?php
            $str = "<?xml version='1.0' standalone='yes'?>
            <Tutorial>
               <Name>JavaFX</Name>
               <Pages>535</Pages>
               <Author>Krishna</Author>
               <Version>11</Version>
            </Tutorial>";
            $opts = array(
               'http' => array(
                  'user_agent' => 'PHP libxml agent',
               )
            );
            $context = stream_context_create($opts);
            libxml_set_streams_context($context);
          
            $xml = new SimpleXMLElement($str);
            $xml->addAttribute('type', 'test');
            print_r($xml);
         ?>      
      </body>
   </head>   
</html> 

This will produce following result −

SimpleXMLElement Object ( 
   [@attributes] => Array ( [type] => test ) 
   [Name] => JavaFX [Pages] => 535 
   [Author] => Krishna [Version] => 11 
)
php_function_reference.htm
Advertisements