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
Related
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.
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)
I'm testing paypal ipn for a website I am building, but it fails when I try to validate the ipn. In particular this line doesn't work:
$fp = fsockopen('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);
the check if (!$fp) fails (fp is false). I assume it can't connect. Why this?
Update
I discovered my hosting solution doesn't support ssl, in fact I get:
Warning: fsockopen() [function.fsockopen]: unable to connect to
ssl://www.sandbox.paypal.com:443 (Unable to find the socket transport
"ssl" - did you forget to enable it when you configured PHP?)
Is there a way to get over this without going for another hosting solution?
You don't have to use SSL (though Paypal highly recommends it.)
$fp = fsockopen('www.paypal.com', 80, $errno, $errstr, 30);
First off, I'm a complete novice as a web developer. I have a PHP function that handles a post request for HTTP, and it works great. I read a few places online that all I have to do to make that same function post to HTTPS is change the port I'm hitting from port 80 to port 443. So instead of looking like this:
$fp = fsockopen($host, 80, $errno, $errstr, 30);
It would look like this:
$fp = fsockopen($host, 443, $errno, $errstr, 30);
Unfortunately, this change doesn't seem to be working. So my questions are these:
Is it true that all I have to change is the port number?
If there is more to do, than what is it I still need to do?
Please try to keep things in as simple terms as possible, since I am the first to admit I'm very new to this kind of stuff.
Thanks a ton everyone.
Is it true that all I have to change is the port number?
No
If there is more to do, than what is it I still need to do?
You have to negotiate an SSL connection and tunnel the HTTP request through it.
Don't try to do this with sockets. Use a library designed for it, such as cURL.
From php.net:
If OpenSSL support is installed, you may prefix the hostname with either ssl:// or tls:// to use an SSL or TLS client connection over TCP/IP to connect to the remote host.
Try prepending ssl:// to your $host (but also keeping port 443;
Warning: fsockopen()
[function.fsockopen]: unable to
connect to www.paypal.com:443
(Connection timed out)
This has worked previously? And suddenly has stopped working.
Any ideas?
This is the call:
$fp = fsockopen ('www.paypal.com', 443, $errno, $errstr, 30);
Thanks for any help
According to the error your system is providing, it's timing out on the outbound connection, are you currently able to connect to www.paypal.com on port 443 though something like telnet?
Running something like: telnet www.paypal.com 443 from the local box should show if it's connecting. I strongly suspect that something is blocking port 443 outbound, which is causing your issue, as it's unlikely that PayPal would block you for use of their IPN services.