0

I am trying to convert an xml feed to json, but using this code it fails to do anything. Any ideas?

<?php
$xml_string = 'https://xml.betfred.com/Horse-Racing-Daily.xml';

//read the XML file
$xml = file_get_contents($xml_string);



//encode the formatted data
$json = json_encode();

//generate the JSON file
header('Content-Type: application/json'); 
echo $json;
?>
BCLtd
  • 1,459
  • 2
  • 18
  • 45

1 Answers1

0

You forgot to put $xml in the json_encode() method as parameter. Adjust your code like this:

$json = json_encode($xml);

this will however only convert the $xml string to json and that is probably not what you want.

To get close to what you are trying to do you could do:

$simpleXml = simplexml_load_string($xml);
$json = json_encode($simpleXml);

Your json might contain alot of junk, but maybe it will be enough for your needs.

Damien Overeem
  • 4,487
  • 4
  • 36
  • 55