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) { ...... } 
5
  • 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? Commented May 19, 2014 at 14:03
  • If I echo, it returns: [function.file-get-contents]: failed to open stream: HTTP request failed! Commented May 19, 2014 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. Commented May 19, 2014 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 to ensure all messages are captured? Commented May 19, 2014 at 17:51
  • Yes it helped... I used CURL and now everything work. Thank you for helping! Commented May 21, 2014 at 8:05

3 Answers 3

3

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); 
Sign up to request clarification or add additional context in comments.

Comments

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 ;)

Comments

0

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

1 Comment

This is not problem, because i'm loading XML files from other servers and it's working...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.