-1

I have to create a webservice which goes to a specific URL that returns a XML-file as response and interprets/parses this file in order to save its contents to a MySQL database.

I've heard about the SimpleXML but I'm not sure how to get the websites response into a file whose path is needed in order to parse the document.

Can somebody at least explain me how to reach the goal of downloading the XML and saving it to a file? (best with some PHP code) I will then (hopefully) find out by myself how to parse it and store its contents.

Here's an example of what my XML will look like (for privacy reasons I can't publish the real URL I'm using...)

Rowland Shaw
  • 37,700
  • 14
  • 97
  • 166
johk95
  • 873
  • 10
  • 28

3 Answers3

3

Here's a couple of pointers..

To download a file and save it, the easiest way I have found is this:

<?php

file_put_contents('saved.xml', file_get_contents('http://www.xmlfiles.com/examples/simple.xml'));

You can then open the file with the simpleXML library like so:

$xml = simplexml_load_file('saved.xml');
var_dump($xml);

Hope that gives you enough info to get started.

See simpleXML for info on the simpleXML library.

Dale
  • 10,384
  • 21
  • 34
  • There is no need to save the file, you could just pass the URL as a parameter of simple_xml_load_file function. For example you can do this: $xml = simplexml_load_file('xml_file_url'); – aletede91 Feb 03 '16 at 11:15
1

You can download and save the xml to a local file by doing this:

$xmlstring = file_get_contents("http://domain.com/webservice/xmlfile.xml");
file_put_contents("path/localxmlfile.xml", $xmlstring);

To parse the xml file I suggest you to use DOMDocument class in combination with the DOMXPath class to query/search for specific elements.

DOMDocument: http://php.net/manual/de/class.domdocument.php

DOMXPath: http://php.net/manual/de/class.domxpath.php

sjkm
  • 3,887
  • 2
  • 25
  • 43
0

Hopefully you can find your answer on below link. Seems related topic.

How do you parse and process HTML/XML in PHP?

Community
  • 1
  • 1
JunM
  • 7,040
  • 7
  • 37
  • 58