Reputation: 985
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
Upvotes: 0
Views: 1773
Reputation: 6393
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);
?>
Upvotes: 2