Mass Action Error - Action PHP code is wrong: syntax error, unexpected token "

  • Posts: 1
  • Thank you received: 0
  • Hikaauction Standard Hikashop Business
4 months 1 week ago #357747

-- HikaShop version -- : 5.0.2
-- Joomla version -- : 5.0.1
-- PHP version -- : 8.1.26
-- Browser(s) name and version -- : Google Chrome
-- Error-message(debug-mod must be tuned on) -- : Action PHP code is wrong: syntax error, unexpected token "

Hello.
I am trying to execute a PHP code in Mass Action that will transfer data to an API.
I get this error - Action PHP code is wrong: syntax error, unexpected token "

<?php
// Presupunând că ai ID-ul adresei de facturare
$billingAddressId = '{order_billing_address_id}';

// Conexiunea la baza de date Joomla
$db = JFactory::getDbo();

// Construim prima interogare pentru a obține detalii despre adresă
$query = $db->getQuery(true);
$query->select($db->quoteName(array('address_company', 'address_vat', 'address_firstname', 'address_lastname', 'address_street', 'address_state', 'address_country')));
$query->from($db->quoteName('db_name_hikashop_address'));
$query->where($db->quoteName('address_id') . ' = ' . $db->quote($billingAddressId));

// Executăm prima interogare
$db->setQuery($query);
$addressResult = $db->loadAssoc();

if ($addressResult) {
    // Extragem codul de stat și codul țării
    $stateCode = substr($addressResult['address_state'], strrpos($addressResult['address_state'], '_') + 1);
    $countryCode = substr($addressResult['address_country'], strrpos($addressResult['address_country'], '_') + 1);

    // Interogare pentru numele zonei
    $query = $db->getQuery(true);
    $query->select($db->quoteName('zone_name'));
    $query->from($db->quoteName('db_name_hikashop_zone'));
    $query->where($db->quoteName('zone_id') . ' = ' . $db->quote($stateCode));
    $db->setQuery($query);
    $zoneName = $db->loadResult();

    // Interogare pentru numele țării
    $query = $db->getQuery(true);
    $query->select($db->quoteName('zone_name'));
    $query->from($db->quoteName('db_name_hikashop_zone'));
    $query->where($db->quoteName('zone_id') . ' = ' . $db->quote($countryCode));
    $db->setQuery($query);
    $countryName = $db->loadResult();

    // Pregătim datele pentru API-ul SmartBill
    $smartBillData = [
        "data" => date("Y-m-d"),
        "client" => [
            "nume" => $addressResult['address_firstname'] . " " . $addressResult['address_lastname'],
            "cif" => "48956989",
            "regCom" => "",
            "adresa" => $addressResult['address_street'] . ", " . ($zoneName ? $zoneName : 'Unknown') . ", " . ($countryName ? $countryName : 'Unknown'),
            // Alte detalii despre client
        ],
        // Produsele sau serviciile facturate
        // ...
    ];

    // URL-ul API-ului SmartBill
    $url = 'https://api.invoice-web-app-ro';

    // Inițializăm sesiunea cURL
    $ch = curl_init($url);

    // Setăm opțiunile cURL necesare
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($smartBillData));
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Content-Type: application/json',
        'Authorization: Basic ' . base64_encode("mail:tokenkey")
    ]);

    // Executăm cererea cURL
    $response = curl_exec($ch);
    $curlError = curl_error($ch);

    // Închidem sesiunea cURL
    curl_close($ch);

    // Afișăm răspunsul sau eroarea în consola JavaScript
    echo "<script type='text/javascript'>";
    if ($curlError) {
        echo "console.error('Eroare cURL: " . json_encode($curlError) . "');";
    } else {
        echo "console.log('Răspuns SmartBill: " . json_encode($response) . "');";
    }
    echo "</script>";
} else {
    echo "<script>console.log('No address or zone result found');</script>";
}
?>

I tested the version of the php code that brings certain information to the console to do tests and it works perfectly.

// Presupunând că ai ID-ul adresei de facturare
$billingAddressId = '{order_billing_address_id}'; // Înlocuiește cu eticheta corectă

// Conexiunea la baza de date Joomla
$db = JFactory::getDbo();

// Construim prima interogare pentru a obține detalii despre adresă
$query = $db->getQuery(true);
$query->select($db->quoteName(array('address_company', 'address_vat', 'address_firstname', 'address_lastname', 'address_street', 'address_state', 'address_country')));
$query->from($db->quoteName('db_name_hikashop_address'));
$query->where($db->quoteName('address_id') . ' = ' . $db->quote($billingAddressId));

// Executăm prima interogare
$db->setQuery($query);
$addressResult = $db->loadAssoc();

if ($addressResult) {
    // Extragem codul de stat și codul țării
    $stateCode = substr($addressResult['address_state'], strrpos($addressResult['address_state'], '_') + 1);
    $countryCode = substr($addressResult['address_country'], strrpos($addressResult['address_country'], '_') + 1);

    // Interogare pentru numele zonei
    $query = $db->getQuery(true);
    $query->select($db->quoteName('zone_name'));
    $query->from($db->quoteName('db_name_hikashop_zone'));
    $query->where($db->quoteName('zone_id') . ' = ' . $db->quote($stateCode));
    $db->setQuery($query);
    $zoneName = $db->loadResult();

    // Interogare pentru numele țării
    $query = $db->getQuery(true);
    $query->select($db->quoteName('zone_name'));
    $query->from($db->quoteName('db_name_hikashop_zone'));
    $query->where($db->quoteName('zone_id') . ' = ' . $db->quote($countryCode));
    $db->setQuery($query);
    $countryName = $db->loadResult();

    // Pregătim informațiile pentru afișare
    $company = addslashes($addressResult['address_company']);
    $vat = addslashes($addressResult['address_vat']);
    $firstname = addslashes($addressResult['address_firstname']);
    $lastname = addslashes($addressResult['address_lastname']);
    $street = addslashes($addressResult['address_street']);
    $state = $zoneName ? addslashes($zoneName) : 'Unknown';
    $country = $countryName ? addslashes($countryName) : 'Unknown';

    // Afișăm informațiile în consola JavaScript
    echo "<script>console.log('Address Company: $company, VAT: $vat, First Name: $firstname, Last Name: $lastname, Street: $street, State: $state, Country: $country');</script>";
} else {
    echo "<script>console.log('No address result found');</script>";
}

I tested php code with cURL for simplified test but I get the same error
<?php
$url = "https://bcc-heritage.com/component/hikashop/product/1523-albania-p-41s2-1976-3-leke";

// Inițializarea sesiunii cURL
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

// Execută cererea cURL
$response = curl_exec($curl);
curl_close($curl);

// Verifică dacă cererea a reușit
if ($response) {
    // Caută titlul în răspunsul HTML
    if (preg_match('/<title>(.*?)<\/title>/', $response, $matches)) {
        $title = $matches[1];
        echo "Titlul paginii este: " . $title;
    } else {
        echo "Nu s-a putut găsi titlul în HTML.";
    }
} else {
    echo "Cererea cURL a eșuat.";
}
?>

At this moment I don't know what I should try to be able to send the code to the invoicing system API.
Please help me understand where I am wrong or what I should do.
Thank you

[Moderator:]Please do not post long code like this, add a file attachment instead.[/Moderator]

Last edit: 4 months 1 week ago by Philip.

Please Log in or Create an account to join the conversation.

  • Posts: 81540
  • Thank you received: 13071
  • MODERATOR
4 months 1 week ago #357754

Hi,

First, you want to activate the "debug" setting of the Joomla configuration in order to get the full error message.
Now, the error message will point at the line number and file where the problem is.
However, it's it's code that's been "eval" ( www.php.net/manual/en/function.eval.php ) by the mass action system, the error message should point at a file of the mass action system and not to where the problem is in your code.
So move your code to a PHP file and in your mass action, just keep the first line

$billingAddressId = '{order_billing_address_id}';
so that it can replace the tag, and then add an include of your PHP file.
That way, the error message will point at your file with the line number in that file so you'll be able to easily pin point the problem in your code.

Please Log in or Create an account to join the conversation.

Time to create page: 0.060 seconds
Powered by Kunena Forum