简体   繁体   中英

external XML file not loading in php . HTTP forbidden 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM