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
Related
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/");
?>
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.
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
I am creating subdomains dynamically through the php script. This is creating well. But when I try to open it I am getting a default cgi error page ( http://subdomain.mydomain.com/cgi-sys/defaultwebpage.cgi). But after 15-20 minutes later this is working fine. Why is this?
My subdomain creation code is below:
function subd($host, $port, $ownername, $passw, $request) {
$authstr = "cpanel_userid:cpanel_password";
$sock = fsockopen('localhost',2082);
if(!$sock) {
print('Socket error');
exit();
}
$pass = base64_encode($authstr);
$in = "GET $request\r\n";
$in .= "HTTP/1.0\r\n";
$in .= "Host:localhost\r\n";
$in .= "Authorization: Basic $pass\r\n";
$in .= "\r\n";
fputs($sock, $in);
while (!feof($sock)) {
$result .= fgets ($sock,128);
}
fclose( $sock );
return $result;
}
I'm calling this function from my code. My subdomain is creating well but this is taking a bit more time to setup properly. Where am I going wrong? My server is running Linux.
Thanks.
I presume that your web hoster needs the time to completely setup the subdomain. Looks normal to me.
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.