I’m trying to read the contents of an external xml feed. When I view the link in the browser it renders the xml contents as it should, but when I run it in my browser I get errors. My code is as follows:
<?php
$url = "http://www.thebigchoice.com/feeds/job_xml.php";
$xml = simplexml_load_file($url);
print_r($xml);
?>
Here is the error that I’m getting:
( ! ) Warning: simplexml_load_file() [function.simplexml-load-file]: http://www.thebigchoice.com/feeds/job_xml.php:1: parser error : Start tag expected, '<' not found in C:\wamp2\www\phpAcademy\TheBigChoice\TMPr785mziagz.php on line 14
Call Stack
# Time Memory Function Location
1 0.0004 365880 {main}( ) ..\TMPr785mziagz.php:0
2 0.0004 366048 simplexml_load_file ( ) ..\TMPr785mziagz.php:14
( ! ) Warning: simplexml_load_file() [function.simplexml-load-file]: Unknown type: [8] Undefined index: HTTP_USER_AGENT<br /> in C:\wamp2\www\phpAcademy\TheBigChoice\TMPr785mziagz.php on line 14
Call Stack
# Time Memory Function Location
1 0.0004 365880 {main}( ) ..\TMPr785mziagz.php:0
2 0.0004 366048 simplexml_load_file ( ) ..\TMPr785mziagz.php:14
( ! ) Warning: simplexml_load_file() [function.simplexml-load-file]: ^ in C:\wamp2\www\phpAcademy\TheBigChoice\TMPr785mziagz.php on line 14
Call Stack
# Time Memory Function Location
1 0.0004 365880 {main}( ) ..\TMPr785mziagz.php:0
2 0.0004 366048 simplexml_load_file ( )
The output of the URL is
So apparently, the script checks for the User Agent in the HTTP Request, cannot find it and then outputs errors before it sends the actual XML. It’s apparently a poorly written script.
The solution is to set the UserAgent. You can do that globally in your php.ini or with a stream context. See my answer to How can I download using PHP a XML file redirected in some weird way? on how to achieve that.