Streaming data using XML

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Streaming data using XML

Post 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?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Post 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.
NumberNineteen
Forum Newbie
Posts: 7
Joined: Mon Jun 11, 2007 7:30 am

Post 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]
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Post by aceconcepts »

Wow! that looks superb.

Thanks a lot.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Post 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?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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"? ;)
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Post 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.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Post by aceconcepts »

Very true onion2k.

Thanks for your posts.
Post Reply