<?php
$printerIp = '192.168.123.102';  // Replace with your printer's IP address
$printerPort = 9100;           // Common port for network printers; adjust if necessary

// Printer commands - ESC/POS commands for initializing and simple text
$init = chr(27) . "@";                     // Initialize printer
$text = "Hello, this is a test print!\n";  // Text to print
$cut = chr(29) . 'V' . chr(66) . "\0";     // Command to cut the paper

// Full command string to send to the printer
$fullCommand = $init . $text . $cut;

// Creating a socket to connect to the printer
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
    echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
} else {
    // Connecting to the printer
    $result = socket_connect($socket, $printerIp, $printerPort);
    if ($result === false) {
        echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";
    } else {
        // Sending the command to the printer
        socket_write($socket, $fullCommand, strlen($fullCommand));
        socket_close($socket);
        echo "Print sent to printer successfully.\n";
    }
}
?>
