tags:

views:

313

answers:

5

A vendor is going to be sending me some xml via an httppost to a script I am going to make. The script, when receives the httppost I need to somehow get the data from the xml and put it into my data-base.

I do have experience with all 3, PHP MYSQL and XML. I do know how to get values from XML using E4X but I do not know how to get values from XML using PHP (4.3.9).

so say the httppost is XML that looks like this:

<data>
<item sku="434322" price="15.00" color="blue" shape="round"/>
<item sku="434323" price="20.00" color="red" shape="square"/>
<item sku="434324" price="45.00" color="green" shape="triangle"/>
<item sku="434325" price="30.00" color="blue" shape="star"/>
</data>

How can I use PHP to loop through each node and get each attribute value?

Thanks!!

EDIT: It seems like getting the data from the XML is way complicated so if there is an easier way using something like CSV instead of XML please share that. The vendor did say they could send CSV or whatever format I want.

A: 

To handle CSV data, I use str_getcsv()

while (($data = str_getcsv() !== FALSE)
{do something useful with $data}

For XML, I recommend the SimpleXML class as opposed to the XML Parser. Simple's not the most flexible, but it works just fine if all you're doing is reading in some data and saving it.

If the form data is just a text string, then you'd

$xml = simplexml_load_string($string)

And then

foreach ($xml->children() as $child)

{do something interesting}

Or you can just ditch the XML and dump it into an array with xml_parse_into_struct

$xmlparser = xml_parser_create();

xml_parse_into_struct($xmlparser,$xmldata,$values);

xml_parser_free($xmlparser);

SimpleXML requires PHP5, he needs a PHP4 solution
Brian Fisher
A: 

You can use file_get_contents() to fetch the XML string from the POST request. But I am not quite sure that "php://input" already works in PHP4.

$xmlData = file_get_contents("php://input");

In PHP4 there is the extension domxml for working with XML. But I would strongly recommend using PHP5 and the DOM extension.

Uwe Mesecke
I believe: file_get_contents("php://input"); does work in 4. But I cannot use PHP5 right now.
John Isaacks
A: 

If your vendor can send it in any way, why not send it as:
Valid PHP code, just send it as an array. If the Vendors script are running as PHP themself then this would be even easier as they can output valid PHP code using: var_export().

Also you could try using the XMLparsing functions found at the comments at this page: http://nl2.php.net/manual/en/function.xml-parse.php they all look pretty okay.

Pim Jager
A: 

CSV would probably be simpler. You can use explode() to split the string (doc at http://us2.php.net/explode).

For example, you can split the input into lines, then split each line into pieces on the commas:

// split up lines
$lines = explode("\n", $_POST['data']);
$data = array();
foreach ($lines as $row) {
    // split a line on the commas
    $prop = explode(',', $row);

    // append an element to the array that contains 
    // the properties as an associative array
    $data[] = array('sku' => $prop[0],
                    'price' => $prop[1],
                    'color' => $prop[2],
                    'shape' => $prop[3]);

}

Here, I'm assuming you want to convert the input into an array of elements, each element of the form:

array(4) {
  ["sku"] =>
  "434322",
  ["price"] =>
  "15.00",
  ["color"] => 
  "blue"
  ["shape"] =>
  "round"
}
kristina
+1  A: 

This is how you loop trough XML nodes with PHP 4.0

<?php
$dom=domxml_open_file('file.xml');
// Or from a string
$dom=domxml_open_mem($xmlString);

$root=$dom->document_element;

$items=$root->get_elements_by_tagname('item');

// Loop trough childNodes
foreach ($items as $item) {
 $sku=$item->get_attribute('sku');

            // Your work here
}

echo $dom->dump_mem();
// Or to a file
$dom->dump_file('filename.xml');
?>
Christian Toma