Page 1 of 1

Streaming data using XML

Posted: Mon Jun 11, 2007 4:06 am
by aceconcepts
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?

Posted: Mon Jun 11, 2007 4:18 am
by onion2k
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.

Posted: Mon Jun 11, 2007 5:11 am
by aceconcepts
Hi,

Thanks for the response to my post.

I have considered the short-comings of this method already. I really need to find out more about the supplier's server setup (RAID...) etc...

Do you know of any examples of using file_get_contents or general data retrieval from XML files?

Thanks again.

Posted: Mon Jun 11, 2007 7:41 am
by NumberNineteen
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>

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]

Posted: Mon Jun 11, 2007 8:00 am
by aceconcepts
Wow! that looks superb.

Thanks a lot.

Posted: Mon Jun 11, 2007 8:22 am
by onion2k
aceconcepts wrote:I really need to find out more about the supplier's server setup (RAID...) etc...
Why would you need to know anything about the supplier's set up? The point of XML is to remove any need for considering that sort of thing.

Posted: Mon Jun 11, 2007 8:30 am
by aceconcepts
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.
I was interested in finding out more about their setup in case the file was unavailable (offline etc...).

Is this not required?

Posted: Mon Jun 11, 2007 10:02 am
by volka
In what way does this knowledge help you in this case?
"The data in unavailable. I feel so much better knowing this can happen even if you have raid10"? ;)

Posted: Mon Jun 11, 2007 10:57 am
by aceconcepts
Using RAID technology, failed drives switch to an operational drive. Is this not correct?

If this is correct then there is less reason to worry about being unable to access the desired files.

If this is incorrect then my bad.

Posted: Mon Jun 11, 2007 1:00 pm
by onion2k
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.

Posted: Tue Jun 12, 2007 6:16 am
by aceconcepts
Very true onion2k.

Thanks for your posts.