Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

From XML to MySQL


I have one external API which output some data in XML for me. I have to handle the data and save in my MySQL databse (It will be saved in different tables, I have allready created ORM and create connections/relationships between models). What is the best way to do that?


edited Feb '16

Load XML via create models and fill data from SimpleXML elements, save and profit :)

edited Feb '16

Ok, I have loaded my XML file with Php simpleXML but i have next situation ... My XML file looks like this:

          <name>First shop</name>
          <name>Second shop</name>

Im accessing this XML data like a objects :

foreach($xml->market->shop as $shop)
        echo $shop->url;
        echo $shop->storeId;
        echo $shop->name;

Is there a way to access data from this url links (which is also an XML file with products list of that shop) ? So every shop have url link with XML file of his products. I have to get all data and store in MySQL databse (all data means shops data from main XML file and data from url links which are product list of that shop).

Sure, but you have to download target XML. Use code like this

$context  = stream_context_create(array('http' => array('header' => 'Accept: application/xml')));
$content = file_get_contents($shop->url, false, $context);
$xml = simplexml_load_string($content);

edited Feb '16

Or you can use directly, it will accept URL.

Im using it but the problem is when im trying to load XML file from any URL I have this error :

Warning: simplexml_load_file(): php_network_getaddresses: getaddrinfo failed: No address associated with hostname in /var/www/html/myproject.local/test1.php on line 4

Warning: simplexml_load_file(https://link_from_my_xml_file.xml): failed to open stream: php_network_getaddresses: getaddrinfo failed: No address associated with hostname in /var/www/html/myproject.local/test1.php on line 4

Warning: simplexml_load_file(): I/O warning : failed to load external entity "https://link_from_my_xml_file.xml" in /var/www/html/myproject.local/test1.php on line 4

Im testing it on simple script :


$url = "https://link_from_my_xml_file.xmll"; 
$xml = simplexml_load_file($url);
echo $xml;


When I load XML file from my local machine it works fine ...

edited Mar '16

You have something missconfigured on the server then

also check your php.ini if you have

``` allowurlfopen = "On" ```

otherwise it will not work

The problem was caused by Vagrant. Im using a developmnet statck (Ubuntu VBox), and that caused some issue with "hosts". Everything works fine, thx for help.