duanjing1276
duanjing1276
2014-12-06 16:16

为什么我的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?..

$Url='http://datacenter.biathlonresults.com/modules/sportapi/api/CupResults?CupId=BT1415SWRLCP__SMTS';
$XML=simplexml_load_file($Url);
</div>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

4条回答

  • doucai5315 doucai5315 7年前

    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

    $Url='http://datacenter.biathlonresults.com/modules/sportapi/api/CupResults?CupId=BT1415SWRLCP__SMTS';
    
    $fileContent =  json_decode(file_get_contents($Url));
    
    点赞 评论 复制链接分享
  • dousong5492 dousong5492 7年前

    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:

    <?php
    
    $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
        var_dump(libxml_get_errors());
        return false;
    }
    
    $xml->asXML('php://output');
    

    Output:

    <?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.

    点赞 评论 复制链接分享
  • dongsheng9203 dongsheng9203 7年前

    Found out that file_get_contents returns JSON so:

    $x=json_decode(file_get_contents($Url));

    does the trick...

    点赞 评论 复制链接分享
  • dongyi8795 dongyi8795 7年前

    You should use:

    $Url='http://datacenter.biathlonresults.com/modules/sportapi/api/CupResults?CupId=BT1415SWRLCP__SMTS';
    $XML=simplexml_load_file(file_get_contents($Url));
    
    点赞 评论 复制链接分享

相关推荐