2014-12-06 16:16
浏览 109

为什么我的simplexml_load_file不起作用? [重复]

This question already has an answer here:

I'm stuck with getting XML content with simplexml_load_file, any idea why it does not work? Does it have something to do with the source below?..


图片转代码服务由CSDN问答提供 功能建议

此问题已经存在 这里有一个答案:

  • 无法使用simplexml_load_file PHP读取XML 2 answers < / span>

    我一直坚持使用simplexml_load_file获取XML内容,不知道为什么会这样做 不行? 它与下面的源有关吗?..

      $ Url ='http://datacenter.biathlonresults.com/modules/sportapi/api/CupResults?CupId  = BT1415SWRLCP__SMTS'; 
     $ XML = simplexml_load_file($ Url); 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • doucai5315 2014-12-06 19:34

    For fom reason if You open this link in browser it's xml. If you trying to get i through php it's JSON. try this code

    $fileContent =  json_decode(file_get_contents($Url));
    打赏 评论
  • dongyi8795 2014-12-06 16:18

    You should use:

    打赏 评论
  • dongsheng9203 2014-12-06 19:25

    Found out that file_get_contents returns JSON so:


    does the trick...

    打赏 评论
  • dousong5492 2014-12-22 12:00

    There are two little (but common) mistakes with your code that prevent you to find out what's going on here on your own quickly (and how to find a solution).

    First of all, you're not doing any error checking. simplexml_load_file() will return FALSE in case it fails to open the file.

    $xml = simplexml_load_file($url);
    if (!$xml) {
        // error opening the URL
        return false;

    This is not yet very informative, you could now just enable PHP error reporting / logging to actually see which errors are created:

    Warning: simplexml_load_file(): http://datacenter.biathlonresults.com/modules/sportapi/api/CupResults?CupId=BT1415SWRLCP__SMTS:1: parser error : Start tag expected, '<' not found in [...]

    Warning: simplexml_load_file(): {"AsOf":"2014-12-22T11:45:50.5976703+00:00","RaceCount":25,"Rows":[{"Rank":"1"," in [...]

    Warning: simplexml_load_file(): ^ in [...]

    Which already signals that the HTTP request to that URL does not provide XML but JSON (see the second warning).

    Which is easy to validate by telling the server to accept XML here:

    stream_context_set_default(['http' => ['header' => "Accept: text/xml"]]);
    $xml = simplexml_load_file($url);

    whcih now just works, the server now provides XML which can be properly parsed by and the SimpleXMLElement created.

    The full code example:

    $url = 'http://datacenter.biathlonresults.com/modules/sportapi/api/CupResults?CupId=BT1415SWRLCP__SMTS';
    stream_context_set_default(['http' => ['header' => "Accept: text/xml"]]);
    $xml = simplexml_load_file($url);
    if (!$xml) {
        // error opening the file
        return false;


    <?xml version="1.0"?>
    <CupResultsResponse xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/sportapi"><AsOf>2014-12-22T11:45:50.5976703+00:00</AsOf><RaceCount>25</RaceCount><Rows><CupResultRow>[...]

    This code example is a shorter version of the answer of a very similar question which covers the same ground:

    This behaviour seems typical for Microsoft-IIS Server running ASP.NET, most likely some REST API component.

    打赏 评论

相关推荐 更多相似问题