Search code examples
phpftp

Why can't I access BoM's FTP server with PHP?


I'm trying to copy an XML file from the Bureau of Meteorology (Australian) Public Access Data Feeds with PHP to my server. I can open the file in the browser but I can't seem to touch it with PHP using CURL, FTP or simplexml_load_file. I've even tried to copy it with wget and I can't.

Full URL: ftp://ftp2.bom.gov.au/anon/gen/fwo/IDD10150.xml

// connect and login to FTP server
$ftp_username = "anonymous";
$ftp_userpass = "guest";
$ftp_server = "ftp2.bom.gov.au";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);

$local_file = "IDD10150.xml";
$server_file = "/anon/gen/fwo/IDD10150.xml";

// download server file
if (ftp_get($ftp_conn, $local_file, $server_file, FTP_ASCII))
  {
  echo "Successfully written to $local_file.";
  }
else
  {
  echo "Error downloading $server_file.";
  }

// close connection
ftp_close($ftp_conn);

Produces the following error

Warning: ftp_get(): Failed to establish connection...
Error downloading /anon/gen/fwo/IDD10150.xml.


Below is the update code as per suggestions below and the current error messages.

// connect and login to FTP server
$ftp_username = "anonymous";
$ftp_userpass = "guest";
$ftp_server = "ftp2.bom.gov.au";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
ftp_pasv($ftp_conn , TRUE);

$local_file = "IDD10150.xml";
$server_file = "/anon/gen/fwo/";

// download server file
if (ftp_get($ftp_conn, $local_file, $server_file, FTP_BINARY))
  {
  echo "Successfully written to $local_file.";
  }
else
  {
  echo "Error downloading $server_file.";
  }

// close connection
ftp_close($ftp_conn);

Produces the following error

Warning: ftp_get(): php_connect_nonb() failed: Operation now in progress (115) in...

Warning: ftp_get(): Switching to Binary mode. in...
Error downloading /anon/gen/fwo/.


Access via SSH

Last login: Mon Aug  3 11:25:27 on ttys000
MacBook-Pro:~ me$ ssh mysite.com
me@mysite.com's password: 
Last login: Mon Aug  3 11:27:37 2015 from IP
me@mysite.com [~]# ftp ftp.bom.gov.au
Connected to ftp.bom.gov.au (134.178.253.145).
220-Welcome to the Bureau of Meteorology FTP service.
220-
220-                              Disclaimer
220-
220-You accept all risks and responsibility for losses, damages, costs and
220-other consequences resulting directly or indirectly from using this site and
220-any information or material available from it.
220-
220-To the maximum permitted by law, the Bureau of Meteorology excludes all
220-liability to any person arising directly or indirectly from using this
220-site and any information or material available from it.
220-
220-Always Check the Information
220-
220-Information at this site:
220-
220-. is general information provided as part of the Bureau of Meteorology's
220-  statutory role in the dissemination of information relating to
220-  meteorology.
220-. is subject to the uncertainties of scientific and technical research
220-. may not be accurate, current or complete
220-. is subject to change without notice
220-. is not a substitute for independent professional advice and users
220-  should obtain any appropriate professional advice relevant to their
220-  particular circumstances
220-. the material on this web site may include the views or recommendations
220-  of third parties, which do not necessarily reflect the views of the
220-  Bureau of Meteorology or indicate its commitment to a particular course of
220-  action.
220 
Name (ftp.bom.gov.au:samw): anonymous
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> get
(remote-file) /anon/gen/fwo/IDQ13015.xml
(local-file) test.xml
local: test.xml remote: /anon/gen/fwo/IDQ13015.xml
227 Entering Passive Mode (134,178,253,145,77,229).
ftp: connect: Connection timed out

Reason

Ended up being the server Firewall blocking unknown outgoing connections which was found out by contacting BoM and working my server administrator. The BoM IP 134.178.253.145 was added and all was good.


Solution

  • First you need to use the passive mode (as suggested by the other answers):

    ftp_pasv($ftp_conn, true);
    

    It's unlikely that you succeed to connect in the default active mode as there's typically a firewall between your webserver and the FTP server, which won't allow connections from the FTP server back to your webserver.

    See my article on the FTP active/passive connection modes for details.


    Regarding the "Operation now in progress (115)" error you get, when using the passive mode.

    From the connect man page:

    If the connection cannot be established immediately and O_NONBLOCK is set for the file descriptor for the socket, connect() shall fail and set errno to [EINPROGRESS], but the connection request shall not be aborted, and the connection shall be established asynchronously. Subsequent calls to connect() for the same socket, before the connection is established, shall fail and set errno to [EALREADY].

    See also TCP Connect error 115 Operation in Progress What is the Cause?

    My guess is that the underlying problem is the firewall again, that won't allow the data transfer connection.

    If you have a shell access to the webserver, try to connect with the command-line ftp client to verify.


    Now that you have tried using the command-line ftp:

    As you have verified yourself, you cannot connect even from command-line ftp. There's nothing wrong with your PHP code. As such, your question was resolved from a programmer's point of view. The issue is with the network. What is off-topic on Stack Overflow.

    Contact your server administrator. Or consider using the SFTP, if you have the option. It should not suffer from these kind of problems.