Check whether outbound port on server is open - php

I am trying to see whether my port 2195 (apples port) is open for outbound executions. I know that it is closed for inbound. I want to be able to check using php so I wrote this code:
$fp = fsockopen("example.co.uk", 2195, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
fclose($fp);
}
Which returns the error:
Warning: fsockopen(): unable to connect to example.co.uk:2195
(Connection refused) in /home3/.../public_html/.../example.php
on line 13 Connection refused (111)
But as I said I think this is due to the inbound port being closed.
So is there another way to test just the outbound?

Related

How can I receive data from GPS device which is sending data to my server ip and port using PHP?

I have a GPS device which is sending data to my server 103.209.146.80 and port 5672
I want to create a php script or a JS script which reads the incoming data on this port.
My first step is to catch the data. I am using this PHP code but it is giving error:
<?php
$fp = fsockopen("103.209.146.80", 5672, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
fwrite($fp, "You message");
while (!feof($fp)) {
echo fgets($fp, 128);
}
fclose($fp);
}
?>
You must first know what type of connection tcp or udp transmits your GPS, then create a socket server (tcp / udp) that listens on the port where you will transmit your GPS.
Do not forget that you must configure a valid APN in your GPS.

Check if anything is listening in a port in PHP

I want to use PHP to check if anything is listening to localhost:81, to ensure that is available for a PHP built in server to run on.
i.e. I want to check if the following would run properly php -S localhost:81.
Now if something is already listening on port 81 (e.g. Apache), this will of course cause problems.
I read the following: How can I check if ports 465 and 587 are open with PHP? and the solution did not seem to work.
i.e.:
$fp = fsockopen('localhost', '81', $errno, $errstr, 5);
var_dump($fp); // returns false
if (!$fp) {
// port is closed or blocked
echo "CLOSED!";
return false;
} else {
// port is open and available
echo "OPEN!";
fclose($fp);
return true;
}
Unfortunately the above keeps returning "CLOSED!" even though its not.
I also get the following error message:
PHP Warning: fsockopen(): unable to connect to localhost:81 (Connection refused)
How do I solve my problem?
Is there any alternatives?
You should actually check to see if fsockopen() has returned a resource:
$connection = #fsockopen('localhost', '81');
if (is_resource($connection))
{
echo 'Open!';
fclose($connection);
return true;
}
else
{
echo 'Closed / not responding. :(';
return false;
}

php socket programming with auth

i want to read file from remote server with Socket programing but server protected with http_auth
and i could not get socket working even with providing tcp://username:passwrod#hostname.com
i got this error
php_network_getaddresses: getaddrinfo failed: No such host is known. (0)
here is my code
$fp = stream_socket_client(" tcp://username:passwrod#hostname.com:80", $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
fwrite($fp, "GET / HTTP/1.0\r\nHost: hostname.com\r\nAccept: */*\r\n\r\n");
while (!feof($fp)) {
echo fgets($fp, 1024);
}
fclose($fp);
}
Well, for one, you have a typo in the TCP string:
" tcp://username:passwrod#hostname.com:80"
should be
"tcp://username:password#hostname.com:80"
By itself, the extra space at the start of your URI will cause yourstream_socket_client call to fail, and I imagine it's responsible for the getaddrinfo error as the host cannot be resolved if the URI string is invalid. Furthermore, even once the DNS issue is resolved, the authentication will fail because password is misspelled as passwrod.
Assuming it still doesn't work after that, you should have a look at your DNS settings. getaddrinfo failed: No such host is known means that your server cannot figure out where hostname.com is located... so before you start worrying about authentication or ports, just make sure that
hostname.com is a valid URL
the DNS service is configured correctly on your server (try to run a few simple ones that should definitely work, like $fp = stream_socket_client("tcp://yahoo.com:80", $errno, $errstr, 30); var_dump($fp); - if that call doesn't return a resource then your DNS service is not functioning correctly... or you could just try pinging from the server to see if you can reach www.yahoo.com.
Check your server's proxy settings
Try this:
<?php
if(!($fp = fsockopen("hostname", 80)))
{
throw new Exception("Couldn't open socket!\n");
}
fputs($fp, "GET / HTTP/1.0\r\n");
fputs($fp, "Host: hostname\r\n");
fputs($fp, "Authorization: Basic " .
base64_encode("username:password") .
"\r\n");
fputs($fp, "\r\n");
fpassthru($fp);
?>

I can't use Paypal IPN on my Hosting, how to solve this?

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);

PHP fwrite() to socket - operation not permitted?

I'm making a simple utility in PHP to control my Minecraft server via UDP:
$fp = fsockopen('udp://host', 'port', $errno, $errstr);
if (!$fp)
error("Unable to connect!");
else {
fwrite($fp, $data['command'].':user:pass');
stream_set_timeout($fp, 5);
error(fread($fp, 128));
fclose($fp);
}
For some reason fwrite is throwing this error:
Notice: fwrite() [function.fwrite]: send of 20 bytes failed with errno=1 Operation not permitted in /homepages/44/d217581656/htdocs/xenforo/util/remoterestart/interface.php on line 22
Anyone know why?
I'm going to go out on a limb and say you're probably not permitted to use sockets on your server. This is a fairly common thing to disable on most hosts.
Run a phpinfo() and see if there is any socket functions which are disabled, or contact your host and just ask.

Categories