stream_socket_client time out - php

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

Related

How to implement OpenSSL's SSL_Write() by using PHP

I am trying to connect to a server using PHP script. The server is set by SSLv3, I think maybe I need use SSL_Write() to process the message which will send to the server. But I do not find the related function in PHP. So, I wonder which function should I use.
What you are looking for is the TLS stream transport. There is no direct equivalent to the SSL_Write() function; PHP does not implement a functional interface to SSL.
Here is a simple example of using the TLS transport to make a connection to an HTTPS web server. This is not an ideal application of the transport, as PHP already natively supports an HTTPS file wrapper; I'm simply using it here as an example of interacting with a publicly accessible TLS server.
$fh = stream_socket_client("tls://example.com:443", $errno, $errstr);
if ($fh === false) {
die("Failed to connect: error=$errno $errstr");
}
fwrite($fh, "GET / HTTP/1.1\r\nHost: example.com\r\nConnection: close\r\n\r\n"\ );
while (!feof($fh)) {
print fgets($fh);
}
If you need to set specific options on the TLS connection, you can do so by creating a stream context (using stream_context_create()) before making the connection. Refer to the PHP documentation for more details.

SwiftMailer not working, connection could not be established [#0]

I'm trying to set up a mailer script using Silex and SwiftMailer. However, I haven't managed to get the mail sent so far. When setting swiftmailer.use_spool to false, I don't get errors but don't receive emails either. When instead flushing the spool transport queue like this...
if ($app['mailer.initialized']) {
$app['swiftmailer.spooltransport']
->getSpool()
->flushQueue($app['swiftmailer.transport']);
}
...I get the following exception:
Fatal error: Uncaught exception 'Swift_TransportException' with message 'Connection could not be established with host abc.xyz [ #0]'
I haven't found any documentation for this exception so far. I'm sure all SMTP settings and credentials are valid (I also tried another host, same problem). I can also ping the host and the port I use is open, and php_openssl is enabled.
It looks like it's failing on Swift\Transport\StreamBuffer.php:269 because stream_socket_client returns 0:
$this->_stream = #stream_socket_client($host.':'.$this->_params['port'], $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT, $streamContext);
if (false === $this->_stream) {
throw new Swift_TransportException(
'Connection could not be established with host '.$this->_params['host'].
' ['.$errstr.' #'.$errno.']'
);
}
Update #1
I forgot to have a look at the error log, now I see it says:
PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib/php5/20121212/php_openssl.dll' - /usr/lib/php5/20121212/php_openssl.dll: cannot open shared object file: No such file or directory in Unknown on line 0
So it seems PHP fails to load the php_openssl extension, although phpinfo() says the module is enabled? Where should it be located, or how should I get it?
Update #2
I believe I may have mistakenly tried to enable the php_openssl.dll extension while it's meant for Windows environments. When I remove it, the error is obviously gone from the error log, but the main issue persists. Back to square one..
What could I be doing wrong?

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.

socket connection fails in php script while it works in terminal

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.

fsockopen is returning an error even if the server im trying to reach is online?

im using fsockopen below:
$socket = fsockopen("uberminecraft.com", 25565, $errno, $errstr, 1);
return ($errno === 0);
Now this should return either false or true if the server is up or not. I know this server is definty up yet i still keep getting an error
Warning: fsockopen() [function.fsockopen]: unable to connect to uberminecraft.com:25565 (Connection timed out)
You have set a timeout of 1 second, is this intentional? Do you know whether the server is able to provide a response fast enough? Have you tried setting a higher timeout?
Also you might want to look at php.net for how to check whether the connection was made.
You must change fsockopen function as follow
$socket = fsockopen("uberminecraft.com", 25565, $errno, $errstr, 30);
Still you are getting same error. please tell to your host provider to open 25565 port.
Thanks

Categories