从URL加载xml文件


Loading XML-file from URL

我想从url中读取xml文件,然后从xml文件中检索信息。

<?php
  if( $_POST["name"])
  {
    ini_set('allow_url_fopen ','ON');
    $completeurl = "http://localhost:8983/solr/select?q=".$_POST['name'];
    $xml = simplexml_load_file(file_get_contents($completeurl));  
    exit();
  }
?>
<html>
<body>
  <form action="<?php $_PHP_SELF ?>" method="POST">
  Search: <input type="text" name="name" />
  <input type="submit" />
  </form>
</body>
</html>

当我向表单写入一些输入时,它显然设法检索一些东西,但我也得到这样的消息:

Warning: simplexml_load_file() [function.simplexml-load-file]: 
I/O warning : failed to load external entity "<?xml version="1.0" encoding="UTF-8"?> 
<response> <lst name="responseHeader"><int name="sta....

如何从url中提取xml-info ?

调用file_get_contents()后,您已经在字符串中拥有了远程文件的内容。使用simplexml_load_string()代替:

$xml = simplexml_load_string(file_get_contents($completeurl)); 

但更简单的是直接使用simplexml_load_file()加载文件。注意,该函数支持HTTP url:

$xml = simplexml_load_file($completeurl);

simplexml_load_file()需要一个文件名,您想要使用simplexml_load_string(file_get_contents($completeurl))代替,或simplexml_load_file($completeurl)

如上所述,您正在从本地主机加载xml,因此您可以给

simplexml_load_file($_SERVER['DOCUMENT_ROOT'].'/uproYourFoldet/xml_data.xml')

如果你使用的是PHP版本<5

$temp = file_get_contents($url);
 $XmlObj = simplexml_load_string($temp);