XML issue in Joomla4

125 views
Skip to first unread message

Red Ant

unread,
Sep 26, 2022, 10:25:50 AM9/26/22
to Joomla! General Development
Hello

I have a J3 site with a few a few custom scripts which I am migrating to J4. Both sites are running PHP7.4

One of those scripts reads an XML file on a remote server and returns the values. This works perfectly on J3 but I cannot get it to work in J4.

Unfortunately I cannot paste the actual url here as it has sensitive data but here is the snippet of code which is causing the problem. 

Has something changed in the way J4 handles XML?

$getbal = "http://<the url>.com:8080/getPullDetails.jsp?LOGIN=<Login creds>&PWD=<Login creds>&id=123456";

header('Content-type: text/xml');
        $balresult = simplexml_load_file($getbal);
        $balance = $balresult->balance;
        
echo $balance; // This returns 0

echo $getbal; // This returns the correct url and I am able to copy and paste it into a browser and retrieve the correct data.

I have error reporting set to E_ALL but no errors are thrown and neither do any errors appear in the J logs or the server logs and FF dev console does not reveal anything either.

I was able to replicate this on a localhost installation as well as a J4 installation on a different server.

Anyone else come across this or have any ideas? 

Thanks so much.

Regards
Anton

MarkRS

unread,
Sep 26, 2022, 11:49:25 AM9/26/22
to Joomla! General Development
What's in $balresult?

Red Ant

unread,
Sep 26, 2022, 1:11:56 PM9/26/22
to Joomla! General Development
Hi.
What is actually in $balresult is: 0

What I expect to be in $balresult is: 175.42

So, an integer value.

But as I said, if I echo the url ($getbal)and copy and paste that into a browser, the correct output is rendered ...

Red Ant

unread,
Sep 26, 2022, 1:20:24 PM9/26/22
to Joomla! General Development
Just for clarity. If I copy and paste the url into the browser this is the returned value:
<balance>175.42</balance>

Viper

unread,
Sep 26, 2022, 2:03:35 PM9/26/22
to Joomla! General Development
Please read the PHP manual.
https://www.php.net/manual/en/function.simplexml-load-file.php Interprets an XML file into an object

So if you do:
$getbal = "http://localhost/index1.php";
$balresult = simplexml_load_file($getbal);

var_dump($ balresult );
$balance = $balresult->balance;
var_dump($balance);

you will see the difference.

Red Ant

unread,
Sep 27, 2022, 2:22:29 AM9/27/22
to Joomla! General Development
Thanks, Viper

Yes, I am aware that the returned XML is an object.

The problem is that nothing is being returned when I run the code in J4. The exact same code snippet works 100% in J3.

The results of  var_dump($ balresult ); is  bool(false)
and of var_dump($balance); is NULL

Using libxml_get_errors(); also does not return any errors.

The main issue is that there are no clues anywhere for me to be able to debug this. There is simply no response data to work with and no errors in the logs. It's as though the returned data is being silently dropped in J4. As mentioned, I was able to replicate this on two different hosts and localhost J4 installations.

Regards
Anton

Red Ant

unread,
Sep 27, 2022, 2:44:40 AM9/27/22
to Joomla! General Development
And finally with debugging set to maximum, a clue.

  Warning: simplexml_load_file(): I/O warning : failed to load external entity "http://<URL>" in ..../custom/editusers.php on line 98 Notice: Trying to get property 'balance' of non-object in ..../custom/editusers.php on line 99 bool(false)

but ... the object is there in the XML tree (as per attachment)
xml.JPG

Hannes Papenberg

unread,
Sep 27, 2022, 2:45:16 AM9/27/22
to Joomla! General Development
Maybe first of all don't open the URL directly, but read the XML with something else, like file_get_content() and then put it into simplexml. See what you get there. I would assume that maybe it doesn't read from URL for some reason.

--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-gene...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/joomla-dev-general/10beb6d1-241e-4848-a077-9238a13907c9n%40googlegroups.com.

Red Ant

unread,
Sep 27, 2022, 3:25:10 AM9/27/22
to Joomla! General Development
Thanks for the help everyone, especially Hannes. You put me on the right path.

I managed to resolve this with the following code but it still begs the question, "Why is this working in J3 but not J4?"

// Build the URL
        $getbal = "https://<url with vars>";

// And fetch the file
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $getbal);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $result = curl_exec($ch);

        $balresult = simplexml_load_string($result);

Reply all
Reply to author
Forward
0 new messages