Page 1 of 2
How do you extract fred from "<company>fred</company>"?
Posted: Tue Mar 05, 2013 4:27 am
by simonmlewis
I have updated this thread as a means of doing it has been lighted but still unsure.
I've been told that a stock system we connect with will pass data via an XML document to us, and from that I can query it and update our "rcstock" field.
They pass "item-code" among various "<nodes>" I think!!
I've read various web pages about this, but it's terribly confusing. I need a simple means of reading the document, and then querying (and possibly updating a database) locally. I think it's read with plenty of -> these types of code, but really am lost.
It feels like I have asked "how do I query a simple XML", and someone has given me a Harry Potter sized book to learn, when I just need the basics. And perhaps will progress from there if needed.
Re: XML - how do you read it, and update a DB from it?
Posted: Tue Mar 05, 2013 4:35 am
by requinix
SimpleXML gives you a really easy way to look at XML. From there you have a few options, a query in a set of loops being one of the least desirable, depending on what all you can do.
What does the XML look like exactly and what data do you need from it?
Re: XML - how do you read it, and update a DB from it?
Posted: Tue Mar 05, 2013 4:48 am
by simonmlewis
Hi.
I'm told it's done through a <form> post. Which puzzles me, as that's not normally in XML - I see those as posted to a variable, that I capture $name = $_POST['name'];.
This is an example, and I'd like to capture "Item-Code", and what will be called "primarystockremaining".
I then query that itemcode against my database, and if primarystockremaining is 0, I set a field to "out of stock", otherwise it's kept as "in stock".
Code: Select all
<romancart-transaction-data><orderid>1934945</orderid><passthru/><paid-flag>True</paid-flag><sales-record-fields><company-name/><title>Mr</title><first-name>John</first-name><last-name>Doe</last-name><address1>10 The Road</address1><address2/><town>AnyTown</town><county>AnyCounty</county><postcode>AN11TC</postcode><country>UNITED KINGDOM</country><state/><delivery-company-name/><delivery-title/><delivery-first-name/><delivery-last-name/><delivery-address1/><delivery-address2/><delivery-town/><delivery-county/><delivery-postcode/><delivery-country/><delivery-state/><delivery-phone/><phone>08702467619</phone><email>support@romancart.com</email><sub-total>4.99</sub-total><actual-discount>0</actual-discount><total-price>4.99</total-price><price-includes-tax>True</price-includes-tax><total-tax>0.74</total-tax><discount-amount>0</discount-amount><total-shipping>0</total-shipping><actual-shipping-type>FREE</actual-shipping-type><created-date>10/1/2007 3:41:31 PM</created-date><status>Charged</status><psp>RomanPay</psp><customer-email-sent>True</customer-email-sent><store-email-sent>True</store-email-sent><currency-code>GBP</currency-code><accepted-ts-and-cs>True</accepted-ts-and-cs><sourced>Unknown</sourced><shipping-status-date>10/1/2007 3:41:33 PM</shipping-status-date><shipping-status>Not Sent</shipping-status><status-date>10/1/2007 3:41:41 PM</status-date><accepts-emails>True</accepts-emails><promotional-code>ABC123</promotional-code></sales-record-fields><extra-checkout-fields><accepts-emails>True</accepts-emails><extra-checkout-field><extra-checkout-field-name>this is a test</extra-checkout-field-name><extra-checkout-field-data>test</extra-checkout-field-data></extra-checkout-field></extra-checkout-fields><order-items><order-item><item-code>Product2</item-code><item-name>Bottle of Piesporter</item-name><price>4.99</price><quantity>1</quantity></order-item></order-items></romancart-transaction-data>
Re: XML - how do you read it, and update a DB from it?
Posted: Tue Mar 05, 2013 9:09 am
by simonmlewis
Got a bit more info on this now, which may any those who want to help me, understand it more.
It's being posted in a <form>
Code: Select all
<form action=romancarthandler.php method=post>
<input type=hidden name=romancartxml value=’
<romancart-transaction-data>
<orderid>1934945</orderid>
<passthru/>
<paid-flag>True</paid-flag>
<sales-record-fields>
<company-name/>
<title>Mr</title>
<first-name>John</first-name>
<last-name>Doe</last-name>
<address1>10 The Road</address1>
<address2/>
<town>AnyTown</town>
<county>AnyCounty</county>
<postcode>AN11TC</postcode>
<country>UNITED KINGDOM</country>
<state/>
<delivery-company-name/>
<delivery-title/>
<delivery-first-name/>
<delivery-last-name/>
<delivery-address1/>
<delivery-address2/>
<delivery-town/>
<delivery-county/>
<delivery-postcode/>
<delivery-country/>
<delivery-state/>
<delivery-phone/>
<phone>08702467619</phone>
<email>support@romancart.com</email>
<sub-total>4.99</sub-total>
<actual-discount>0</actual-discount>
<total-price>4.99</total-price>
<price-includes-tax>True</price-includes-tax>
<total-tax>0.74</total-tax>
<discount-amount>0</discount-amount>
<total-shipping>0</total-shipping>
<actual-shipping-type>FREE</actual-shipping-type>
<created-date>10/1/2007 3:41:31 PM</created-date>
<status>Charged</status>
<psp>RomanPay</psp>
<customer-email-sent>True</customer-email-sent>
<store-email-sent>True</store-email-sent>
<currency-code>GBP</currency-code>
<accepted-ts-and-cs>True</accepted-ts-and-cs>
<sourced>Unknown</sourced>
<shipping-status-date>10/1/2007 3:41:33 PM</shipping-status-date>
<shipping-status>Not Sent</shipping-status>
<status-date>10/1/2007 3:41:41 PM</status-date>
<accepts-emails>True</accepts-emails>
<promotional-code>ABC123</promotional-code>
</sales-record-fields>
<extra-checkout-fields>
<accepts-emails>True</accepts-emails>
<extra-checkout-field>
<extra-checkout-field-name>this is a test</extra-checkout-field-name>
<extra-checkout-field-data>test</extra-checkout-field-data>
</extra-checkout-field></extra-checkout-fields>
<order-items><order-item>
<item-code>Product2</item-code>
<item-name>Bottle of Piesporter</item-name>
<price>4.99</price>
<quantity>1</quantity>
</order-item></order-items>
</romancart-transaction-data>
’>
<input type=submit>
</form>
So how do I access that as a POSTED element in my handler.php page? I guess it's to do wtih capturing the XML contents of "romancartxml", but how?
And how do you then see, for example, "postcode", and assign that to a variable.
Once I can see how to do that, I'm sorted I think.
Simon
Re: XML - how do you read it, and update a DB from it?
Posted: Tue Mar 05, 2013 9:49 am
by simonmlewis
Someone asked me this: "do you know how to read the data from inside tags like <postcode>TM11 1ZZ</postcode?"
The answer is no.
They asked this because they said I should forget about the XML side for a moment, and concentrate on how to read tags.
If I can suss that, I guess I'm there?!
Re: How do you extract fred from "<company>fred</company>"?
Posted: Tue Mar 05, 2013 11:19 am
by simonmlewis
Why does this not work?
Code: Select all
<form action=romancarthandler.php method=post>
<input type=hidden name=romancartxml value='<title>Mr</title> <first-name>John</first-name> <last-name>Doe</last-name> <postcode>AN11TC</postcode> <price>4.99</price> <quantity>1</quantity>'>
<input type=submit>
</form>
Posted to this...
If I echo $romancartxml2, it shows everything. But $postcode produces nothing.
Code: Select all
$romancartxml = $_POST['romancartxml'];
$romancartxml2 = htmlspecialchars($romancartxml);
$count = preg_match('/<quantity>(.*)<\/quantity>/s', '$romancartxml2', $matches);
$postcode = $matches[0];
echo "$postcode";
Should this not work?
Re: How do you extract fred from "<company>fred</company>"?
Posted: Tue Mar 05, 2013 11:38 am
by brahama
I really dont understand what you should do. Also im really new to PHP. But i would recommend you to read SimpleXML object. There's a funciotn also that reads xml from string so u could pass the value as a variable and would create de simpleXML object. Then traverse it using its methods. XPATH is quite powerful.
Re: How do you extract fred from "<company>fred</company>"?
Posted: Tue Mar 05, 2013 11:42 am
by simonmlewis
Hi. I've tried that. But was told to ignore the xml side for now as there should be a way to read inside particular tags.
Re: How do you extract fred from "<company>fred</company>"?
Posted: Tue Mar 05, 2013 12:36 pm
by Christopher
simonmlewis wrote:Posted to this...
If I echo $romancartxml2, it shows everything. But $postcode produces nothing.
Code: Select all
$romancartxml = $_POST['romancartxml'];
$romancartxml2 = htmlspecialchars($romancartxml);
$count = preg_match('/<quantity>(.*)<\/quantity>/s', '$romancartxml2', $matches);
$postcode = $matches[0];
echo "$postcode";
Should this not work?
Try:
Code: Select all
$count = preg_match('/[\<]quantity[\>](.*)[\<]\/quantity[\>]/s', '$romancartxml2', $matches);
Re: How do you extract fred from "<company>fred</company>"?
Posted: Tue Mar 05, 2013 12:40 pm
by simonmlewis
That echos nothing. A blank page.
Re: How do you extract fred from "<company>fred</company>"?
Posted: Tue Mar 05, 2013 12:42 pm
by simonmlewis
Code: Select all
<?php
$romancartxml = $_POST['romancartxml'];
$romancartxml2 = htmlspecialchars($romancartxml);
$count = preg_match('/[\<]quantity[\>](.*)[\<]\/quantity[\>]/s', '$romancartxml2', $matches);
$postcode = $matches[0];
echo "$postcode";
?>
Re: How do you extract fred from "<company>fred</company>"?
Posted: Tue Mar 05, 2013 1:15 pm
by mecha_godzilla
Hi,
Christopher's code example does work, but you need to make sure you're not using htmlspecialchars() because your string will end up looking like this instead:
You'll need to view the page source in your browser to see that, because it looks normal when displayed in the browser.
HTH,
Mecha Godzilla
Re: How do you extract fred from "<company>fred</company>"?
Posted: Tue Mar 05, 2013 1:18 pm
by simonmlewis
Code: Select all
<?php
$romancartxml = $_POST['romancartxml'];
$count = preg_match('/[\<]quantity[\>](.*)[\<]\/quantity[\>]/s', '$romancartxml', $matches);
$postcode = $matches[0];
echo "$postcode";
?>
This doesn't work either.
And if I echo $romancartxml, it just shows the text.
Re: How do you extract fred from "<company>fred</company>"?
Posted: Tue Mar 05, 2013 1:21 pm
by mecha_godzilla
Hi,
You need to fix this line of code:
Code: Select all
$count = preg_match('/[\<]quantity[\>](.*)[\<]\/quantity[\>]/s', '$romancartxml', $matches);
PHP won't interpret the $romancartxml variable unless you either omit the single quotes (') or replace them with double quotes (") instead.
HTH,
M_G
Re: How do you extract fred from "<company>fred</company>"?
Posted: Tue Mar 05, 2013 1:24 pm
by simonmlewis
You beauty!!
Code: Select all
<?php
$romancartxml = $_POST['romancartxml'];
$count = preg_match("/[\<]first-name[\>](.*)[\<]\/first-name[\>]/s", "$romancartxml", $firstname);
$count = preg_match("/[\<]postcode[\>](.*)[\<]\/postcode[\>]/s", "$romancartxml", $postcode);
$firstname = $firstname[0];
$postcode = $postcode[0];
echo "$firstname $postcode";
?>
Got it. And done two as I will need to get hold of two of the parsed amounts.
Thanks SOOO much. Been working on this all day.