Files
wevads-platform/scripts/import_all_creatives.php
2026-02-26 04:53:11 +01:00

97 lines
4.3 KiB
PHP

<?php
// Connexion DB
$pdo = new PDO('pgsql:host=localhost;dbname=adx_system', 'admin', 'admin123');
// Récupérer les réseaux
$networks = [];
$stmt = $pdo->query("SELECT id, name, api_key, api_url, affiliate_id FROM admin.affiliate_networks WHERE id IN (6,7)");
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$networks[$row['id']] = $row;
}
// Récupérer toutes les offres sans créative
$offers = $pdo->query("SELECT o.id, o.name, o.network_id FROM admin.affiliate_offers o LEFT JOIN admin.offer_creatives c ON o.id = c.offer_id WHERE c.offer_id IS NULL")->fetchAll(PDO::FETCH_ASSOC);
echo "Offres sans créative : " . count($offers) . "\n";
foreach ($offers as $offer) {
$network_id = $offer['network_id'];
if (!isset($networks[$network_id])) {
echo "Réseau inconnu pour offre {$offer['id']}, skipping.\n";
continue;
}
$net = $networks[$network_id];
echo "Traitement offre {$offer['id']} - réseau {$net['name']}\n";
if ($net['name'] == 'CX3 Ads' || $net['name'] == 'CX3') {
// API CAKE
$url = rtrim($net['api_url'], '/') . '/api/5/creatives.asmx/GetCreatives';
$params = [
'api_key' => $net['api_key'],
'affiliate_id' => $net['affiliate_id'],
'offer_id' => $offer['id'] // Tentative de filtrer par offre
];
$fullUrl = $url . '?' . http_build_query($params);
$xml = @simplexml_load_file($fullUrl);
if (!$xml) {
echo " Erreur API pour offre {$offer['id']}\n";
continue;
}
$count = 0;
foreach ($xml->creative as $creative) {
// Insérer ou mettre à jour
$creative_id = (string)$creative->creative_id;
$name = (string)$creative->creative_name;
$image_url = (string)$creative->image_url;
$link = (string)$creative->click_url;
// Vérifier si cette créative existe déjà (par creative_network_id)
$check = $pdo->prepare("SELECT id FROM admin.offer_creatives WHERE creative_network_id = ?");
$check->execute([$creative_id]);
if ($check->fetch()) {
$pdo->prepare("UPDATE admin.offer_creatives SET name = ?, image_url = ?, link = ? WHERE creative_network_id = ?")
->execute([$name, $image_url, $link, $creative_id]);
} else {
$pdo->prepare("INSERT INTO admin.offer_creatives (offer_id, creative_network_id, name, image_url, link, status) VALUES (?, ?, ?, ?, ?, 'active')")
->execute([$offer['id'], $creative_id, $name, $image_url, $link]);
}
$count++;
}
echo " Importé $count créatives pour offre {$offer['id']}\n";
} elseif ($net['name'] == 'Double M') {
// API Everflow
$url = rtrim($net['api_url'], '/') . "/v1/affiliates/offers/{$offer['id']}/creatives";
$url .= "?api_key=" . $net['api_key'];
$json = @file_get_contents($url);
if (!$json) {
echo " Erreur API pour offre {$offer['id']}\n";
continue;
}
$data = json_decode($json, true);
if (!isset($data['creatives'])) {
echo " Pas de créatives dans la réponse\n";
continue;
}
$count = 0;
foreach ($data['creatives'] as $creative) {
$creative_id = $creative['id'];
$name = $creative['name'];
$image_url = $creative['image_url'] ?? '';
$link = $creative['click_url'] ?? '';
$check = $pdo->prepare("SELECT id FROM admin.offer_creatives WHERE creative_network_id = ?");
$check->execute([$creative_id]);
if ($check->fetch()) {
$pdo->prepare("UPDATE admin.offer_creatives SET name = ?, image_url = ?, link = ? WHERE creative_network_id = ?")
->execute([$name, $image_url, $link, $creative_id]);
} else {
$pdo->prepare("INSERT INTO admin.offer_creatives (offer_id, creative_network_id, name, image_url, link, status) VALUES (?, ?, ?, ?, ?, 'active')")
->execute([$offer['id'], $creative_id, $name, $image_url, $link]);
}
$count++;
}
echo " Importé $count créatives pour offre {$offer['id']}\n";
}
}
echo "Terminé.\n";