2

I have an XML file I need to pass.

http://mckay.canvashost.com:8080/opentripplanner-api-webapp/ws/transit/stopTimesForStop?id=27833&startTime=1361784386000&extended=true&references=true

I used PHP's CURL to load it into the page:

$url = "http://mckay.canvashost.com:8080/opentripplanner-api-webapp/ws/transit/stopTimesForStop?id=27833&startTime=1361784386000&extended=true&references=true";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$xml = curl_exec($ch);      
curl_close($ch);
echo htmlentities($xml, ENT_QUOTES, "UTF-8");

The output is a JSON file, which I wasn't expecting. No matter, I continued working with the JSON but can't seem to parse it.

I had thought this would work:

$obj=json_decode($xml);
echo $obj[stopTime][0][phase];

With a result of 'departure'. I've spent a good while on this yet can't get it to output anything.

Anyone help? And anyone know at which point it switched from an XML file to JSON format?

10
  • What are you trying to extract out of this data?
    – Blender
    Feb 26, 2013 at 4:03
  • 2
    Try json_decode($xml,true);. PHP Doc.
    – Passerby
    Feb 26, 2013 at 4:08
  • That link returns an XML file for me. What makes you think it is JSON?
    – cdmckay
    Feb 26, 2013 at 4:20
  • the link does indeed return an XML file but the echo htmlentities($xml, ENT_QUOTES, "UTF-8"); gives an output in JSON Feb 26, 2013 at 4:40
  • The output is not json. You are outputting
    – bigblind
    Feb 26, 2013 at 4:41

1 Answer 1

0

the output is a xml-string. try to use php's xml functionality delivered by SimpleXml:

SimpleXml

as soon as you successfully parsed the string you can iterate its nodes like an array

foreach ($xmlElement as $node) {
echo $node->attributeName;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.