0

Unfortunately I can't check it right now, because the XML (which will be on another server) is offline. The url to the xml file will look like this: http://url.com:123/category?foo=bar. It comes with no .xml file extension as you can see. I forgot to insert a file check to avoid error messages printing out the url of the xml file.
simple_load_file works fine with that URL, but I'm not sure about file_exists!

Would this work?:

if(file_exists('http://url.com:123/category?foo=bar')) { 
$xml = simplexml_load_file('http://url.com:123/category?foo=bar');
//stuff happens here
} else{echo 'Error message';}

I'm not sure since file_exists doesn't work with URLs. Thank you!

dnnr
  • 13
  • 2

2 Answers2

1

As you suspect, file_exists() doesn't work with URLs, but fopen() and fclose() do:

if (fclose(fopen("http://url.com:123/category?foo=bar", "r"))) {
    $xml = simplexml_load_file('http://url.com:123/category?foo=bar');
    //stuff happens here
} else {
    echo 'Error message';
}
miken32
  • 42,008
  • 16
  • 111
  • 154
1

It is not really useful, if you just try to fetch the data to parse it. Especially if the URL you call is a program/script itself. This will just mean that the script is executed twice.

I suggest you fetch the data with file_get_contents(), handle/catch the errors and parse the fetched data.

Just blocking the errors:

if ($xml = @file_get_contents($url)) {
  $element = new SimpleXMLElement($xml);
  ...
}
ThW
  • 19,120
  • 3
  • 22
  • 44