我试图将PHP文件从使用PHP的气象局(澳大利亚)公共访问数据源复制到我的服务器。我可以在浏览器中打开文件但我似乎无法使用CURL,FTP或simplexml_load_file
来触摸它。我甚至试图用wget
复制它,但我无法做到。
完整网址: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);
产生以下错误
警告:ftp_get():无法建立连接...
下载/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);
产生以下错误
警告:ftp_get():php_connect_nonb()失败:现在正在进行操作(115)......
警告:ftp_get():切换到二进制模式。在...
时出错
下载/ 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,一切都很好。
答案 0 :(得分:1)
您的服务器未处于被动模式,并将此代码添加到进程:
ftp_pasv($ftp, true);
有关更多信息,请参阅php.net上的被动模式:http://php.net/manual/en/function.ftp-pasv.php被动模式使用客户端而不是服务器发起的数据。所以这就是你不能穿上服务器的原因。如果未设置,则会失败。
注意:在ftp_pasv()
功能后设置ftp_login()
功能。
<强>更新强>
从ftp_pasv($ftp, true);
更改为ftp_pasv($ftp_conn, true);
答案 1 :(得分:0)
答案 2 :(得分:0)
首先,您需要使用被动模式(如其他答案所示):
ftp_pasv($ftp_conn, true);
您不可能成功连接默认的主动模式,因为您的网络服务器和FTP服务器之间通常都是防火墙,它不允许从FTP服务器返回到你的网络服务器。
有关详细信息,请参阅FTP active/passive connection modes上的我的文章。
关于使用被动模式的&#34;正在进行中的操作(115)&#34; 错误。
如果无法立即建立连接并且为套接字的文件描述符设置了O_NONBLOCK,则connect()将失败并将errno设置为[EINPROGRESS],但不应中止连接请求,并且应建立连接异步。在建立连接之前,对同一套接字的connect()的后续调用将失败并将errno设置为[EALREADY]。
另见TCP Connect error 115 Operation in Progress What is the Cause?
我的猜测是基础问题再次出现在防火墙上,不允许数据传输连接。
如果您具有对Web服务器的shell访问权限,请尝试使用命令行ftp
客户端进行连接以进行验证。
现在您已尝试使用命令行ftp
:
正如您自己验证的那样,即使从命令行ftp
也无法连接。您的PHP代码没有任何问题。因此,您的问题是从程序员的角度来解决的。问题出在网络上。什么是off-topic on Stack Overflow。
与您的服务器管理员联系。如果您有选择,可以考虑使用SFTP。它不应该受到这些问题的困扰。