如何解决如何连接到 Zebra 打印机 ZD220 或 Gc420t 并打印?
我手头有一项被证明很困难的任务。过去几天我一直在关注它。
我需要将 Mercado Livre(类似于亚马逊)的货件标签从我的系统直接打印到我的 zebra 打印机。我的系统使用 PHP,我从他们的 API 中获取标签。
问题是,我这辈子都无法将计算机连接到打印机。
在我的工作中,打印机连接到通过以太网电缆连接到我的本地网络的桌面并共享。
我可以从我的笔记本电脑打印标签,所以我知道有一条路线。我如何找到它?
我的尝试
基本上,我可以在互联网上找到的所有内容。
我曾尝试使用 robgridley/zebra PHP ZPL Builder 以及我能找到的其他几个,但没有成功。
我尝试了以下代码:
当我尝试这个时,
error_reporting(E_ALL);
/* Get the port for the service. */ $port = 9100;
/* Get the IP address for the target host. */ $host = "xxx.xxx.xx.xx"; //this is the address of the computer that the printer is connected
/* 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,4 ^FS ^FO561,16 ^ADN,10 ^FDP/V ^FS ^FO611,10 ^FD1:3 ^FS ^FO693,10 ^FD18/12/14 ^FS ^FO20,66 ^GB540,135,80 ^FS ^FO21,70 ^ADN,25 ^FR ^FDREGGIO CALABRIA ^FS ^FO720,66 ^GB72,80 ^FS ^FO721,25 ^FR ^FD13 ^FS ^FO4,210 ^GB128,166,4 ^FS ^FO128,210 ^GB680,4 ^FS ^FO136,220 ^ADN,15 ^FDProva Rossi ^FS ^FO136,276 ^ADN,10 ^FDCONTRADA RAVAGNESE GRAZIELLA,8 ^FS ^FO15,243 ^ADN,108,50 ^FDE5 ^FS ^FO136,338 ^ADN,14 ^FDReggio Calabria ^FS ^FO580,15 ^FDRC ^FS ^FO632,210 ^GB176,4 ^FS ^FO660,220 ^AAN,18,5 ^FDCALABRIA SICILIA ^FS ^FO695,246 ^ADN,5 ^FDCSN ^FS ^FO632,273 ^GB176,103,4 ^FS ^FO640,280 ^ADN,5 ^FDPeso Kg ^FS ^FO680,318 ^ADN,20 ^FD4.1 ^FS ^FO632,372 ^GB176,48,378 ^ADN,5 ^FDPorto: ^FS ^FO658,398 ^ADN,10 ^FDFRANCO ^FS ^FO4,372 ^GB804,4 ^FS ^FO25,383 ^ADN,10 ^FDNote: ^FS ^FO88,10 ^FDProva Zaffignani ^FS ^FO640,650 ^XGE:GLSMINI.GRF,1,1 ^FS ^FO606,718 ^GB202,82,718 ^GB606,4 ^FS ^FO240,718 ^GB144,80,40 ^FS ^FO242,724 ^ADN,96,25 ^FR ^FD1-25 ^FS ^FO50,422 ^BY3 ^BCN,260,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); echo "<pre>"; echo "Socket: " . $socket; if ($socket === false) {
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n"; } else {
echo "\nOK. \n"; }
echo "Attempting to connect to '$host' on port '$port'..."; $result = socket_connect($socket,$host,$port);
echo $result; 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: Resource id #2
OK.
Attempting to connect to 'xxx.xxx.xx.xx' on port '9100'...
Warning: socket_connect(): unable to connect [110]: Connection timed out in /xxx/xxx/xxxxxx/xxxxxxxxxxx/xxxxxxxxxxxxxx/xxxxxxxxxxxx on line 38
socket_connect() failed.
Reason: () Connection timed out
Warning: socket_write(): unable to write to socket [32]: Broken pipe in /xxx/xxx/xxxxxx/xxxxxxxxxxx/xxxxxxxxxxxxxx/xxxxxxxxxxxx on line 47
我知道端口 445 是开放的(通过所述计算机上的 netstat)。
于是我把端口改成445,发送命令打印。
Socket: Resource id #2
OK.
Attempting to connect to 'xxx.xxx.xx.xx' on port '445'...1OK.
不幸的是,没有打印任何内容。
由于某种原因,以下代码根本不返回任何内容。
<?php
$fp = fsockopen("xxx.xxx.xx.xx",445,$errno,$errstr,30);
if (!$fp) {
echo "$errstr ($errno)
\n";
} else {
$out = "GET / HTTP/1.1\r\n";
$out .= "Host: xxx.xxx.xx.xx\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp,$out);
while (!feof($fp)) {
echo fgets($fp,128);
}
fclose($fp);
}
echo "</pre>";
我尝试了很多其他代码,但基本上都是一样的(socket_create 和 fsockopen)。
我已经尝试使用我的打印机名称执行 exec:
exec("lpr -P '\\xxx.xxx.xx.xx\ZDesigner ZD220-203dpi ZPL' -r 'README.txt');
和
exec("lp -d '\\xxx.xxx.xx.xx\ZDesigner ZD220-203dpi ZPL' -r 'README.txt');
在我写这篇文章的时候,我感到很大胆,用这段代码测试了从 1 到 20000 的 20000 个端口:
<?php
$host = 'xxx.xxx.xx.xx';
for($i = 1; $i <= 20000; $i++) {
$connection = @fsockopen($host,$i);
if (is_resource($connection)) {
echo '<h2>' . $host . ':' . $i . ' ' . '(' . getservbyport($i,'tcp') . ') is open.</h2>' . "\n";
fclose($connection);
} else {
echo '<h2>' . $host . ':' . $i . ' is not responding.</h2>' . "\n";
}
}
这是一个愚蠢的举动,因为它花费了很多时间并返回了我已经知道的东西。有些端口是开放的,但 9100 不是。
我在防火墙上打开了端口 23(用于 telnet)、515 和 9100,这并没有让我在任何地方,它仍然说连接超时。当我在桌面上完全停用 Windows 防火墙时,它说连接被拒绝。
当我尝试通过打印机的设置向打印机发送命令时,它给了我 CM:OpenPrinter Access Denied。
Zebra 还有一个设置实用程序工具,它没有给我这个错误,但今天它是:
另外,当我发送 ~WL 时,它说没有安装网络,这很好,我知道,但有一些方法,因为否则我将无法打印任何东西。
我需要什么:
有什么办法可以通过 PHP 访问打印机吗?
我真的需要它来工作。感谢您花时间阅读本文。
我已经尽力提出这个问题了,希望不要让我感到困惑。
解决方法
我们使用 fputs($fp,$out)
而不是 fwrite($fp,$out)
,$out
是字符串中的一些 EZPL/ZPL 代码。
$fp = fsockopen($printer['ip'],$printer['port'],$errno,$errstr,10);
if (!$fp) {
// code if no connection found
} else {
$out = "some EZPL code as string";
fputs($fp,$out);
fclose($fp);
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。