【发布时间】:2024-05-20 07:40:02
【问题描述】:
我正在尝试从 FTP 服务器获取文件,使用 WordPress 短代码。
在开始创建 WP 短代码之前,我在“空白”的非 WP 环境中编写并测试了我的代码,它运行良好。
然后,我将代码移动到 WP 短代码中。现在我遇到了问题。
首先,我了解到您不能在 WordPress 中使用 file_get_contents()....
...所以,我用 wp_remote_get() 替换了该函数。但是,此函数不接受以 'ftp://' 开头的 URL。
这是我的代码,有什么想法需要改变才能让它在 WordPress 中运行吗?:
$stuff = array();
$conn_id = ftp_connect($ftp_server) or die("Error: Cannot connect to FTP Server.");
// try to login
if (@ftp_login($conn_id, $ftp_user, $ftp_pass)) {
echo "";
} else {
echo "Error: Cannot connect to FTP Server.";
}
if (($response_xml_data = file_get_contents('ftp://username:password@ftp.example.co.uk/file.xml'))===false){
echo "Error: Failed to fetch file.<br/>";
} else {
libxml_use_internal_errors(true);
print_r($response_xml_data);
$data = simplexml_load_string($response_xml_data);
if (!$data) {
echo "Error loading XML\n";
foreach(libxml_get_errors() as $error) {
echo "\t", $error->message;
}
} else {
$stuff = $data;
}
}
// close the connection
ftp_close($conn_id);
当我用 wp_remote_get() 替换 file_get_contents() 时,$response_xml_data 等于:
WP_Error Object ( [errors] => Array ( [http_request_failed] => Array ( [0] => A valid URL was not provided. ) ) [error_data] => Array ( ) )
【问题讨论】:
标签: php wordpress ftp file-get-contents