AlphaNico AlphaNico - 2 months ago 7
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);


$baseUrl
is the URL of Youtube captions.

Thanks for your help.

Answer

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 => "https://www.youtube.com/api/timedtext?key=yttt1&expire=1473964248&caps=asr&sparams=asr_langs%2Ccaps%2Cv%2Cexpire&signature=616905FBCCA3D1416505086247C279F998C4E540.940715461068C48B432B6B1A3AD33628C450CF86&v=WlaoL3A9ros&hl=fr_FR&asr_langs=it%2Cko%2Cfr%2Cen%2Cpt%2Cja%2Cnl%2Ces%2Cde%2Cru&type=track&lang=en&name=&kind=asr&fmt=1",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
));

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

curl_close($curl);

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