0

I want to read a XML-file from an url and then retrieve the information from my XML-file.

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

When I write some input to the form, it clearly manages to retrieve something, but I also get a message like this:

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

How can I extract the xml-info from the url?

3 Answers 3

4

You are already having the remote file's contents in a string after calling file_get_contents(). Use simplexml_load_string() instead:

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

But simpler would be to load the file directly using simplexml_load_file(). Note that the function supports HTTP urls:

$xml = simplexml_load_file($completeurl);
Sign up to request clarification or add additional context in comments.

Comments

2

simplexml_load_file() takes a filename, you'd want to use simplexml_load_string(file_get_contents($completeurl)) instead, or simplexml_load_file($completeurl)

1 Comment

I find it also worth to note that there is the stream context which allows for example to use POST requests and set custom HTTP headers which can be useful when talking to a REST API. You can find a simplexml example here: OSM Data parsing to get the nodes with child
0

As above you are loading xml in from local host so you can give

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

you can also do this if you are using php version <5

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.