由于第一个标记之前的html行,无法获取xml

时间:2022-09-22 15:07:36

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

我想获取以下url的xml内容并将其更改为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."

不幸的是,之前有一段html,所以我无法获取xml:“这个XML文件似乎没有与之关联的任何样式信息。文档树如下所示。”

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:

这是我使用此librairy将xml转换为Json的代码:

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

$baseUrl is the URL of Youtube captions.

$ baseUrl是Youtube标题的URL。

Thanks for your help.

谢谢你的帮助。

1 个解决方案

#1


1  

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

您需要将XML内容传递给simplexml_load_string函数,因为我看到您传递了一个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 = 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);
}

#1


1  

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

您需要将XML内容传递给simplexml_load_string函数,因为我看到您传递了一个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 = 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);
}