2

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.

Edit: 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/.

Edit 2: 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

Edit 3 & 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.

Sam
  • 185
  • 1
  • 3
  • 16

3 Answers3

1

Your server is not in a passive mode, and add this code to process:

ftp_pasv($ftp, true);

For more information look at passive mode on php.net: http://php.net/manual/en/function.ftp-pasv.php Passive mode uses the data initiated by the client rather than the server. So this is why you can't put on server. If this is not set it will fail.

NOTE: Set ftp_pasv() function after ftp_login() function.

Update

Change from ftp_pasv($ftp, true); to ftp_pasv($ftp_conn, true);

Muhammad Hassaan
  • 7,296
  • 6
  • 30
  • 50
0

ftp_pasv($ftp_conn , TRUE);

ftp_pasv() in w3schools

Edit 01

$local_file = fopen("IDD10150.xml",'w');
Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
  • Thanks but that didn't work either. Got the following errors. Warning: ftp_get(): php_connect_nonb() failed: Operation now in progress (115) Warning: ftp_get(): Switching to Binary mode. Error downloading /anon/gen/fwo/. – Sam Jul 29 '15 at 05:29
  • paste you code and just paste above code to it. look at the link – Abdulla Nilam Jul 29 '15 at 05:31
  • I did, and it doesn't work. Paste my code where? Too long to fit here as a comment. – Sam Jul 29 '15 at 05:34
  • paste after your `ftp_login` – Abdulla Nilam Jul 29 '15 at 05:35
  • The "Edit 01" is nonsense. The `local_file` argument of `ftp_get` is "The local file path". And the problem is with opening data transfer connection, not accessing/writing the local file anyway. – Martin Prikryl Jul 29 '15 at 07:28
0

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.

Community
  • 1
  • 1
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • I can connect via terminal on OSX if that helps? This link is their guide on how to connect if that also helps. I need to automate the process to happen daily, so that's why I was looking to PHP/Cron Jobs. http://reg.bom.gov.au/other/Ftp.shtml – Sam Jul 30 '15 at 03:26
  • Took a while to get SSH access enabled, I've updated my code with the latest progress. – Sam Aug 03 '15 at 01:38