Rick Rick - 9 days ago 5
PHP Question

SimpleXML will not parse this one URL

I am developing a plugin for WordPress to read from an XML Output which is being delivered from a cloud-based POS.

This is not about complicated programming, this is more of a debug.

The XML url is: Removed :)

And the basic, simple code is:

<?php

error_reporting(E_ALL);

ini_set('display_errors', true);

$url = '--URL to the XM--L';

$xml=simplexml_load_file($url);

print_r($xml);

?>


I have tried all the approaches. DOMDoc, CURL and SimpleXML. Everything spits out errors. The coder who made the output has been able to get it to work within their domain, but I need to debug more to find out where the error may be.

I have run into quite a series of errors depending on how I feed the XML into the script.

Fatal error: Uncaught Exception: String could not be parsed as XML in /var/www/html/test2.php:27 Stack trace: #0 /var/www/html/test2.php(27): SimpleXMLElement->__construct('http://jewelbas...', NULL, true) #1 {main} thrown in /var/www/html/test2.php on line 27


And sometimes I get these weird support errors, that I assume is coming from their host, but they cannot identify them. These support errors come no matter what server I use. They come when using
simplexml_load_string()


Warning: simplexml_load_string(): act the webmaster. <br><br>Your support ID is: 9641638103684613562</body></html>

ThW ThW
Answer

The server requires a user agent to be set. All the standard XML APIs in PHP are based on libxml. You can set a stream context for it:

libxml_set_streams_context(
  stream_context_create(
    [
      'http' => [
        'header'=>
          "User-Agent: Foo\r\n"
      ]
    ]
  )
);

$document = new DOMDocument();
$document->load($url);
echo $document->saveXML();