【问题标题】:external XML file not loading in php . HTTP forbidden 403外部 XML 文件未在 php 中加载。 HTTP 禁止 403
【发布时间】:2016-10-02 18:35:48
【问题描述】:

我有这个 xml 资源,我想用 php 解析它。

http://example.gov

我的php代码:

<?php
$userinput='http://example.gov/MapClick.php?lat=41.98&lon=-87.9&FcstType=digitalDWML';

$xml = simplexml_load_file($userinput);
echo $xml;
?>

我收到错误:

警告:simplexml_load_file(http://forecast.weather.gov/MapClick.php?lat=41.98&lon=-87.9&FcstType=digitalDWML):打开流失败:HTTP 请求失败! HTTP/1.0 403 Forbidden in C:\xampp\htdocs\xmlParser.php on line 4

警告:simplexml_load_file():I/O 警告:未能在第 4 行的 C:\xampp\htdocs\xmlParser.php 中加载外部实体“http://forecast.weather.gov/MapClick.php?lat=41.98&lon=-87.9&FcstType=digitalDWML

只是补充一下,xml资源在不同的服务器上

【问题讨论】:

    标签: php xml xml-parsing


    【解决方案1】:

    服务器似乎正在阻止基于用户代理的请求。您可以使用 cURL 或 file_get_contents() 并使用它们指定用户代理,然后使用 simplexml_load_string() 提取您选择的任何一个的结果。

    <?php
        $context  = stream_context_create(array('http' => array('header' => 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36')));
    
        $url = "http://forecast.weather.gov/MapClick.php?lat=41.98&lon=-87.9&FcstType=digitalDWML";
    
        $xml = file_get_contents($url, false, $context);
        $xmlObject = simplexml_load_string($xml);
        print_r($xmlObject);
    ?>
    

    【讨论】:

    • 像魅力一样工作
    猜你喜欢
    • 1970-01-01
    • 2012-07-17
    • 1970-01-01
    • 2012-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-25
    相关资源
    最近更新 更多