Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/243.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 正在加载包含文件\u get\u contents()的远程xml页面_Php_Xml_File Get Contents - Fatal编程技术网

Php 正在加载包含文件\u get\u contents()的远程xml页面

Php 正在加载包含文件\u get\u contents()的远程xml页面,php,xml,file-get-contents,Php,Xml,File Get Contents,我在网上看到过一些类似的问题,都没有答案 我想将远程XML页面的源返回到字符串中。就本问题而言,远程XML页面是: http://www.test.com/foo.xml 在常规的webbrowser中,我可以查看页面,并且源代码是XML文档。当我使用文件获取内容时('http://www.test.com/foo.xml),但是,它返回一个带有相应URL的字符串 是否存在检索XML组件的方法?我不在乎它是否使用文件内容,只是一些有用的东西。这似乎很奇怪。file_get_contents()

我在网上看到过一些类似的问题,都没有答案

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

http://www.test.com/foo.xml
在常规的webbrowser中,我可以查看页面,并且源代码是XML文档。当我使用
文件获取内容时('http://www.test.com/foo.xml)
,但是,它返回一个带有相应URL的字符串


是否存在检索XML组件的方法?我不在乎它是否使用文件内容,只是一些有用的东西。

这似乎很奇怪。file_get_contents()是否为其他站点(不仅仅是XML)返回任何有效数据?URL只能在已启用(默认情况下)的情况下用作文件名参数

我猜您稍后会处理检索到的XML—然后您应该能够使用simplexml\u load\u file()直接将其加载到中


我建议使用SimpleXML来读取XML文件,它非常容易使用。

您需要在服务器中设置allow\u url\u fopen才能工作

如果没有,则可以使用此功能作为替换:

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


借用自。

请看这个问题:它不一定是替代品-这些函数要求PHP在编译时启用cURL。如果禁用了fopen包装器,那么cURL功能也可能被禁用。为了安全起见,最好使用fsockopen()函数。我遇到过很多主机禁用了fopen包装器,但没有使用cURL,因此虽然它确实不是一个替换,但它可能是一个很好的解决方法。对于return语句,我更喜欢三元表达式,如:
return($contents)$内容:假
<?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;
    }
?>