-1

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);
John Conde
  • 217,595
  • 99
  • 455
  • 496
  • 1
    That's not a file. That's a URL. – John Conde Dec 06 '14 at 16:17
  • You are right, I thought it will be something with the url, but this url is generating xml output, doesn't it? Can you please give me advice how to get the result? – Michal Slesingr Dec 06 '14 at 16:34
  • @MichalSlesingr: Please enable PHP error reporting and logging. Then take a look at the actual error messages. See as well: [How to get useful error messages in PHP?](http://stackoverflow.com/q/845021/367456) – hakre Dec 22 '14 at 11:40
  • I don't think it's constructive to ask a question like "why XXX does not work" while not doing any debugging nor providing error messages. – hakre Dec 22 '14 at 12:05

4 Answers4

0

You should use:

$Url='http://datacenter.biathlonresults.com/modules/sportapi/api/CupResults?CupId=BT1415SWRLCP__SMTS';
$XML=simplexml_load_file(file_get_contents($Url));
Ananth
  • 4,227
  • 2
  • 20
  • 26
  • This didn't solve the issue, still not working... I tried also file_get_contents and then simplexml_load_string, running out of ideas... :/ – Michal Slesingr Dec 06 '14 at 16:31
  • And you shouldn't use it either. Interally, file_get_contents as well as simplexml_load_file use exactly the same routines. Next to that, the code given in the answer is plain wrong, it was likely meant to use simplexml_load_string instead of simplexml_load_file. -1 for that. – hakre Dec 22 '14 at 11:34
0

Found out that file_get_contents returns JSON so:

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

does the trick...

0

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));
hakre
  • 193,403
  • 52
  • 435
  • 836
krzysiej
  • 889
  • 9
  • 22
0

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.

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836