Loading a remote xml page with file_get_contents()

微笑、不失礼 提交于 2019-11-29 01:49:22
Seb

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;
    }
?>

Borrowed from here.

That seems odd. Does file_get_contents() return any valid data for other sites (not only XML)? An URL can only be used as the filename parameter if the fopen-wrappers has been enabled (which they are by default).

I'm guessing you're going to process the retrieved XML later on - then you should be able to load it into SimpleXml directly using the simplexml_load _file().

try {
   $xml = simplexml_load_file('http://www.test.com/foo.xml');
   print_r($xml);
} ...

I recommend using SimpleXML for reading XML-files, it's very easy to use.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!