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

Hello,

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?

Thanks



7.0k
Accepted
answer
edited Feb '16

Load XML via http://php.net/manual/en/book.simplexml.php create models and fill data from SimpleXML elements, save and profit :)



9.3k
edited Feb '16

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

<xml>
    <status>1</status>
    <market>
      <shop>
          <url>https://some.api.link.with.products.of.shop.com/file_with_products.xml</url>
          <shopId>1</shopId>
          <name>First shop</name>
      </shop>
      <shop>
          <url>https://some.api.link.with.products.of.shop.com/file_with_products.xml</url>
          <shopId>2</shopId>
          <name>Second shop</name>
      </shop>
    </market>
</xml>

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);


9.3k
edited Feb '16

Or you can use http://php.net/manual/en/function.simplexml-load-file.php 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 :

<?php

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



9.3k
edited Mar '16

You have something missconfigured on the server then

http://stackoverflow.com/questions/6275535/php-error-php-network-getaddresses-getaddrinfo-failed-while-getting-informat http://stackoverflow.com/questions/8210099/php-php-network-getaddresses-getaddrinfo-failed-no-such-host-is-known

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.