Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP打印到远程打印机_Php_Sockets_Printing - Fatal编程技术网

PHP打印到远程打印机

PHP打印到远程打印机,php,sockets,printing,Php,Sockets,Printing,我有一台远程POS打印机,它使用ESC/POS协议。 我有一个PHP应用程序正在运行,可以打印消息: $texttoprint = "Samuel is a legend \nHello. \nTest123"; $texttoprint = stripslashes($texttoprint); $fp = fsockopen("10.1.1.100", 9100, $errno, $errstr, 10); if (!$fp) { debug

我有一台远程POS打印机,它使用ESC/POS协议。 我有一个PHP应用程序正在运行,可以打印消息:

    $texttoprint = "Samuel is a legend \nHello. \nTest123";
    $texttoprint = stripslashes($texttoprint);

    $fp = fsockopen("10.1.1.100", 9100, $errno, $errstr, 10);
    if (!$fp) {
        debug("$errstr ($errno)<br />\n");
    } else {
        fwrite($fp, "\033\100");
        $out = $texttoprint . "\r\n";
        fwrite($fp, $out);
        fwrite($fp, "\012\012\012\012\012\012\012\012\012\033\151\010\004\001");
        fclose($fp);
    }
$texttoprint=“塞缪尔是一个传奇人物\nHello.\nTest123”;
$texttoprint=stripslashes($texttoprint);
$fp=fsockopen(“10.1.1.100”,9100,$errno,$errstr,10);
如果(!$fp){
调试($errstr($errno)
\n); }否则{ fwrite($fp,“\033\100”); $out=$texttoprint。“\r\n”; fwrite($fp,$out); fwrite($fp,“\012\012\012\012\012\012\012\012\012\033\151\010\004\001”); fclose($fp); }
它工作,但只有有时,如果我运行的代码,它的工作约1在10倍,但它没有击中错误,所以我不知道为什么它只是打印有时

仅使用一个fwrite()修复了此问题。 谢谢@user113215。
如果有人知道我为什么想知道。

首先,您忽略了返回值。另外,为什么不只使用一个写调用?@user113215我已经检查过它是否返回每个fwrite()的正确字节数。@user113215只有一个fwrite()修复了它。不知道为什么。如果你把它作为一个答案,我会很乐意接受它。如果不是,我会自己回答的。奇怪!这只是我的一个风格建议:——)