0

我想从 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?

4

3 回答 3

4

在调用file_get_contents(). 改用simplexml_load_string()

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

但更简单的是直接使用simplexml_load_file(). 请注意,该函数支持 HTTP url:

$xml = simplexml_load_file($completeurl);
于 2013-04-26T05:12:46.003 回答
2

simplexml_load_file()需要一个文件名,你想用它simplexml_load_string(file_get_contents($completeurl))来代替,或者simplexml_load_file($completeurl)

于 2013-04-26T05:12:41.707 回答
0

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

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

如果您使用的是 php 版本 <5,您也可以这样做

$temp = file_get_contents($url);
 $XmlObj = simplexml_load_string($temp); 
于 2013-04-26T05:19:08.287 回答