3

I have a problem loading an external XML file.

When I open it in a browser, everything looks good. I tried to download the XML file and upload it on my own server. When I try to load the XML file from my server, everything works well.

Can somebody help me solve this problem, so that I can load it from the external server?

My code:

$oXML_cz = new SimpleXMLElement(file_get_contents('http://www.ticketportal.cz/xml/temp/partnerall1.xml?ID_partner=122'));
foreach ($oXML_cz->event as $event_cz)
{
      ......
}
lonesomeday
  • 233,373
  • 50
  • 316
  • 318
general666
  • 1,001
  • 2
  • 16
  • 31
  • You mention in a comment below that you are able to use `file_get_contents` to fetch other remote files, which is an important possibility to rule out. What other steps have you tried to debug this? For instance, if you echo out the contents of `file_get_contents('http://www.ticketportal.cz/xml/temp/partnerall1.xml?ID_partner=122')`, do you get the expected XML? What is the *exact* error message you receive when trying to run the code posted here? – IMSoP May 19 '14 at 14:03
  • If I echo, it returns: [function.file-get-contents]: failed to open stream: HTTP request failed! – general666 May 19 '14 at 16:52
  • Aha! Well, that is an important discovery, then, isn't it? It means that this has nothing to do with parsing it as XML, and everything to do with being able to access that URL from your server. So, it might be worth trying some alternative ways of fetching the file (e.g. CURL) and seeing if they at least give you a more expressive error. – IMSoP May 19 '14 at 17:49
  • Also, to be clear, that message is not the result of `echo`, and should have been showing even in your previous code. Have you made sure your [error logging or display is turned up to the max](http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php/12772851#12772851) to ensure all messages are captured? – IMSoP May 19 '14 at 17:51
  • Yes it helped... I used CURL and now everything work. Thank you for helping! – general666 May 21 '14 at 08:05

3 Answers3

2

This library's errors are not too well documented. The problem MAY be due to an excessively large XML file rather than xml structural compliance / integrity.

For example, once parsed, 15MB files may extrapolate 1GB, so ini_set('memory_limit', '1024M'); may not be effective.

In above situation, i solved the problem by including the LIBXML_PARSEHUGE parameter during xml declaration / loading.

$xml = new SimpleXMLElement($contents, LIBXML_PARSEHUGE);
tony gil
  • 9,424
  • 6
  • 76
  • 100
1

The solution is to try CURL:

function download_page($path){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$path);
    curl_setopt($ch, CURLOPT_FAILONERROR,1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 15);
    $retValue = curl_exec($ch);          
    curl_close($ch);
    return $retValue;
}

$sXML = download_page('http://www.domain.com/file.xml');
$oXML_cz = new SimpleXMLElement($sXML);

foreach($oXML_cz->event as $event_cz)
{
   ...
}

Thank you for answers ;)

general666
  • 1,001
  • 2
  • 16
  • 31
0

Check the configuration of allow_url_fopen. More tips can be found in this question.

Community
  • 1
  • 1
ebo
  • 2,717
  • 1
  • 27
  • 22