Your IP : 216.73.216.189


Current Path : /var/www/magento.test.indacotrentino.com/www/scripts/tools/
Upload File :
Current File : /var/www/magento.test.indacotrentino.com/www/scripts/tools/iubendaToDB.php

<?php
$servername = "localhost";
$username = "iubendaindaco";
$password = "indaco";
$dbname = "iubenda_indaco";

$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$url = 'https://consent.iubenda.com/consent?limit=100';

//echo $url . "\n";
/*
$url = 'https://consent.iubenda.com/consent/08649c56-127f-4f2d-987a-4e4ebaafb873';
$url = 'https://consent.iubenda.com/consent?from_time=1664582400&limit=1';
$url = 'https://consent.iubenda.com/consent?starting_after=08649c56-127f-4f2d-987a-4e4ebaafb873';
$url = 'https://consent.iubenda.com/consent?to_time=1664582400&limit=10';
$url = 'https://consent.iubenda.com/consent?limit=2';
*/

$req = curl_init();
curl_setopt($req, CURLOPT_RETURNTRANSFER, true);
curl_setopt($req, CURLOPT_URL, $url);
curl_setopt($req, CURLOPT_HTTPHEADER, array(
    'ApiKey: MIMKEgxQw0kpGwldWMDSSQb5fmUr9GpI',
    'Content-Type: application/json'
));
$response = curl_exec($req);

$jsonReverse = array_reverse(json_decode($response,true));

foreach (array_reverse(json_decode($response,true)) as $item) {
    $policy = array_key_exists('policy', $item['preferences']) ? ($item['preferences']['policy'] ? '1' : '0') : 'null';
    $terms = array_key_exists('terms', $item['preferences']) ? ($item['preferences']['terms'] ? '1' : '0') : 'null';
    $adult = array_key_exists('adult', $item['preferences']) ? ($item['preferences']['adult'] ? '1' : '0') : 'null';
    $consent1 = array_key_exists('consent1', $item['preferences']) ? ($item['preferences']['consent1'] ? '1' : '0') : 'null';
    $consent2 = array_key_exists('consent2', $item['preferences']) ? ($item['preferences']['consent2'] ? '1' : '0') : 'null';
    $consent3 = array_key_exists('consent3', $item['preferences']) ? ($item['preferences']['consent3'] ? '1' : '0') : 'null';
    $consent4 = array_key_exists('consent4', $item['preferences']) ? ($item['preferences']['consent4'] ? '1' : '0') : 'null';
    $subject_id = $item['subject']['id'] ?? null;
    $subject_email = $item['subject']['email'] ?? null;
    $subject_firstname = $item['subject']['first_name'] ?? null;
    $subject_lastname = $item['subject']['last_name'] ?? null;
    $subject_ownerid = $item['subject']['owner_id'] ?? null;
    $subject_verified = array_key_exists('verified', $item['subject']) ? ($item['subject']['verified'] ? '1' : '0') : 'null';

    $sql = "select * from consent where identifier='" . $item['id'] . "' limit 1";
    $result = $conn->query($sql);

    if ($result->num_rows == 0) {
        $sql = "INSERT INTO consent (identifier, owner, source, timestamp, policy, terms, adult, consent1, consent2, consent3,
                     consent4, ipaddress, id_subject, email, firstname, lastname, id_owner, verified)
        VALUES (
                '" . $item['id'] . "',
                '" . $item['owner'] . "',
                '" . $item['source'] . "',
                STR_TO_DATE('" . $item['timestamp'] . "', '%Y-%m-%dT%H:%i:%sZ'),
                $policy,
                $terms,
                $adult,
                $consent1,
                $consent2,
                $consent3,
                $consent4,
                '" . $item['ip_address'] . "',
                '$subject_id',
                '$subject_email',
                '$subject_firstname',
                '$subject_lastname',
                '$subject_ownerid',
                '$subject_verified'
        )";

        if ($conn->query($sql) === TRUE) {
            echo "New record created successfully: " . $item['id'] . " $subject_email \n";
        } else {
            echo "Error: " . $sql . "<br>" . $conn->error;
        }
    }
}


$conn->close();