PHP ftp_put via SSL - php

One of our suppliers has requested we change from an ftp to ftps for sending them files.
I have a PHP script that uses ftp_connect to transfer the files.
I've modified the script to now use ftp_ssl_connect and while it can open the connection and login, it fails to transfer the files.
This is the error I get from the ftp_put operation:
"Warning: ftp_put(): Unable to build data connection: Operation not permitted in....."
Could this be a firewall issue? The server is an EC2 instance.
Paddy

I ran into the exact same problem today. Using
ftp_pasv($conn_id, true);
to enforce passive transfers before the actual call to ftp_put() solved the problem for me.

Related

Cannot connect to external FTPS server with php

I need to extract data from a .gz file which is located on an ftp server. The FTP server works with IP white listing, so I submitted my local IP and my website IP (i used gethostbynameto get the IP) which were both approved.
Locally, I can run this code to reach the file:
$url="ftp://username:password#host/targetfile.gz";
$xml = simplexml_load_file("compress.zlib://$url") or die ("Cannot load file");
This runs perfectly fine and it allows me to extract data from the XML file.
When I run the script on my server however, i'm not getting a connection. I contacted the admin running the ftp server and they told me they only allow FTPS connections.
So, I proceeded with the following code to try and establish a connection:
$conn_id = ftp_ssl_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user, $ftp_pass);
if (!$login_result) {
die("Cannot login.");
}
echo ftp_pwd($conn_id); // /
// close the ssl connection
ftp_close($conn_id);
This also doesn't connect. I'm new to FTP connections with PHP and FTP in general and i've got no clue on how to proceed from this point.
OpenSSL is enabled.
Can anyone point me in the right direction?
Edit: These are the error messages:
When I use the top block of code, this occurs:
Warning: simplexml_load_file(): I/O warning : failed to load external
entity
"compress.zlib://username:password#ftp.thehost.com/targetfile.gz"; in
/path/to/my/website/folder/htdocs/mydomain.com/feeds/script.php on
line 13
When I use the bottom block of code, I get this error:
Warning: ftp_login() expects parameter 1 to be resource, boolean given
in /path/to/my/website/folder/htdocs/mydomain.com/feeds/script.php on
line 16 Cannot login.
I do not have access to the log file afaik
I do not think there's much we can help you here.
You do not have a network connectivity between the web server and FTP server.
Note that it does not have to be because the web server has not been white-listed on the FTP server. It's also possible that the web server does not allow outgoing connections.

ftp_nlist(): data_accept: SSL/TLS handshake failed

Once upon a time, there was a normalish error in PHP land:
Warning: ftp_nlist(): data_accept: SSL/TLS handshake failed in [path] on line 29
But here's the catch, "line 29" is not the connection or login, note how it referenced the ftp_nlist() function:
$ftp = ftp_ssl_connect($cred['host'], $cred['port'], 180);
if (!ftp_login($ftp, $cred['user'], $cred['pass'])) {die("Login Failed");}
ftp_pasv($ftp, true);
$files = ftp_nlist($ftp, '');
OpenSSL is compiled and enabled in phpinfo() as suggested here:
ftp_login() : SSL/TLS handshake failed
Other posts I've seen all seem to reference error in the ftp_ssl_connect() or ftp_login() commands which work for me. What can I check when ftp_login() returns true?
Or... are there any logs to get more details on what is wrong?
I'm using php 5.3.29. The code does work properly on my desktop (php 7), but I'm hoping I don't have to upgrade the server to 7 for this to work
12-28-2017 update:
Upgrading to 5.6 resolved, so looks like Martin is on point.
Although this question is quite old, but in case someone else hits this problem:
If your ftp_ssl_connect and ftp_login works fine but functions like ftp_nlist, ftp_put, ftp_fput don't works the problem might be that your FTP server is using port 21 for Connection but different port ranges for data transfer, that explains why you can connect and login but you can't upload or download data, and you need to allow the Out-going connections to those port range in your firewall
The ftp_nlist opens a data connection. That connection needs TLS/SSL handshake too.
As the control connection handshake succeeded, the problem indeed cannot be with an absent TLS/SSL support in PHP. Neither the problem can be with anything like the server and PHP not being able to find a cipher to agree on.
When TLS/SSL handshake on data connection fails after handshake on control connection succeeded, it's quite usually because the client (PHP) did not reuse TLS/SSL session from control connection on the data connection (see Why is session reuse useful in FTPS?). Some servers do require that. PHP supports the reuse only since 5.6.26. See PHP Bug 70195. So make sure you use that version of PHP at least.

file_get_contents not working - connection refused

After reading the following links:
get url content PHP
file_get_contents failed to open stream: Connection refused godaddy server with remote connection server
PHP file_get_contents() returns "failed to open stream: HTTP request failed!"
I'm having a problem with the function file_get_contents or even curl in one of my servers at hostgator, they are not working, returning the PHP error failed to open stream: Connection refused. I've tried cURL with USERAGENT set with no result too. It's a simple weather service that I'm creating, it returns the altitude, wind direction, speed and temperature on a certain coordinate of the globe.
Return sample: 30000;221;2;-32;1;
In the other side (request side), I have a web server running IIS 7.5, with all router firewalls, computers firewalls and antivirus softwares disabled only for testing, and is still refusing the connection ONLY for the hostgator servers. I've tried the same code in other web hosting providers, and the code is working properly.
This service will handle a lot of requests per minute, and this seems to me that something has blocked my connection between hostgator and my server due to the number of requests. But I don't know where!!
The page is perfectly accesible via browser.
This is my enviorioment at the hostgator side:
allow_url_fopen: On
allow_url_include: On
OpenSSL: Enabled
Here is my PHP code:
$datalink = "http://#####.########.###:8280/weather.php?waypoint_lat=-10.981925&waypoint_lon=-37.077377&altitude=30000";
$weather_layer = file_get_contents($datalink);
echo "Layer ($datalink):" .$weather_layer."<br>";
Isn't Hostgator blocking the requests because of the DDoS protection? Give them a call, my hosting provider was blocking connections to my other server because they were thinking it was some hacker DDoSing using my hosting.
Also, there might be problem with the port in URL - Hostgator cannot proccess it?

APNS + PHP "stream_socket_client(): Failed to enable crypto"

I'm having trouble with using APNS with PHP and getting the following message:
stream_socket_client(): Failed to enable crypto
The problem only happens sometimes, and other times it would actually send the push.
Since I have the test script on a loop of 10 iterations, I would sometimes get this:
stream_socket_client(): SSL: Connection reset by peer
I'm testing using the sandbox server tls://gateway.sandbox.push.apple.com:2195
Here is what I tried:
I tried to reissue the PEM and all certificates with it.
I played around with the request protocol sslv3:// and tls://.
I played around with the passphrase (push worked without the passphrase btw)
I tried searching stackoverflow for a solution and nothing worked.
Checked pem file permissions 644
Checked pem parent directories permissions 755
It seems that all the solutions I found on Google and SO are people having problem pushing altogether.
I feel like the service is rate limited maybe? Because we waited a while (around 15 minutes) and then tried it again, and was able to successfully push around 100 messages until I started getting that message again.
The sandbox push service is rate limited. I have experienced this myself when testing but have never encountered any such limit using the production API.
You might also be hitting their other protections.
Are you opening a connection, sending a message, closing connection and then looping and doing it all over again?
That will get your notifications dropped. Apple wants you to send several push notifications using the same connection, not a new one each time.
Best Practices for Managing Connections
You may establish multiple connections to the same gateway or to
multiple gateway instances. If you need to send a large number of
remote notifications, spread them out over connections to several
different gateways. This improves performance compared to using a
single connection: it lets you send the remote notifications faster,
and it lets APNs deliver them faster.
Keep your connections with APNs open across multiple notifications;
don’t repeatedly open and close connections. APNs treats rapid
connection and disconnection as a denial-of-service attack. You should
leave a connection open unless you know it will be idle for an
extended period of time—for example, if you only send notifications to
your users once a day it is ok to use a new connection each day.
From Apple Docs # https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/CommunicatingWIthAPS.html
My PHP code was generating following error:
PHP Warning: stream_socket_client(): Failed to enable crypto in /private/tmp/t.php on line 12
PHP Warning: stream_socket_client(): unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Unknown error) in /private/tmp/t.php on line 12
PHP Warning: fclose() expects parameter 1 to be resource, boolean given in /private/tmp/t.php on line 24
The problem was, the damn certificate, expired the day before yesterday! :-) Can you believe this?
So, I need to recreate my PEM file.
It is not necessary recreate your pem file
that error happens when you use an incorrect PassPhrase
regards
Emiliano
I had this problem. Disappeared after giving write permission for 'everyone' for the .pem file.
I have this problem because I foolishly forgot to include the file extension (.pem) when supplying the file path for local_cert.
few checks :
device token should be - with out spaces and with out < or >
make sure the path of certificate is correct and expired date of it.
make sure the passphrase you are using is the one u used to make certificate
In my case, the issue was with my mac (OSX Sierra). I uploaded php and cert to my server, ran it, and the notification was delivered.
I tried examples from book of Marin Todorov iOs 6 by Tutorials.
And before I could send push notifications for automatical update I had a lot of headache cause of handshake error - stream_socket_client(): Failed to enable crypto.
I did all of what I found in Stackoverflow - changed permissions on certificate and others.
What I did eventually?
I created selfsigned SSL certificate and setup Apache for serving SSL.
Also I changed SSL protocol from ssl to tls in hostname:
tls://gateway.push.apple.com:2195
After that service works.

readfile() fails because of SSL

I'm using readfile() to serve a download from Rapidshare's API through my server. This is the relevant code:
readfile("http://rs$server_id$short_host.rapidshare.com/cgi-bin/rsapi.cgi?sub=download&fileid=$file_id&filename=$file_name&login={$account['username']}&password={$account['password']}");
But when I request the page, I get this error:
Warning: readfile(http://rs869l34.rapidshare.com/cgi-bin/rsapi.cgi?sub=download&fileid=3457766962&filename=some_file.rar&login=mylogin&password=mypassword) [function.readfile]: failed to open stream: Unable to find the socket transport "ssl" - did you forget to enable it when you configured PHP? in C:\xampp\htdocs\dl\downloaders\rapidshare_com.php on line 25
According to the Rapidshare API, there's no way to stop it returning an SSL response.
Do I need to configure some special wrapper to handle this or something?
Thanks for any help!
libCurl is used to download file from a remote server like as readfile(http://....).
Though it can handle finer details like as cookie, post and ssl certificates. read more at http://cn.php.net/manual/en/book.curl.php
enable curl in php.ini and try the following code :
$ch=curl_init("http://rs$server_id$short_host.rapidshare.com/cgi-bin/rsapi.cgi?sub=download&fileid=$file_id&filename=$file_name&login={$account['username']}&password={$account['password']}");
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true); // follow http redirect
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,true); //temporary ignore certificate error for easier testing
$data=curl_exec($ch);

Categories