simplexml_import_dom - 不起作用

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..

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

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

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

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";
}

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

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

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

print?

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