我正在尝试阅读外部xml Feed的内容。当我在浏览器中查看链接时,它会按原样呈现xml内容,但是当我在浏览器中运行它时会出现错误。我的代码如下:
<?php
$url = "http://www.thebigchoice.com/feeds/job_xml.php";
$xml = simplexml_load_file($url);
print_r($xml);
?>
这是我得到的错误:
( ! ) 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 ( )
答案 0 :(得分:1)
网址的输出为
Unknown type: [8] Undefined index: HTTP_USER_AGENT<br />
Line : 20 File :/var/www/thebigchoice/html/includes/Common/setup.php<br /><br />WARNING: [2] Cannot modify header information - headers already sent by (output started at /var/www/thebigchoice/html/includes/Common/functions.php:80)<br />
Line : 10 File :/var/www/thebigchoice/html/includes/Common/feeds/feeds_complete_job_feed_minus_247.php<br /><br /><?xml version="1.0" encoding="utf-8"?>
<XMLJobFeed><Job><CompanyRef>60</CompanyRef><JobTitle>Enterprise Management Training Programme</JobTitle><SummaryLocation>Worldwide</SummaryLocation><SalaryBenefits>Competative</SalaryBenefits><Summary>SWYgeW91IHNlZSB5b3Vyc2VsZiBhcyBvbmU
显然,脚本在HTTP请求中检查用户代理,找不到它,然后在发送实际XML之前输出错误。这显然是一个写得不好的剧本。
解决方案是设置UserAgent。您可以globally in your php.ini或stream context执行此操作。请参阅我对How can I download using PHP a XML file redirected in some weird way?的答案,了解如何实现这一目标。