使用file_get_contents()加载远程xml页面 [英] Loading a remote xml page with file_get_contents()

查看:165
本文介绍了使用file_get_contents()加载远程xml页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在互联网上看到了一些与此类似的问题,没有一个答案.

I have seen some questions similar to this on the internet, none with an answer.

我想将远程XML页面的源返回到字符串中.就此问题而言,远程XML页面是:

I want to return the source of a remote XML page into a string. The remote XML page, for the purposes of this question, is:

http://www.test.com/foo.xml

在常规的Web浏览器中,我可以查看页面,并且源是XML文档.但是,当我使用file_get_contents('http://www.test.com/foo.xml')时,它将返回带有相应URL的字符串.

In a regular webbrowser, I can view the page and the source is an XML document. When I use file_get_contents('http://www.test.com/foo.xml'), however, it returns a string with the corresponding URL.

是否可以检索XML组件?我不在乎它是否使用file_get_contents,只是可以使用.

Is there to retrieve the XML component? I don't care if it uses file_get_contents or not, just something that will work.

推荐答案

您需要在服务器中设置allow_url_fopen才能使其正常工作.

You need to have allow_url_fopen set in your server for this to work.

如果不这样做,则可以使用此功能代替:

If you don´t, then you can use this function as a replacement:

<?php
function curl_get_file_contents($URL)
    {
        $c = curl_init();
        curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($c, CURLOPT_URL, $URL);
        $contents = curl_exec($c);
        curl_close($c);

        if ($contents) return $contents;
            else return FALSE;
    }
?>

此处借来.

这篇关于使用file_get_contents()加载远程xml页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆