PHP fsockopen subfolder - php

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/");
?>

Related

Cannot write to file I created myself

I have this piece of code:
$fp = fopen("/path/to/file", "a+");
if (!$fp) {
fwrite($fp, "somedata\n");
fclose($fp);
} else {
echo "cannot open";
}
and the result is an empty file and the message cannot open. I have SELinux disabled and the permissions seem OK, otherwise the file wouldn't be created, I'd say.
Any ideas?
Obvious mistake:
if (!$fp) {
should be
if ($fp) {

PHP Printing to Remote Printer

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.

Can not open serial port using PHP

I am working on to send request to VSP200 device, my device is connected to com port8 of windows machine. I am using fopen() of PHP to open the com port, but I am getting an error
Warning: fopen(COM8:) [function.fopen]: failed to open stream
can you please tell me, what is wrong in my code,
$fp = fopen ("COM8:", "w+");
if (!$fp) {
echo 'not open';
}
else{
echo 'port is open for write<br/>';
$string .= '<STX>C30C10178C10100C103110606C103081000C10100C10101C100<ETX>';
fputs ($fp, $string );
echo $string;
fclose ($fp);
}
$fp = fopen ("COM8:", "r+");
if (!$fp) {
echo 'not open for read';
}
else{
echo '<br/> port is open for read<br/>';
$buffer = fread($fp, 128 );
echo $buffer;
fclose ($fp);
}
You should not include the trailing colon in the port name:
$fp = fopen ("COM8", "w+");

Long delay between stream_socket_server and stream_socket_client on the same PC

When I run the two files below through the command line, (first start socket_server, then socket_client) there is a long delay (~60s) before any output is sent to socket_client by the server. Is there a way to reduce this gap, or any hints as to what is causing the problem? Here are my two code snippets:
socket_client.php:
<?php
$fp = stream_socket_client("tcp://127.0.0.1:8000", $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
}
else {
fwrite($fp, "2");
echo fgets($fp, 1024);
}
fclose($fp);
?>
socket_server.php:
<?php
$socket = stream_socket_server("tcp://127.0.0.1:8000", $errno, $errstr);
if (!$socket) {
echo "$errstr ($errno)<br />\n";
} else {
while ($conn = stream_socket_accept($socket)) {
while (!feof($conn)) {
$result = fgets($conn, 1024);
if($result = "2"){
fwrite($conn, "Hullo there");
}
else{
fwrite($conn, "Hullo here\n");
}
}
fwrite($conn, 'The local time is ' . date('n/j/Y g:i a') . "\n");
fclose($conn);
}
fclose($socket);
}
?>
You were forgetting to send \n at the end of some of the fwrite calls. The reason this was causing a problem is because fgets is looking for the newline before it returns.
I removed the feof loop from the server because the client is only sending one line.
I added the feof loop in the client to handle the multiple lines sent from the server.
I changed if($result = into if ($result == because == is a comparison operator (which is what you actually wanted). Inside an if statement you almost always want to use == instead of =.
socket_client.php:
<?php
$fp = stream_socket_client("tcp://127.0.0.1:8000", $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
fwrite($fp, "2\n");
while (!feof($fp)) {
echo fgets($fp, 1024);
}
}
fclose($fp);
?>
socket_server.php:
<?php
$socket = stream_socket_server("tcp://127.0.0.1:8000", $errno, $errstr);
if (!$socket) {
echo "$errstr ($errno)<br />\n";
} else {
while ($conn = stream_socket_accept($socket)) {
$result = fgets($conn, 1024);
if ($result == "2\n") {
fwrite($conn, "Hullo there\n");
} else {
fwrite($conn, "Hullo here\n");
}
fwrite($conn, 'The local time is ' . date('n/j/Y g:i a') . "\n");
fclose($conn);
}
fclose($socket);
}
?>

Get page from external server

I want to pass a function a string, which takes that string tacks it onto url. Then goes to that url and then returns the page to my server so I can manipulate it with JS.
Any Ideas would be much appreciated.
cheers.
If your fopen_wrappers are enabled, you can use file_get_contents() to retrieve the page, and then insert JavaScript into the content before echoing it as output.
$content = file_get_contents('http://example.com/page.html');
if( $content !== FALSE ) {
// add your JS into $content
echo $content;
}
This of course won't affect the original page.
You should be able to use fopen() for what you want. It can accept URLs.
echo "<script type='text/javascript' src='myjavascript.js'></script>";
$handle = #fopen("http://www.example.com/", "r");
if ($handle) {
while (!feof($handle)) {
$buffer = fgets($handle, 4096);
echo $buffer;
}
fclose($handle);
}
Using CURL would probably be easiest but I prefer to do stuff myself. This will connect to a given address and return the contents of the page. It will also return the headers, though, so watch out for that:
function do_request ($host, $path, $data, $request, $specialHeaders=null, $type="application/x-www-form-urlencoded", $protocol="", $port="80")
{
$contentlen = strlen($data);
$req = "$request $path HTTP/1.1\r\nHost: $host\r\nContent-Type: $type\r\nContent-Length: $contentlen\r\n";
if (is_array($specialHeaders))
{
foreach($specialHeaders as $header)
{
$req.=$header;
}
}
$req.="Connection: close\r\n\r\n";
if ($data != null) {
$req.=$data;
}
$fp = fsockopen($protocol.$host, $port, $errno, $errstr);
if (!$fp) {
throw new Exception($errstr);
}
fputs($fp, $req);
$buf = "";
if (!feof($fp)) {
$buf = #fgets($fp);
}
return $buf;
}

Categories