tags:

views:

93

answers:

6

Hello,

if (file_exists('http://something_from_a_url.xml')) {
    $xml = simplexml_import_dom('http://something_from_a_url.xml');

    print_r($xml);
} else {
    exit('Failed to open xml file.');
}

when I execute this code it says failed to open, I fail to understand why..

+1  A: 

simplexml_import_dom takes a DOM node, rather than a URL. You should use simplexml_load_file intead:

$xml = simplexml_load_file('http://something_from_a_url.xml');
print_r($xml);
James Wheare
done simplexml_load_file does not work either
Re your latest comment on the question, removing the `file_exists` if statement altogether would have fixed it. Youre "reversing the statements" basically just means `simplexml_load_file` runs first, whereas before it would *never* run.
James Wheare
A: 

file_exists() does not work on URLs. That doesn't even make sense for an URL.

simplexml_import_dom() is intended to convert DOM nodes to SimpleXML nodes. It expects an instance of a DOM node as an argument. You are passing an URL instead.

Perhaps you want to:

$simplexml = simplexml_load_string(file_get_contents($url));
its the simplexml thats not working, nope that piece of code does not work
Did you take out the file_exists check before trying the above code? As mentioned elsewhere in this thread, file_exists does not work over http(s).
Anti Veeranna
yes check latest comment
A: 

Check that:

a) parameter for simplexml_import_dom is a DOMNode.

b) you have allow_url_fopen enabled in your php.ini

As an alternative, you can try to use [simplexml_load_file][3] with the URL and see what's happening.

Extra links: php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen

php.net/manual/en/function.simplexml-load-file.php

aurelian
allow_url_fopen is open
A: 

generally ini setting prohibit this.

You could try this method to verify the existance of a remote file.

if (fopen($url, "r")) {
    echo "File Exists";
} else {
    echo "Can't Connect to File";
}
Brian
check latest comment in question
A: 

What does

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

$contents = file_get_contents('http://something_from_a_url.xml');
echo '<div>Debug: len(contents)=', strlen($contents), "</div>\n";

libxml_use_internal_errors(true);
libxml_clear_errors();
$xml = new SimpleXMLElement($contents);

foreach( libxml_get_errors() as $error ) {
  echo 'error: ', $error->$message, "<br />\n";
}

print?

VolkerK
Debug: len(contents)=35966
A: 

it works the other way round, not sure why--but solved