file_get_contents to do a http request on the same domain - php

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

Related

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

Using UDP fsockopen to get info from file on gameserver

I got a file on a gameserver called "current_map.tmp".
This file contains a number depending on the current map.
What I need is to read that number.
This is what I got so far:
<?php
$server_ip = '213.239.207.85';
$server_port = 27960;
$server_timeout = 2;
$server_addr = "udp://" . $server_ip;
$fp = fsockopen($server_addr, $server_port, $errno, $errstr, $server_timeout);
socket_set_timeout ($fp, $server_timeout);
if (!$fp) {
echo "ERROR: $errno - $errstr<br />\n";
} else {
$File = "current_map.tmp";
$filesize = filesize($File);
$handle = fopen($File, "r");
$map_id = fread($handle, $filesize);
fclose($handle);
}
fclose($fp);
?>
$fp returns "Resource id #2".
So that works.
Then there is nothing.
1) How do I know wich folder I connected to with $fp?
2) How can I read the content of this file?
$fp returns "Resource id #2". So that works.
No; this doesn't actually mean anything! Since UDP sockets are connectionless, there is no such thing as a UDP "connection"; calling fsockopen() only initializes sockets to prepare to send packets.
In any case, sending and receiving UDP packets does not allow you to access files on a remote server, unless that server has implemented a protocol to allow you to do so, and you are making use of that protocol. It certainly will not allow you to use fopen() to access remote files — this code is essentially just nonsense.

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.

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

Problem with fwrite php

I am getting problem while using fwrite in php. the following code works in my local computer but gives error in server.
$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
if(!$fp) {
echo 'Error: '.$errno.', '.$errstr;
} else {
fwrite($fp, 'kool');
}
There is no error with fsockopen. it passes and gives no error. fwrite is not being able to write. it fails and returns no error only false
This is a permissions issue with the Apache/Nobody user accessing a remote file that it doesn't have permission to modify/read/write/execute.
You should also print the error message(s) for debugging
$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
if(!$fp) {
echo "Error No: ".$errno."<br />\n";
echo "Error Str: ".$errstr."<br />\n";
} else {
fwrite($fp, 'kool');
}
If you're on a shared host, most likely your server does not allow outbound connections on port 80. Usually only inbound connections are allowed.

Categories