dongxun8189 2016-06-02 20:41
浏览 49
已采纳

外部XML文件未在php中加载。 HTTP禁止403

I have this xml resource and I want this to parsed in php.

http://example.gov

My php code :

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

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

Error I am receiving :

Warning: simplexml_load_file(http://forecast.weather.gov/MapClick.php?lat=41.98&lon=-87.9&FcstType=digitalDWML): failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden in C:\xampp\htdocs\xmlParser.php on line 4

Warning: simplexml_load_file(): I/O warning : failed to load external entity "http://forecast.weather.gov/MapClick.php?lat=41.98&lon=-87.9&FcstType=digitalDWML" in C:\xampp\htdocs\xmlParser.php on line 4

And just to add, xml resource is on different server

  • 写回答

1条回答 默认 最新

  • doulan0297 2016-06-02 21:21
    关注

    The server appears to be blocking requests based on user-agent. You can either use cURL or file_get_contents() and specify the user-agent with those and then use simplexml_load_string() to ingest the result of whichever of those you choose.

    <?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);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分