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.