将 php 生成的 xml 文件提供给 javascript 解析器

Providing php generated xml file to javascript parser

我正在开发一款可以播放直播的智能电视应用。当我向其提供有效的 xml 播放列表时,应用程序本身运行良好。

但是当我使用 php 生成 xml 文件时(生成的也很好),它不起作用。

我收到一个错误:

TypeError: 'null' is not an object (evaluating 'this.XHRObj.responseXML.documentElement')

这是我的 php 文件,它生成 videoList.xml,100% 有效。 简而言之,此脚本检查数据库中是否存在 MAC 地址,如果是,则写入 videoList.xml 和有效的直播链接。 SamsungAPI.php

<?php
$MAC = $_GET['MAC'];
require_once('../config.php'); 
//Remove brackets form array
$_INFO = preg_replace('/[{}]/', '', $_INFO);
$mysqli = new mysqli($_INFO['host'], $_INFO['db_user'], $_INFO['db_pass'], $_INFO['db_name']);
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}
$sql="SELECT * FROM users WHERE admin_notes = '$MAC' ";
$rs=$mysqli->query($sql);
$rows=mysqli_num_rows($rs);
if ($rows == 1) {
    //MAC FOUND
    $row = mysqli_fetch_array($rs);
    $username = $row['username'];
    $password = $row['password'];
    $file = "videoList.xml";
         $txt_file = file_get_contents('http://' . $_SERVER['HTTP_HOST'] . '/get.php?type=starlivev3&username=' . $username . '&password=' . $password . '&output=hls');
           $rows        = explode("\n", $txt_file);
              if(empty($rows[count($rows)-1])) {
                 unset($rows[count($rows)-1]);
                 $rows=array_map('trim',$rows);
              }
       $handle = fopen($file, "w+") or die('Could not open file');
       fwrite($handle, "<?xml version=\"1.0\"?>"."\n");
       fwrite($handle, "<rss version=\"2.0\">"."\n");
       fwrite($handle, "<channel>"."\n");
  foreach($rows as $row => $data)
       {
    //get row data
    $row_data = explode(',', $data);
    //replace _ with spaces
    $row_data[0] = str_replace('_', ' ', $row_data[0]);


       //generate playlist content
       fwrite($handle, "<item>"."\n");
       fwrite($handle, "<title>{$row_data[0]}</title>"."\n");
       fwrite($handle, "<link>{$row_data[1]}</link>"."\n");
       fwrite($handle, "<description> Reserved for EPG </description>"."\n");
       fwrite($handle, "</item>"."\n");
     }
fwrite($handle, "</channel>"."\n");
fwrite($handle, "</rss>");
fclose($handle);
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    readfile($file);
    exit;

} else {
//MAC NOT FOUND
echo "MAC NOT FOUND";
}
mysqli_close($mysqli); // Closing Connection

?>

然后在三星智能电视视频播放器应用程序中,我有 xml 解析器,如下所示:

Server.js

var Server =
{
    /* Callback function to be set by client */
    dataReceivedCallback : null,

    XHRObj : null,
    url : "http://myvalidhost.com/samsungAPI.php?MAC=02000027000b"
}

Server.init = function()
{
    var success = true;

    if (this.XHRObj)
    {
        this.XHRObj.destroy();  // Save memory
        this.XHRObj = null;
    }

    return success;
}

Server.fetchVideoList = function()
{
    if (this.XHRObj == null)
    {
        this.XHRObj = new XMLHttpRequest();
    }

    if (this.XHRObj)
    {
        this.XHRObj.onreadystatechange = function()
            {
                if (Server.XHRObj.readyState == 4)
                {
                    Server.createVideoList();
                }
            }

        this.XHRObj.open("GET", this.url, true);
        this.XHRObj.send(null);
     }
    else
    {
        alert("Failed to create XHR");
    }
}

Server.createVideoList = function()
{
    if (this.XHRObj.status != 200)
    {
        Display.status("XML Server Error " + this.XHRObj.status);
    }
    else
    {
        var xmlElement = this.XHRObj.responseXML.documentElement;

        if (!xmlElement)
        {
            alert("Failed to get valid XML");
        }
        else
        {
            // Parse RSS
            // Get all "item" elements
            var items = xmlElement.getElementsByTagName("item");

            var videoNames = [ ];
            var videoURLs = [ ];
            var videoDescriptions = [ ];

            for (var index = 0; index < items.length; index++)
            {
                var titleElement = items[index].getElementsByTagName("title")[0];
                var descriptionElement = items[index].getElementsByTagName("description")[0];
                var linkElement = items[index].getElementsByTagName("link")[0];
                if (titleElement && descriptionElement && linkElement)
                {
                    videoNames[index] = titleElement.firstChild.data;

                    if(linkElement.firstChild.data.substring(0,4) !="http"){
                        alert("asdasdasd  "+linkElement.firstChild.data.substring(0,4));
                        var rootPath = window.location.href.substring(0, location.href.lastIndexOf("/")+1);
                        var Abs_path = unescape(rootPath).split("file://")[1]+linkElement.firstChild.data;
                        videoURLs[index] = Abs_path;        
                    }
                    else{
                        videoURLs[index] = linkElement.firstChild.data;                         
                    }
                    videoDescriptions[index] = descriptionElement.firstChild.data;
                }
            }

            Data.setVideoNames(videoNames);
            Data.setVideoURLs(videoURLs);
            Data.setVideoDescriptions(videoDescriptions);

            if (this.dataReceivedCallback)
            {
                this.dataReceivedCallback();    /* Notify all data is received and stored */
            }
        }
    }
}

有谁知道为什么它不接受我生成的 xml 文件?

问候 M

我想通了,在php headers 内容类型是错误的。

已更改 header('Content-Type: application/octet-stream');

header('Content-Type: application/xml');

现在完美运行了!