mysterious timeout error on VPS - php

So, the problem looks quite trivial - error "Connection timed out" in php function fsockopen().
BUT, I have feeling that I've tried almost everything on earth to solve this :(
Details:
Sever is dedicated on Hetzner, Debian 7, apache2, nginx, nothing special. When i try to use function with IP instead of site name, it works. Futhermore, ping and telnet works fine, php function gethostbyname works too. I tried to check resolv.conf, it looks normal. I tried to flush Iptables settings, didnt help.
"allow_url_open" - on
"Registered PHP Streams" - https, ftps, compress.zlib, compress.bzip2, php, file, glob, data, http, ftp, phar, zip
"Registered Stream Socket Transports" - tcp, udp, unix, udg, ssl, sslv3, tls.
I cant use cUrl or something else instead of fsockopen() because it's built-in module of site's CMS.
I check it with simple code
$fp = fsockopen("www.google.com", 80, $errno, $errstr, 10); // work fine
if(! $fp) echo "www.google.com - $errstr ($errno)<br>\n";
else echo "www.google.com - ok<br>\n";
it doesn.t work. But if I use google's IP instead of domain:
$fp = fsockopen("93.158.134.11", 80, $errno, $errstr, 10); // work fine
if(! $fp)
echo "93.158.134.11 - $errstr ($errno)<br>\n";
else
echo "93.158.134.11 - ok<br>\n";
It works.
On my another similarly configured VPS it says "ok" in both cases.

Related

I'm getting "Unable to find the socket transport 'http'" error suddenly. It was working fine until we switched networks

After a network switch, parts of my program that send data to other servers are no longer working.
I tried the following code:
<?php
fsockopen("www.php.net", 80, &$errno, &$errstr, 30);
if(!$fp) {
echo "Error: $errstr ($errno)<br>\n";
} else {
fputs($fp,"GET / HTTP/1.0\n\n");
while(!feof($fp)) {
echo fgets($fp,128);
}
fclose($fp);
}
?>
After running that code, I am presented with the following error:
Unable to find the socket transport "http" - did you forget to enable it when you configured PHP? (19)
What do I need to check to ensure this works? It's baffling because it was working fine just before switching networks. I'm also getting the "php_network_getaddresses: getaddrinfo" error when I try get_file_contents.
Did you try opening the socket without the protocol part, e.g. just
fsockopen("www.php.net", 80, &$errno, &$errstr, 30);
I found the answer by doing a google search for
Unable to find the socket transport "http"
The same answer is in all of the top 5 results, so it would have saved you 3 days to just spend 5 seconds copying and pasting the error in to google.

how to use php sockets to write string

So Im trying to get my head wrapped around this....
I open the port
$remip = $_SERVER['SERVER_ADDR']; //Grab my server address
$fp = fsockopen($remip, 80, $errno, $errstr, 10);//Godaddy hosting only 80 and 443 ports work
//fsockopen(ip address , port, IDK, IDK, timeout delay)
so now the ports open or if not maybe some error checking to be sure
if (!$fp) { echo "$errstr ($errno)<br>\n"; exit; } //Not sure what this echos out but its clear how it stops errors
So now that the port is open any ip/client can connect on this port????
Ill assume I can now connect....
So on my client I open a socket to my server ip address port tcp connection.....
The php file includes something like
else {$out = "hello, 80\r\n"; //out specifies the string to be written , bytes to write
fwrite($fp, $out); //$fp is the handle
fclose($fp)}//close the connection
at this point ill assume that my client gets the hello written to it ..
finish up by closing the connection
Im entirely new to this so Im attempting to understand some sample code here...
So how long is this socket open for? If i want to keep this port open do i need to do a cron job to launch this file periodically.
Im 100% sure that I have got something wrong here so please set me straight.
I think you have a misconception of what fsockopen does. In your example your fsockopen does not actually open port 80 (as in opening a server socket), but it opens a client socket that connects to port 80 on the server itself. It actually does open a (client) port which gets a (not completely) random number.
After you connected using fsockopen you can send HTTP commands to the webserver such as GET /index.php
What you need to use is socket_listen() and socket_bind(). There are a few places in the docs that show you how to get PHP listening on a socket: http://www.php.net/manual/en/function.socket-listen.php
I suggest you read and try them out by simply testing then with a unix tool called netcat (nc <ip_address> <port> command normally)

Fetch file from https only server

I have a small script which uses curl and retrives specific contents from a defined url. I had this tested on my localhost and it worked.
Now I have to retrive data from a HTTPS-only website (plus, the certificate is invalid, but I know the owners) from my free hosting, but the myserver neither supports CURL nor file_get_contents("https://other-server.com") function. By the way, http://other-server.com isn't accesible.
Is there any method to fetch a file from this server using the HTTPS port, but with HTTP protocol? Or is there some method to use HTTPS, altough my server doesn't support it? (It isn't my server, I haven't access to its configuration)
Try this:
<?php
$fp = fsockopen("ssl://other-server.com", 443, $errno, $errstr);
if(!$fp) die($errno. " : " . $errstr);
$send =
"GET / HTTP/1.0\r\n".
"Host:other-server.com\r\n".
"Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n".
"\r\n";
fwrite($fp, $send);
while(!feof($fp)) {
echo fread($fp, 512);
}
?>
Should you run into 'ssl transport not available error message', see Socket transport "ssl" in PHP not enabled
If your host is external and perhaps a free webhosting service, you are fresh out of luck.. Best option would be to figure out which webhosts has the SSL transport enabled - otherwise the working with HTTPS protocol simply will not comply.
Your last 'out' is to try to load extension into PHP language dynamically. You will need the excact extension (dll/so) which matches
the PHP version on host (see phpinfo).
the CPU architechture of host (unix, see passthru("cat /proc/cpuinfo");), e.g. amd64,i386..
the OS 'layout', .dll is for a windows host (IIS etc) and .so for UNIX.
Funcition to use is dl aka dynamic-link to load the library. For windows host, you will need php_openssl.dll and php_sockets.dll - and in turn for UNIX, OOops - you would need to recompile php core..
Happy hacking :)
php-man-pages

fsockopen, can't get a foot in! Connection timeout

Excuse my terrible pun.
Basically, the deal is, the host is being a bitch. A simple fsockopen script, no matter what port, server, or anything, will connect. Trying to get server help from the webhost (due to unfortunate circumstances I have no way of changing hosts) proved ineffective... bunch of morons. A simple phpinfo() shows, however, that allow_url_fopen is on and Registered Stream Socket Transports allows me tcp, udp, unix, udg, ssl, sslv3, sslv2, tls.
So, what am I missing? Does it sound like the host is discreetly blocking socket connections? I mean, even port 80 doesn't work:
Warning: fsockopen() [function.fsockopen]: unable to connect to www.google.com:80 (Connection timed out)
Provided you are calling fsockopen correctly (you don't show code), that error is implying that yes, they are restricting you from making outbound connections.
$fp = fsockopen("www.google.com", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
}
If you have shell access, try using telnet and you should see the same result:
telnet www.google.com 80

Port checking from php

I'm trying to connect to gmail pop server from a phplist installation and it fails, but i'm not sure whether my webhost opened port 995 or not. They say they have opened it, but i'm in doubt. Is there a way i can check it from a php script? They are running php 5.2.0 on a windows server, though i'm not sure what OS is that. phpinfo() says "Windows NT DEDI514 5.2 build 3790"
You can put code in a php script to open a connection to a specific hostname (or IP address) and port.
If you know the expected response, you should be able to tell if you are getting a connection. If you get something like "Connection refused", then either you are being blocked, or the destination host is not accepting connections on that port.
This example uses IP address 192.0.2.0 and port 995. Replace these with whatever you want to test.
<?php
echo "\nOpening connection\n\n";
$fp = fsockopen("192.0.2.0", 995, $errno, $errstr);
if (!$fp) {
echo "ERROR: $errno - $errstr\n";
} else {
echo fread($fp, 1024);
fclose($fp);
}
?>
You can also send data to the server using
fwrite($fp, "blah blah blah\r\n");
There is more information about fsockopen here.
I think you'll need to ping or traceroute to a machine that will respond on that port.
This article should have much more than you want to know, but there's an example script at the bottom that you can modify to test.
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?lngWId=8&txtCodeId=1786
There are some other scripts here:
http://www.theworldsend.net/
I can't vouch for any of these personally, but they look like what you need.
And, of course, if you can ssh or telnet into your server, you can do all this much more easily using the ping and traceroute commands.
Maybe safe mode is active? This prevents calling services on other servers.
Edit:
All filesystem and stream functions are affected by the safe mode settings!
The open_basedir setting affects fopen()!

Categories