1

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.

1 Answer 1

1

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);
}
3
  • Thank you, unfortunately, the file_get_contents() doesn't work, there is no loaded content I don't know why...
    – nico_lrx
    Sep 15, 2016 at 13:20
  • did your internet connection use a proxy? Sep 15, 2016 at 13:21
  • It works for me with no problems, make sure you pass a well formatted to file_get_contents() or CURLOPT_URL , i updated my answer with the code i used Sep 15, 2016 at 13:30

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.