I have recently put my website online and found simplexml_load_file problem. It works perfectly on local server (MAMP) but not live on the website. Code is as below:
<?
$source = simplexml_load_file('http://domainname.com/file.xml') or die("Something is wrong");
echo $source->Result;
?>
The above code displays following message on live website: "
Something is wrong
And same code displays following message on local server MAMP:
Success
And file.xml is:
<?xml version="1.0"?>
<!DOCTYPE ValidateUser>
<ValidateUser>
<Customer>john</Customer>
<Result>Success</Result>
</ValidateUser>
Thanks in advance.
It sounds like your hosting provider has disabled PHP's ability to open URL's as files.
Run the following snippit of code on your server. If the result is "0", then this feature has been disabled.
<?php var_dump(ini_get('allow_url_fopen')); ?>
If its disabled you might need to use something like CURL to fetch the XML file before processing it. You could then use simplexml_load_string() instead of simplexml_load_file()
Here is an example of how to use CURL:
http://davidwalsh.name/curl-download
If CURL is also not available you will have to discuss alternatives with your hosting provider.
So, using the code from the link above, you could do something like this:
/* gets the data from a URL */
function get_data($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$returned_content = get_data('http://davidwalsh.name');
$source = simplexml_load_string($returned_content) or die("Something is wrong");
echo $source->Result;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With