You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP(CodeIgniter)连接Zebra ZD-420打印机9100端口遭连接拒绝求助

Connection Refused Error When Sending ZPL to Zebra ZD-420 via PHP Socket

I'm trying to send ZPL text to a Zebra ZD-420 printer using PHP (CodeIgniter framework), but keep getting a Connection refused error. I've tried two different libraries and followed a socket programming tutorial, but the issue persists. I've verified the port is open, and Python code connects successfully—unfortunately, I can't use Python in my host environment.

My PHP Code:

/* construct the label */
$label = "^XA ^LH0,0 ^FO4,2 ^GB804,798,4 ^FS ^FO4,2 ^GB548,52,4 ^FS ^FO19,12 ^ADN,36,20 ^FDRossi Andrea ^FS ^FO548,2 ^GB260,52,4 ^FS ^FO561,16 ^ADN,36,10 ^FDP/V ^FS ^FO611,16 ^ADN,36,10 ^FD1:3 ^FS ^FO693,16 ^ADN,36,10 ^FD18/12/14 ^FS ^FO20,66 ^GB540,135,80 ^FS ^FO21,70 ^ADN,135,25 ^FR ^FDREGGIO CALABRIA ^FS ^FO720,66 ^GB72,135,80 ^FS ^FO721,70 ^ADN,135,25 ^FR ^FD13 ^FS ^FO4,210 ^GB128,166,4 ^FS ^FO128,210 ^GB680,166,4 ^FS ^FO136,220 ^ADN,36,15 ^FDProva Rossi ^FS ^FO136,276 ^ADN,36,10 ^FDCONTRADA RAVAGNESE GRAZIELLA,8 ^FS ^FO15,243 ^ADN,108,50 ^FDE5 ^FS ^FO136,338 ^ADN,36,14 ^FDReggio Calabria ^FS ^FO580,338 ^ADN,36,15 ^FDRC ^FS ^FO632,210 ^GB176,166,4 ^FS ^FO660,220 ^AAN,18,5 ^FDCALABRIA SICILIA ^FS ^FO695,246 ^ADN,18,5 ^FDCSN ^FS ^FO632, 273 ^GB176,103,4 ^FS ^FO640,280 ^ADN,18,5 ^FDPeso Kg ^FS ^FO680,318 ^ADN,36,20 ^FD4.1 ^FS ^FO632, 372 ^GB176,48,4 ^FS ^FO640,378 ^ADN,18,5 ^FDPorto: ^FS ^FO658,398 ^ADN,18,10 ^FDFRANCO ^FS ^FO4,372 ^GB804,48,4 ^FS ^FO25,383 ^ADN,36,10 ^FDNote: ^FS ^FO88,383 ^ADN,36,10 ^FDProva Zaffignani ^FS ^FO640,650 ^XGE:GLSMINI.GRF,1,1 ^FS ^FO606,718 ^GB202,82,4 ^FS ^FO4,718 ^GB606,82,4 ^FS ^FO240,718 ^GB144,80,40 ^FS ^FO242,724 ^ADN,96,25 ^FR ^FD1-25 ^FS ^FO50,422 ^BY3 ^BCN,260,N,N,N,A ^FVWW540006975010RC ^FS ^FO50,685 ^ADN,27,15 ^FDWW 540006975 01 0 RC 01 ^FS ^XZ";
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
    echo "socket_create() failed: reason: " . socket_strerror(socket_last_error ()) . "\n";
} else {
    echo "OK.\n";
}
echo "Attempting to connect to '$host' on port '$port'...";
$result = socket_connect($socket, $host, $port);
if ($result === false) {
    echo "socket_connect() failed.\nReason: ($result) " . socket_strerror (socket_last_error($socket)) . "\n";
} else {
    echo "OK.\n";
}
socket_write($socket, $label, strlen($label));
socket_close($socket);
socket_close($socket); // Note: Double close here!

Working Python Code:

import socket
mysocket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
host = "10.80.209.106"
port = 9100
try:
    mysocket.connect((host, port)) #connecting to host
    mysocket.send(b"^XA^A0N,50,50^FO50,50^FDSocket Test^FS^XZ")#using bytes
    mysocket.close () #closing connection
except:
    print("Error with the connection")

Troubleshooting "Connection Refused" with PHP & Zebra ZD-420

Since your Python code works, the printer and network are solid—this is definitely a PHP-specific issue. Let’s walk through fixes and checks step by step:

1. Fix the Double socket_close() Call in PHP

First, spot the bug in your PHP code: you’re calling socket_close($socket); twice. The first call closes the socket, so the second one is trying to operate on an already invalid resource. While this might not be the root connection failure cause, it’s a bug that can lead to unexpected behavior once you get connected. Remove one of the socket_close lines.

2. Verify $host and $port Are Explicitly Set

In your PHP script, you’re using $host and $port variables but never defining them! Unlike your Python code where you explicitly set host = "10.80.209.106" and port = 9100, your PHP code relies on these variables being set elsewhere (maybe CodeIgniter config?). If they’re missing or incorrect, this will 100% cause a connection refusal.

Add these lines at the top of your PHP code to eliminate ambiguity:

$host = "10.80.209.106";
$port = 9100;

3. Check Web Server User Permissions & Firewall Rules

PHP runs under a web server user (like www-data for Apache, nginx for Nginx) which often has stricter network permissions than the user running your Python script.

  • Test PHP connectivity via CLI: Run a simple test script directly through PHP’s command line interface (not your web server) to rule out web server-specific restrictions:
    <?php
    $host = "10.80.209.106";
    $port = 9100;
    $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
    if ($socket) {
        $result = socket_connect($socket, $host, $port);
        if ($result) {
            echo "Connected successfully!\n";
            socket_write($socket, "^XA^A0N,50,50^FO50,50^FDPHP CLI Test^FS^XZ", strlen("^XA^A0N,50,50^FO50,50^FDPHP CLI Test^FS^XZ"));
        } else {
            echo "Connection failed: " . socket_strerror(socket_last_error($socket)) . "\n";
        }
        socket_close($socket);
    } else {
        echo "Socket create failed: " . socket_strerror(socket_last_error()) . "\n";
    }
    
    If this works, the issue is with your web server’s user permissions or environment.
  • Check local firewall/SELinux: Ensure your web server user is allowed to make outgoing connections to port 9100. On Linux, use tools like ufw or iptables to add a rule, or verify SELinux isn’t blocking the connection.

4. Confirm PHP Sockets Extension is Enabled

Run phpinfo() and look for the "sockets" section. If it’s missing, you’ll need to enable the extension by adding extension=sockets to your php.ini file (and restart your web server). Some shared hosting environments disable raw sockets by default—if you’re on shared hosting, reach out to your provider to confirm outgoing connections to port 9100 are allowed.

5. Try a Dedicated ZPL PHP Library (Alternative Approach)

If raw sockets keep giving you trouble, use a purpose-built library that handles connection management out of the box. For example, install the zebra/zpl package via Composer:

<?php
require 'vendor/autoload.php';

use Zebra\Zpl\Builder;
use Zebra\Zpl\Printer;

$printer = new Printer('10.80.209.106', 9100);
$zpl = (new Builder())
    ->addText('Rossi Andrea', 36)
    ->addText('REGGIO CALABRIA', 135)
    // Add other elements of your label here
    ->toZpl();

$printer->send($zpl);

This library abstracts low-level socket details, which can avoid common pitfalls.


内容的提问来源于stack exchange,提问作者Viorel Vornicu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 08:57:44