开发者

Upload XML feed to mysql using PHP

开发者 https://www.devze.com 2023-01-10 22:53 出处:网络
I have an XML feed coming in: <?xml version=\"1.0\" encoding=\"UTF-8\"?><product> <name>John</name>

I have an XML feed coming in:

<?xml version="1.0" encoding="UTF-8"?><product>
<name>John</name>
<contact_email>john@johnson.com</contact_email>
<contact_telephone>01234 567</contact_telephone>
<url>www.johnsone.com.com</url></product>

I need开发者_开发百科 to get this loaded to MySQL using php - have seen a few examples but all of them take a file saved locally.

My feed is taken from the internet so changes all of the time. Does anybody have any suggestions where to start?

Thanks


First you'll need to define a data model. Then you'll need an xml parser to parse the feed, extract the data and populate your data model. Then you'll need to pass your model object to a DAO which writes the data to your database.


If it's taken from the internet you can just do

<?php
$feedData = file_get_contents('http://mywebsite/myfeed.xml');
?>


if you want to parse the data and store then

  • create a table for product
  • parse the xml and get the fields
  • insert into db or update existing data

so, what's your problem?


To be a bit more specific: You'll obviously need to set up a table and database structure. I'm going to assume you have, or at least can figure out how to, set this up, and how to write to a database. If not, there are plenty of tutorials on that that should be plenty helpful. You'll need to use PHP's built-in MySQL library.

For parsing the XML you will probably want to use SimpleXML. It's not clear how your feed is coming in, but SimpleXML has the simplexml_load_string function that will let you pass it a string containing an XML document, however you get it, for parsing.

From there, you can just take the parsed XML and write it to your database. Any examples that use SimpleXML with a local file should be pretty easy to adapt using simplexml_load_string instead of simplexml_load_file, and doing whatever you're already (presumably) doing to get the data from this feed.

0

精彩评论

暂无评论...
验证码 换一张
取 消