无法读取XML文件。 (file_get_contents无法打开流:连接超时)

时间:2014-12-18 20:19:36

标签: php xml file-get-contents

我正在尝试使用XML读取php文件并将其打印为JSON文件,但我收到以下错误:

Warning: file_get_contents(http://193.190.76.149:81/ParkoParkings/states.php): failed to open stream: Connection timed out in /customers/2/9/e/viktordebock.be/httpd.www/parca/index.php on line 7 false

我要解析的xml文件是:

(http://193.190.76.149:81/ParkoParkings/states.php)

当我使用不同的网址(http://www.parkodata.be/OpenData/ParkoInfo.xml)测试相同的代码时,一切正常。

是因为第一个网址是IP地址吗?

这是我的代码:

<?php

    function Parse ($url) {

        $fileContents = file_get_contents($url);
        $fileContents = str_replace(array("\n", "\r", "\t"), '', $fileContents);
        $fileContents = trim(str_replace('"', "'", $fileContents));

        $simpleXml = simplexml_load_string($fileContents);
        $json = json_encode($simpleXml);

        return $json;

    }

    print Parse("http://193.190.76.149:81/ParkoParkings/states.php");

?>

2 个答案:

答案 0 :(得分:0)

PHP file_get_contents() doc:'如果找不到文件名,则会生成E_WARNING级错误',

尽管您收到了错误,但这可能是404.请尝试自己浏览原始页面。

答案 1 :(得分:0)

您的代码运行正常!但您可以在文件顶部添加错误报告,并检查是否有任何错误!

否则再试一次!

代码:

<?php

    ini_set("display_errors", 1);
    error_reporting(E_ALL);

    function Parse ($url) {

        $fileContents = file_get_contents($url);
        $fileContents = str_replace(array("\n", "\r", "\t"), '', $fileContents);
        $fileContents = trim(str_replace('"', "'", $fileContents));

        $simpleXml = simplexml_load_string($fileContents);
        $json = json_encode($simpleXml);

        return $json;

    }

    echo Parse("http://193.190.76.149:81/ParkoParkings/states.php");

?>

输出:

{"parking":["P Veemarkt","P Schouwburg","P Broeltorens"]}

看到它正常工作:http://codepad.viper-7.com/SFVtQA

相关问题