AlphaNico AlphaNico - 1 year ago 42
JSON Question

Can't get the xml because of a html line before first tag

I want to get the xml content of the following url and change it into Json: link

Unfortunately, there is a piece of html before so I can't get the xml:
"This XML file does not appear to have any style information associated with it. The document tree is shown below."

I get the following error:

Warning: simplexml_load_string(): Entity: line 1: parser error : Start tag expected, '<' not found in /Users/nicolasleroux/Sites/YouTube-Closed-Captions-master/closed-captions.php on line 56

Here is my code using this librairy to convert xml to Json:

$xmlNode = simplexml_load_string($baseUrl);
$arrayData = xmlToArray($xmlNode);
echo json_encode($arrayData);

is the URL of Youtube captions.

Thanks for your help.

Answer Source

You need to pass a XML content to simplexml_load_string function, as i see you pass a url.

Try to load a content into a new variable and pass that variable to the function, like this

$xmlData = file_get_contents($baseUrl)

$xmlNode = simplexml_load_string($xmlData);

Or using CURL

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "",

$response = curl_exec($curl);
$err = curl_error($curl);


if ($err) {
    echo "cURL Error #:" . $err;
} else {
    $xmlNode = simplexml_load_string($response);
    $arrayData = xmlToArray($xmlNode);
    echo json_encode($arrayData);