Current File : //var/www/pharmacius/probar_prestashop.php |
<?php
$api_url = 'https://prestashop.maausk.tech/api/';
$api_key = '7GEEPRS2PV7AC3YE3JS9LV7YN45SC54K';
function llamar_api($endpoint) {
global $api_url, $api_key;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_url . $endpoint);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $api_key . ':');
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Accept: application/xml']);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
// Cargar XML raíz
$xml_raw = llamar_api("orders/");
$xml = simplexml_load_string($xml_raw);
echo "<h2>Pedidos encontrados:</h2>";
// Acceder a los nodos dentro de <prestashop><orders><order>
if (isset($xml->orders->order)) {
foreach ($xml->orders->order as $orderNode) {
$id = (int)$orderNode['id'];
echo "<h3>Pedido #$id</h3>";
// Obtener detalles de cada pedido
$detalle_xml = llamar_api("orders/$id");
$detalle = simplexml_load_string($detalle_xml);
$pedido = $detalle->order;
// Mostramos XML (opcional, solo para depuración)
// echo "<pre>" . htmlspecialchars($detalle_xml) . "</pre>";
$estado = isset($pedido->current_state) ? trim((string)$pedido->current_state) : 'N/A';
$fecha = isset($pedido->date_add) ? trim((string)$pedido->date_add) : 'N/A';
$id_cliente = isset($pedido->id_customer) ? (int)$pedido->id_customer : 0;
echo "Fecha: $fecha<br>";
echo "Estado: $estado<br>";
echo "Cliente ID: $id_cliente<br>";
echo "<strong>Productos:</strong><ul>";
if (isset($pedido->associations->order_rows->order_row)) {
foreach ($pedido->associations->order_rows->order_row as $linea) {
$producto = isset($linea->product_id) ? (int)$linea->product_id : 0;
$nombre = isset($linea->product_name) ? (string)$linea->product_name : 'Sin nombre';
$cantidad = isset($linea->product_quantity) ? (int)$linea->product_quantity : 0;
echo "<li>$nombre (ID $producto) - Cantidad: $cantidad</li>";
}
} else {
echo "<li>No hay productos</li>";
}
echo "</ul><hr>";
}
} else {
echo "<p>No se encontraron pedidos.</p>";
}
?>