Well, I'm still a noob when it comes to PHP (or anything), so I'll just give an example. If you go here: you'll see the XML gadget displaying nicely in the browser window. Of course, if you load only the gadget in the browser (http://hosting.gmodules.com/ig/gadgets/file/102166312438248764726/calendar-personalized.xml) the page will display only as text.
What I want is the PHP code that does this trick. In this example is and I suspect that it might be PHP, but I don't mind a javascript code that can 'read' and display xml gadget.
Thanks!

Dani AI

Generated

Short summary and a reliable approach

The simplest, most robust fix is to fetch and parse the gadget XML on the server and then emit the gadget HTML inside a valid HTML page (or inside a sandboxed iframe). That avoids client-side cross-origin issues and the common problem where malformed output (unmatched quotes or an unclosed <script> tag) causes the browser to ignore everything outside the injected fragment. was correct to point at PHP parsing (DOMDocument / SimpleXML); 's client-side idea can work but is fragile unless the page is valid and the remote host permits cross-origin requests.

Server-side workflow (example)

  • Fetch the XML with cURL (so you can check HTTP status/timeouts).
  • Load it into DOMDocument (use internal libxml errors so you can inspect failures).
  • Extract the Content node (its CDATA block) and echo it where you want it rendered.
  • Wrap the result in a proper DOCTYPE/html/head/body or put it into a sandboxed iframe.

Example (server-side proxy/extractor):

<?php
$xmlUrl = $_GET['xml'] ?? '';
$ch = curl_init($xmlUrl);
curl_setopt_array($ch, [CURLOPT_RETURNTRANSFER => true, CURLOPT_FOLLOWLOCATION => true, CURLOPT_TIMEOUT => 5]);
$xml = curl_exec($ch);
$http = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($http !== 200 || !$xml) { echo 'Failed to fetch XML'; exit; }

libxml_use_internal_errors(true);
$doc = new DOMDocument();
$doc->loadXML($xml);
$node = $doc->getElementsByTagName('Content')->item(0);
$content = $node ? $node->nodeValue : '';
echo $content;
?>

Troubleshooting and safety

  • Inspect page source: if the rest of your HTML is missing, a quoting or unclosed-tag issue in the generated output is likely. Use the browser console/network tab to see JS errors and network failures.
  • If you must fetch client-side, the remote server must allow CORS or offer JSONP.
  • Security: gadget HTML may include scripts. Either sanitize the HTML on the server or render it inside a sandboxed iframe to avoid XSS and CSS/JS collisions.

Recommended Answers

All 11 Replies

It is just an XML CDATA block. Load this file into SimpleXML/DOMDocument, and you can do with it what you want.

Search this forum, examples are here.

Well, at least you didn't say something stupid like "Google is your friend".
Anyway, here's a possible hybrid sollution i found (php and jquery):

<?PHP
$xml = $_GET['xml'];
echo "<script src='http://code.jquery.com/jquery.min.js' type='text/javascript'></script>
        <script type='text/javascript'>
        $(document).ready(function(){
    function loadfail(){
        alert('Error: Failed to read the file!');
    }
    function parse(document){
        $(document).find('Module').each(function(){
           $('.combo1').append( 
            '' + $(this).find('Content').text() +
            ''
           );
        });
    }
    $.ajax({
        url: '$xml',
        dataType: 'xml',
        success: parse,
        error: loadfail
    });
});
</script><span class='combo1'></span>This text doesn't appear either.";
?>This text doesn't appear on page.

The problem is that anything else other then the module's content is hidden or doesn't load.
Any ideas, please?

Where's the rest of the HTML file? No html, head, body tag... Trying to append to a class named 'combo1' in an invalid HTML file will probably fail.

<span class='combo1'></span>

It works, just like I said before. The page loads only the combo1 span, bot nothing else. Everything I write before or after the combo1 doesn't show when the page load.
Anyway, I think now is a javascript problem.; probably someone should move the topic to another forum.

I still think it is because you have an invalid HTML file.

M8, I might be wrong, but it doesn't need to start with <html> and end with </html> to act like a HTML. Anyway, what is your solution? Do you have any suggestions?! And BTW, it is a PHP file, not a HTML; or, it's a PHP file that renders a HTML code.
Thx!

That is true for plain HTML, but most browsers won't run Js correctly if it is invalid (because the DOM parser will fail).

what is your solution?

what is your solution?

<!DOCTYPE html>
<html>
<head>
  <script src='http://code.jquery.com/jquery.min.js' type='text/javascript'></script>
  <script type='text/javascript'>
    $(document).ready(function(){
      $.ajax({
        url: '<?php echo $xml; ?>',
        dataType: 'xml',
        success: function(document){
          $(document).find('Module').each(function(){
            $('.combo1').append($(this).find('Content').text());
          });
        },
        error: function(){
          alert('Error: Failed to read the file!');
        }
      });
    });
</script>
</head>
<body>
<span class='combo1'></span>
</body>
</html>

If you still have problems, please show any javascript errors you get, and the xml url you are trying to load.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.