Streaming data using XML
Moderator: General Moderators
- aceconcepts
- DevNet Resident
- Posts: 1424
- Joined: Mon Feb 06, 2006 11:26 am
- Location: London
Streaming data using XML
Hi,
I am setting up an online shop and my supplier keeps track of their stock qty and offer their stock availability in the form of an XML file. They say that I can automatically "pick up" the stock availability from this XML file for each product/link it to my shop.
In a nutshell, what I want to be able to do is check the stock availabliity within my shop before someone actually pays for something. However, I do not physically hold the stock so I never know how much stock there actually is (except for the XML file offered by supplier).
How can I do this?
I am setting up an online shop and my supplier keeps track of their stock qty and offer their stock availability in the form of an XML file. They say that I can automatically "pick up" the stock availability from this XML file for each product/link it to my shop.
In a nutshell, what I want to be able to do is check the stock availabliity within my shop before someone actually pays for something. However, I do not physically hold the stock so I never know how much stock there actually is (except for the XML file offered by supplier).
How can I do this?
Grab the file from the supplier using file_get_contents or fopen or cURL or something, and parse it to get the quantity. That's pretty easy to do.
What'll be more of a problem is figuring out what to do if the file is unavailable, if the supplier's server is offline or there's a routing problem or something that stops you accessing the data.
What'll be more of a problem is figuring out what to do if the file is unavailable, if the supplier's server is offline or there's a routing problem or something that stops you accessing the data.
- aceconcepts
- DevNet Resident
- Posts: 1424
- Joined: Mon Feb 06, 2006 11:26 am
- Location: London
-
NumberNineteen
- Forum Newbie
- Posts: 7
- Joined: Mon Jun 11, 2007 7:30 am
feyd | Please use
You could also write the XSL to reformat the original XML and output new XML so you can organize the data the way you want.
feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
A simple XSL transform will get you what you need... (note: Make sure "extension=php_xsl.dll" is uncommented in your php.ini)Code: Select all
<?php
$xml = "http://www.thedomain.com/theXML.xml";
$xsl = "your.xsl";
$xp = new XsltProcessor();
// create a DOM document and load the XSL stylesheet
$xsl_doc = new DomDocument;
$xsl_doc->load($xsl);
// import the XSL styelsheet into the XSLT process
$xp->importStylesheet($xsl_doc);
// create a DOM document and load the XML data
$xml_doc = new DomDocument;
$xml_doc->load($xml);
// pass the params you need in an array to the XSL
$params['productID'] = $productID;
$params['anotherID'] = $anotherID;
$namespace = "http://yourdomain/namespace";
$xp->setParameter($namespace, $params);
$html = $xp->transformToXML($xml_doc);
echo $html;
?>Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" media-type="text/xml" indent="yes" doctype-system="http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd" doctype-public="-//W3C//DTD XHTML 1.1//EN" encoding="utf-8" omit-xml-declaration="yes"/>
<xsl:param name="productID"/>
<xsl:param name="anotherID"/>
<xsl:template match="/">
<xsl:value-of select="//products/product[@id=$productID]/@quantity"/>
</xsl:template>
</xsl:stylesheet>
feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]- aceconcepts
- DevNet Resident
- Posts: 1424
- Joined: Mon Feb 06, 2006 11:26 am
- Location: London
- aceconcepts
- DevNet Resident
- Posts: 1424
- Joined: Mon Feb 06, 2006 11:26 am
- Location: London
I was interested in finding out more about their setup in case the file was unavailable (offline etc...).What'll be more of a problem is figuring out what to do if the file is unavailable, if the supplier's server is offline or there's a routing problem or something that stops you accessing the data.
Is this not required?
- aceconcepts
- DevNet Resident
- Posts: 1424
- Joined: Mon Feb 06, 2006 11:26 am
- Location: London
When you're writing something that accesses a web service (eg an XML file) you should ignore the probability of whether or not it'll be down. The only relevant question is "Might this ever be unavailable?". If the answer is yes then you need to code a system that's robust enough to work without the web service data (even if it just chucks out a 'please try again later' message).
The next thing to remember is that things can and do go wrong, so there's always going to be a chance the data won't be available.
The next thing to remember is that things can and do go wrong, so there's always going to be a chance the data won't be available.
- aceconcepts
- DevNet Resident
- Posts: 1424
- Joined: Mon Feb 06, 2006 11:26 am
- Location: London