Joao
Joao

Reputation: 63

Failing to read xml file with php's simplexml_load_file in localhost (XAMPP)

I am trying to do something as simple as reading an XML file from php using XAMPP in MAC OS 10.9. For some reason I can't understand I cannot get a consistent response from the server (localhost). Sometimes it works, and I get the response printed on the browser; but most of the times I simply don't get an answer from the server.

Here is what google chrome tells me when the server does not respond: "No data received. Unable to load the webpage because the server sent no data. Error code: ERR_EMPTY_RESPONSE."

Here's the code (this is as simple as it can get):

PHP code:

<?php
    $xmlCustomers = simplexml_load_file("customers.xml");
    print_r($xmlCustomers);
?>

XML file:

<?xml version="1.0"?>
<channel>
    <item>
         <name>Joao</name>
         <phone>961111111</phone>
         <email>[email protected]</email>
    </item>
    <item>
         <name>Joana</name>
         <phone>962222222</phone>
         <email>[email protected]</email>
    </item>
</channel>

Somebody knows how can I solve this? is this a problem of XAMPP? Thanks in advance

Upvotes: 0

Views: 3500

Answers (1)

Tim Kant
Tim Kant

Reputation: 46

I have faced the same problem and found the solution. Well, solution, it's more of a work around but I makes everything work again.

Instead of loading the XML file directly into SimpleXML you can also parse the content into a string and then load it into SimpleXML, which did the trick for me:

/* Read XML file */
$xml_file_content = file_get_contents($xml_file);

/* Parse XML */
$parsed_xml = new SimpleXMLElement($xml_file_content);

Upvotes: 3

Related Questions