<?php
$data = [
['Code' => 'DES001', 'Commennte' => '', 'Quantity' => 1, 'Price' => '145.00', 'Unity' => 1],
['Code' => 'DES002', 'Commennte' => '', 'Quantity' => 5, 'Price' => '133.00', 'Unity' => 1],
['Code' => 'DES003', 'Commennte' => '', 'Quantity' => 9, 'Price' => '125.00', 'Unity' => 1],
];
// expected <value><![CDATA...</value> payload
$payload = [
'Pedido' => [
'Nombre' => '',
'Producto' => [
'Product' => array_map(function($v) {
return [
'ClaveProducto' => $v['Code'],
'Cantidad' => $v['Quantity'],
'Precio' => $v['Price'],
'Unidad' => 'PIEZA',
'Comentario' => '',
];
}, $data)
],
]
];
// init domdocument
$xml = new DOMDocument("1.0", "ISO-8859-15");
// optional
$xml->preserveWhiteSpace = false;
$xml->formatOutput = true;
// create request wrapper
$request = $xml->createElement("request");
// create method (CDATA), append it to the wrapper
$method = $xml->createElement("method");
$method->appendChild($xml->createCDATASection('registra'));
// create param wrapper
$param = $xml->createElement("param");
// create name (CDATA), append it to the param
$name = $xml->createElement("name");
$name->appendChild($xml->createCDATASection('pedido'));
$param->appendChild($name);
// create value (CDATA), append it to the param
$value = $xml->createElement("value");
$value->appendChild($xml->createCDATASection(json_encode($payload)));
$param->appendChild($value);
// append method to request
$request->appendChild($method);
// append param to request
$request->appendChild($param);
// request root
$xml->appendChild($request);
// output
echo $xml->saveXML($xml->documentElement);
preferences:
28.78 ms | 402 KiB | 5 Q