为什么我不能用 PHP 访问 BoM 的 FTP 服务器?
Why can't I access BoM's FTP server with PHP?
我正在尝试从气象局(澳大利亚)Public Access Data Feeds PHP 复制一个 XML 文件到我的服务器。我可以在浏览器中打开该文件,但我似乎无法使用 CURL、FTP 或 simplexml_load_file
以 PHP 触摸它。我什至尝试用 wget
复制它,但我做不到。
完整 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);
产生以下错误
Warning: ftp_get(): Failed to establish connection...
Error downloading /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);
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);
产生以下错误
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/.
编辑 2: 通过 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
编辑 3 和原因
最终成为服务器防火墙阻止未知的传出连接,这是通过联系 BoM 和我的服务器管理员工作发现的。已添加 BoM IP 134.178.253.145,一切正常。
你的服务器不是被动模式,添加这段代码处理:
ftp_pasv($ftp, true);
有关更多信息,请查看 php.net 上的被动模式:http://php.net/manual/en/function.ftp-pasv.php 被动模式使用客户端而非服务器发起的数据。所以这就是为什么你不能放在服务器上。如果未设置,它将失败。
注意: 在ftp_login()
函数之后设置ftp_pasv()
函数。
更新
从 ftp_pasv($ftp, true);
更改为 ftp_pasv($ftp_conn, true);
ftp_pasv($ftp_conn , TRUE);
编辑 01
$local_file = fopen("IDD10150.xml",'w');
首先你需要使用被动模式(如其他答案所建议的):
ftp_pasv($ftp_conn, true);
您不太可能在默认活动模式下成功连接,因为您的网络服务器和 FTP 服务器之间通常有防火墙,不允许从 FTP 服务器返回到你的网络服务器。
有关详细信息,请参阅 我在 FTP active/passive connection modes 上的 文章。
关于使用被动模式时出现的 "Operation now in progress (115)" 错误。
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].
另见 TCP Connect error 115 Operation in Progress What is the Cause?
我的猜测 潜在的问题又是防火墙,不允许数据传输连接。
如果您可以 shell 访问网络服务器,请尝试连接命令行 ftp
客户端进行验证。
现在您已经尝试使用命令行 ftp
:
正如您自己验证的那样,您甚至无法从命令行连接 ftp
。您的 PHP 代码没有任何问题。因此,您的问题是从程序员的角度解决的。问题出在网络上。什么是 off-topic on Stack Overflow.
请联系您的服务器管理员。或者考虑使用 SFTP,如果可以的话。它不应该遇到这些问题。
我正在尝试从气象局(澳大利亚)Public Access Data Feeds PHP 复制一个 XML 文件到我的服务器。我可以在浏览器中打开该文件,但我似乎无法使用 CURL、FTP 或 simplexml_load_file
以 PHP 触摸它。我什至尝试用 wget
复制它,但我做不到。
完整 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);
产生以下错误
Warning: ftp_get(): Failed to establish connection...
Error downloading /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);
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);
产生以下错误
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/.
编辑 2: 通过 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
编辑 3 和原因
最终成为服务器防火墙阻止未知的传出连接,这是通过联系 BoM 和我的服务器管理员工作发现的。已添加 BoM IP 134.178.253.145,一切正常。
你的服务器不是被动模式,添加这段代码处理:
ftp_pasv($ftp, true);
有关更多信息,请查看 php.net 上的被动模式:http://php.net/manual/en/function.ftp-pasv.php 被动模式使用客户端而非服务器发起的数据。所以这就是为什么你不能放在服务器上。如果未设置,它将失败。
注意: 在ftp_login()
函数之后设置ftp_pasv()
函数。
更新
从 ftp_pasv($ftp, true);
更改为 ftp_pasv($ftp_conn, true);
ftp_pasv($ftp_conn , TRUE);
编辑 01
$local_file = fopen("IDD10150.xml",'w');
首先你需要使用被动模式(如其他答案所建议的):
ftp_pasv($ftp_conn, true);
您不太可能在默认活动模式下成功连接,因为您的网络服务器和 FTP 服务器之间通常有防火墙,不允许从 FTP 服务器返回到你的网络服务器。
有关详细信息,请参阅 我在 FTP active/passive connection modes 上的 文章。
关于使用被动模式时出现的 "Operation now in progress (115)" 错误。
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].
另见 TCP Connect error 115 Operation in Progress What is the Cause?
我的猜测 潜在的问题又是防火墙,不允许数据传输连接。
如果您可以 shell 访问网络服务器,请尝试连接命令行 ftp
客户端进行验证。
现在您已经尝试使用命令行 ftp
:
正如您自己验证的那样,您甚至无法从命令行连接 ftp
。您的 PHP 代码没有任何问题。因此,您的问题是从程序员的角度解决的。问题出在网络上。什么是 off-topic on Stack Overflow.
请联系您的服务器管理员。或者考虑使用 SFTP,如果可以的话。它不应该遇到这些问题。