PHP Printing to Remote Printer - php

I have a remote POS printer it uses the ESC/POS protocol.
I have a PHP app running that prints messages:
$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);
}
It works but only sometimes, if i run the code it works around 1 in 10 times but it is not hitting the error so I don't know why its only printing sometimes.

Using only one fwrite() fixed this issue.
Thank you #user113215.
If anyone knows why I would love to know.

Related

How to force/ close a socket connection in php

I have an issue with a project, I'm connecting to a remote server using telnet , i'm able to retrieve the console text and store it in a array and the to a database, all that fine, the problem is the telnet server keeps the session opened for 1:30 minutes so i get the output until 1:30 minutes, i want to retrieve the console text and force the php telnet connection but i don't know how, i tried everything and the sessions keeps opened.
Any help would be appreciated
<?php
$ip= $argv[1];
$da = fsockopen($ip, 23, $errno, $errstr, $timeout=3);
if (!$da){
echo "$errstr ($errno)<br />\n";
} else {
$output = "\data\r\n";
fwrite($da, $output);
while (!feof($da))
{
$str = fgets($da, 1024);
$a = explode(" ", $str);
$myJSON = json_encode($a,JSON_FORCE_OBJECT);
print var_dump($a);
print_r($a);
}
stream_set_timeout($da, 1);
fclose($da);
}
?>
Thanks

PHP fsockopen subfolder

I have a page on my website that have a really long execution time, i solved the problem of the fact that it was too slow by flushing contents while writing, but now i need to load it from another website, if i had to do it from my website a include would have been enough, but since i need to load it from another website, i thought of using file_get_contents, the problem is that in this way the flushs don't work, and the page take a lot of time to load, i heard that the solution was using fsockopen and fread, so i tried to use some examples, and i get a thing like this
<?php
ob_start();
$fp = fsockopen("www.mysite.com", 80);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
fwrite($fp, "Data sent by socket");
$content = "";
while (!feof($fp)) {
$content .= fread($fp, 1024);
ob_flush();
}
fclose($fp);
echo $content;
}
?>
The problem is that if i do this it work, but when i try something like
<?php
ob_start();
$fp = fsockopen("www.mysite.com/subfolder/index.php", 80);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
fwrite($fp, "Data sent by socket");
$content = "";
while (!feof($fp)) {
$content .= fread($fp, 1024);
ob_flush();
}
fclose($fp);
echo $content;
}
?>
It says me something like:
Warning: fsockopen(): php_network_getaddresses: getaddrinfo failed: No such host is known. on line 2
What am I mistaking?
UPDATE: Solved by myself: the correct code is
<?php
function HTTP_Post($URL) {
ob_start();
$URL_Info=parse_url($URL);
$fp = fsockopen($URL_Info["host"],80);
fwrite($fp, "GET ".$URL_Info["path"]." HTTP/1.0\r\n" );
fwrite($fp, "Host: ".$URL_Info["host"]."\r\n");
fwrite($fp, "Connection: Close\r\n\r\n");
while(!feof($fp)) {
echo fgets($fp, 1024);
ob_flush();
flush();
}
fclose($fp);
}
ini_set('max_execution_time', 300);
HTTP_Post("http://www.corriere.it/cronache/");
?>

file_get_contents doesn't work but fsockopen works

I've installed php 5.3.14 on Ubuntu Desktop 12.04.
with:
allow_url_fopen = 1
Doesn't work bellow:
<?php
echo file_get_contents('http://www.example.com');
Works bellow:
<?php
$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$out = "GET / HTTP/1.1\r\n";
$out .= "Host: www.example.com\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
while (!feof($fp)) {
echo fgets($fp, 128);
}
fclose($fp);
}
Even curl_exec() works.
I've also tried like this with Python, Python could fetch www contents.
I'm not using Firewall, Proxy.
But no problem with local network.
(192.168.1.36 is my local server machine.)
<?php
echo file_get_contents('http://192.168.1.36);
Is any configuration or installation problems?
Thanks.
One of the answers are:
You need to also check in PHP.ini file
extension = php_openssl.dll
If it is enable or not, if not then just enable that by removing ";" sign
This is a simular question:
PHP file_get_contents does not work on localhost
Check the answer and then it should work

Reading data from fsockopen using fgets/fread hangs

Here is the code that I am using:
if (!($fp = fsockopen('ssl://imap.gmail.com', '993', $errno, $errstr, 15)))
echo "Could not connect to host";
$server_response = fread($fp, 256);
echo $server_response;
fwrite($fp, "C01 CAPABILITY"."\r\n");
while (!feof($fp)) {
echo fgets($fp, 256);
}
I get the first response:
OK Gimap ready for requests from xx.xx.xx.xx v3if9968808ibd.15
but then the page times out. I have searched through stream_set_blocking, stream_set_timeout, stream_select, fread, etc. but could not get it to work. I need to read all the data that the server sends and then proceed with other commands (I would be retrieving emails using imap).
Thanks
Your script is hanging in the while loop at the end. This is because you have used !feof() as the condition for the loop, and the server is not closing the connection. This means the feof() will always return false and the loop will continue forever.
This will not be problem when your write a full implementation, as you will be looking for response codes and can break out of the loop accordingly, for example:
<?php
// Open a socket
if (!($fp = fsockopen('ssl://imap.gmail.com', 993, $errno, $errstr, 15))) {
die("Could not connect to host");
}
// Set timout to 1 second
if (!stream_set_timeout($fp, 1)) die("Could not set timeout");
// Fetch first line of response and echo it
echo fgets($fp);
// Send data to server
echo "Writing data...";
fwrite($fp, "C01 CAPABILITY\r\n");
echo " Done\r\n";
// Keep fetching lines until response code is correct
while ($line = fgets($fp)) {
echo $line;
$line = preg_split('/\s+/', $line, 0, PREG_SPLIT_NO_EMPTY);
$code = $line[0];
if (strtoupper($code) == 'C01') {
break;
}
}
echo "I've finished!";
Your script should be working. In fact, it is working.
See the results below on my pc when I ran your code:
* OK Gimap ready for requests from xx.xx.xx.xx l5if4585958ebb.20
* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 XYZZY SASL-IR AUTH=XOAUTH
C01 OK Thats all she wrote! l5if4585958ebb.20
Since gmail doesn't disconnect you. No end of file occurs. And the page loading simply times out.
In other words: Your script will just keep waiting and waiting until gmail does disconnect, which unfortunately happens after your page load has already timed out.

file_get_contents to do a http request on the same domain

I'm trying to use file_get_contents to retrieve the output a browser would receive from another file on the same domain.
I've moved to another server and now it always gets a timeout.
Below is a sample of what I'm trying to do.
index.php
<?php
echo file_get_contents('http://'.$_SERVER['SERVER_NAME'].'/sample.php');
?>
sample.php
<?php
echo 'test';
?>
Any ideas what might be the cause of this problem?
EDIT
Our server manager mentioned something about apache not responding to localhost, does that perhaps ring a bell?
Thank you
Are you sure the URL is actually correct? Have you tried using $_SERVER ['HTTP_HOST'] instead? On the machine that runs PHP, what does the host from the generated URL resolve to? Has your web server (Apache?) been set up listen on the localhost interface?
You can use fsockopen to do the same , along with you can specify the timeout
<?php
$fp = fsockopen($_SERVER['SERVER_NAME'], 80, $errno, $errstr, 30/*timeout*/);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$out = "GET /sample.php HTTP/1.1\r\n";
$out .= "Host: www.example.com\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
while (!feof($fp)) {
echo fgets($fp, 128);
}
fclose($fp);
}
?>
Check documentation for more details
http://php.net/manual/en/function.fsockopen.php

Categories