socket connection fails in php script while it works in terminal - php

That is the script I have
<?php
$timeout = 10;
$target = "tls://testbed-epp.nominet.org.uk:700";
$result = stream_socket_client($target, $errno, $errstr, 30, STREAM_CLIENT_CONNECT);
if ($result === False) {
throw new Exception("Error connecting to $target: $errstr (code $errno)");
}
echo "Connected";
And it throws an exception
Error connecting to tls://testbed-epp.nominet.org.uk:700: (code 0)
There is also a warning
WARNING: stream_socket_client(): Failed to enable crypto
At the same time running
openssl s_client -connect testbed-epp.nominet.org.uk:700
in a terminal connects flawlessly.
Any ideas will be appreciated

Try this instead:
$result = stream_socket_client("testbed-epp.nominet.org.uk:700", $errno, $errstr);
EDIT: also you can setup secure connection via stream_socket_enable_crypto() function, but you should note that it must be used AFTER initialization of socket connection.

Well, i tested your code on my apache server, and it is working fine. Can you check your apache configs. In the configs there is a parameter called "Registered Stream Socket Transports ". Just check if tls exists as a value over there, else there is some other problem, but it definitely isn't your script

$timeout =- 10;
^----
You're setting $timeout to be negative 10 seconds. e.g. you're killing the connection attempt before it can EVER get started.

Related

PHP ftp_put returning "Unable to build data connection: Connection refused"

I have a PC that is running some FTP via PHP that I know used to work 1-2 months ago, but now I return to it I find that the PC is no longer working. I know I have been using the PC but I cannot think of what might have changed.
The PHP is throwing out error messages reading
Unable to build data connection: Connection refused
...when I use the ftp_put() function.
The cut down code I am using is:
<?php
$trackErrors = ini_get('track_errors');
ini_set('track_errors', 1);
$server="***.***.***.***";
$port=21;
echo "<LI>Connecting to $server:$port<BR>";
$conn_id = ftp_connect($server,$port,9999999) or die("<BR>Unable to connect to ".$server.":$port server.");
if ( !$conn_id ) {
$errmsg = $php_errormsg;
echo "<BR><LI>ERR:$errmsg";
}
else {
$passive=false;
echo "<LI>Setting Passive Mode=$passive";
ftp_pasv($conn_id, $passive);
$user="*********";
$pass="*********";
echo "<LI>Connecting as $user/*****";
if (!ftp_login($conn_id, $user, $pass)) {
$msg = "Failed to login to $selected_server as $user; <BR>check logincredentials in the Settings";
echo "<BR><LI>$msg";
$errmsg = $php_errormsg;
echo "<LI>ERR:$errmsg";
return $msg;
}
ftp_set_option($conn_id, FTP_TIMEOUT_SEC, 10000);
if (!#ftp_put($conn_id, "test.txt", "C:......test.txt", FTP_BINARY)) {
echo "<BR><LI>ftp_put failed";
$errmsg = $php_errormsg;
echo "<LI>ERR:$errmsg";
}
echo "<HR>Done";
}
?>
the output when running this as a webpage is
Connecting to ***.***.***.***:21
Setting Passive Mode=
Connecting as *******/*****
ftp_put failed
ERR:ftp_put(): Unable to build data connection: Connection refused
Done
The result is that the ftp_put() gives the error message and leaves a zero (0) byte file with the right filename on the server.
The strange thing is is that
the same code/connection info works on another laptop ok
the same connection info works ok using FileZilla when pushing a file
the problem occurs on several servers (ie. it's not just one specific destination that has the problem)
Also, this doesn't seem to have anything to do with the passive mode (it fails with and without this enabled)
Does anyone have any suggestions?
Thanks
Abe
You are using the active FTP mode. In the active mode the server tries to connect to the client. In most network configurations, that's not possible as the client machine is usually behind a firewall.
That's why the server fails with:
Unable to build data connection: Connection refused
It's specifically ProFTPD error message for this situation.
See my article on the active and passive FTP connection modes for details.
The code can work on other machines, if they have firewall disabled or if they have rules that allow incoming traffic on unprivileged ports.
FileZilla works because it defaults to the passive mode (as most modern FTP clients do).
You have claimed to try the passive mode too, yet to get the same error message.
That's because you are using the ftp_pasv call incorrectly.
You have to move the ftp_pasv call after the ftp_login.
$user = "*********";
$pass = "*********";
echo "<LI>Connecting as $user/*****";
if (!ftp_login($conn_id, $user, $pass)) {
// ...
}
$passive = true;
echo "<LI>Setting Passive Mode=$passive";
ftp_pasv($conn_id, $passive);
The documentation clearly suggests it:
Please note that ftp_pasv() can only be called after a successful login or otherwise it will fail.
For a similar issue (just with Pure-FTPd), see PHP upload via FTP - ftp_put() I won't open a connection to x.x.x.x (only to y.y.y.y).

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

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.

Clueless on how to use fsockopen()

I am very new to php, and i am trying to connect to ftp and post a form, which has a few text fields, and 3 image upload, the images will be uploaded to the server. I am using godaddy, and they dont allow ftp_connect, only fsocketopen(),and only available on ports 80 (http) and 443(https). Can i have some advice on how to approach this(fsockopen)?
I researched and below is what i got, i assume the first part is server, second part is the port so i assume is 80(as godaddy said only that 2 ports are available), but what are the last 3? The $error_number,$error_string, and the last part?
Thanks for your time. Sorry that if the question is a newbie question. I researched for a while, i still can't fix it.
fsockopen('abc.com', '80', $error_number, $error_string, 30)
<?php
$ftp_user_name='name';
$ftp_user_pass='pass';
$connection = 'server';
$errno='';
$connect= fsockopen("abc.info", 80, $errno, $errstr, 30) or die ("Cannot connect to host");
$login = ftp_login($connect, $ftp_user_name, $ftp_user_pass);
if (!$connect)
{die ("FTP connection has encountered an error!");}
//exit;
if (!$login)
{die ("But failed at login Attempted to connect to $connection for user $ftp_user_name....");}
?>
At the risk of sounding conceited, RTM please.
From the PHP Docs:
errno If provided, holds the system level error number that
occurred in the system-level connect() call. If the value returned in
errno is 0 and the function returned FALSE, it is an indication that
the error occurred before the connect() call. This is most likely due
to a problem initializing the socket.
errstr The error message as a string.
timeout The connection timeout, in seconds.

stream_socket_client time out

I'm trying to debug the PHP function stream_socket_client but I don't really know how. This is the code that I'm having trouble with:
$this->socket = #stream_socket_client(
$remote, $errno, $errstr,
$this->request->getConfig('connect_timeout'),
STREAM_CLIENT_CONNECT, $context
);
if (!$this->socket) {
throw new HTTP_Request2_ConnectionException(
"Unable to connect to {$remote}. Error: {$errstr}",
0, $errno
);
}
The exception is thrown and the error reads "Unable to connect to tcp://www.dropbox.com:80. Error: Connection timed out". This code comes from a very popular Wordpress plugin that's been well tested. The server I'm working on has some quirks, e.g. I'm allowed to upload or remove files via PHP etc. so I'm wondering if there are any PHP settings that could prevent stream_socket_client from working and how I can check what those settings are for my server.
Try to check allow_url_fopen.
This option enables the URL-aware fopen wrappers that enable accessing URL object like files.
See: https://php.net/manual/en/filesystem.configuration.php

Categories