duandingcu7010 2016-06-22 10:58
浏览 82

从FTP服务器获取文件内容 - WordPress

I'm trying to get a file from an FTP server, in a WordPress shortcode.

Before starting to create a WP shortcode, I wrote and tested my code in a 'blank', non-WP environment, and it was working fine.

Then, I moved the code into a WP shortcode. And now I've got problems.

First, I learned that you can't use file_get_contents() in WordPress....

...So, I replaced that function with wp_remote_get(). But, this function doesn't accept a URL beginning with 'ftp://'.

Here is my code, any ideas on what needs to change to get it working in 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
";
        foreach(libxml_get_errors() as $error) {
            echo "\t", $error->message;
        }
    } else {
        $stuff = $data;
    }
}

// close the connection
ftp_close($conn_id);

When I replace file_get_contents() with wp_remote_get(), $response_xml_data equals:

WP_Error Object ( [errors] => Array ( [http_request_failed] => Array ( [0] => A valid URL was not provided. ) ) [error_data] => Array ( ) )
  • 写回答

1条回答 默认 最新

  • doufu7464 2016-06-22 11:49
    关注

    Scratch that... The problem isn't in the code, it's a server thing. 'allow_url_fopen' setting in php.ini must be set to '1'.

    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况