Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

simplexml_load_file not working on live website ONLY

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.

like image 612
sohal07 Avatar asked Dec 25 '22 19:12

sohal07


1 Answers

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;
like image 179
Cillier Avatar answered Jan 06 '23 06:01

Cillier