Uncaught TypeError:无法读取PHP中加载的XML的undefined属性“ Data”

时间:2019-05-22 14:55:58

标签: php xml

我无法从xml格式的api接收数据,我看到PHP版本7出现此问题,我不知道这是什么问题,服务器上唯一的变化是PHP版本,在5.4版本上全部代码工作,我看到的数据没有问题。我在这里发布了导致此问题的代码部分以及控制台上的错误:

这是我在控制台中看到的错误

Uncaught TypeError: Cannot read property 'Data' of undefined
    at Object.complete ((index):654)
    at u (jquery.min.js:2)
    at Object.fireWith (jquery.min.js:2)
    at k (jquery.min.js:2)
    at XMLHttpRequest.<anonymous> (jquery.min.js:2)

这是文件index.php(我只粘贴了相关部分)

<?php

  $uid = substr(str_shuffle(MD5(microtime())), 0, 10);
  $file = "api/data/data.txt";
  $fileLink = fopen($file, 'w+') or die("Can't open file.");
  fwrite($fileLink, $uid);
  fclose($fileLink);
?>


<script>
 // Setup timeout for XML request
 function getXML(){
  var uid = "<?php echo $uid ?>";
  $.ajax({
    url: "api/api.php",
    data: {uid : uid},
    dataType: "json",
    complete: function(data){
      if(data["responseJSON"]["Data"]) {
        $("#input").html("Output:</br>" + data["responseJSON"]["Data"]["Active"]);
        $("#external").html("External:</br>" + data["responseJSON"]["Data"]["External"]);
        $("#recording").html("Recording:</br>" + data["responseJSON"]["Data"]["Recording"]);
        $("#streaming").html("Streaming:</br>" + data["responseJSON"]["Data"]["Streaming"]);
        $("#fullscreen").html("Fullscreen:</br>" + data["responseJSON"]["Data"]["Fullscreen"]);
        $("#FTB").html("FTB:</br>" + data["responseJSON"]["Data"]["FTB"]);
      }
    },
    error: function(xhr, status, err) {
    }
  });
 }

 setInterval(getXML, 5000);
});
</script>

这是api.php文件

<?php

    $file = "data/data.txt";
    $fileLink = fopen($file, 'r') or die("Can't open file.");
    $fileContent = fread($fileLink, filesize($file));
    fclose($fileLink);

    $result = array("Data" => array(), "Message" => "");

    if($_GET["uid"] == $fileContent) {
    //if($_GET["uid"]) {
        $result = get_data();
    } else {
        $result["Data"] = "";
        $result["Message"] = "Invalid Request!";
    }

    echo json_encode($result);

    function get_data() {
        $url = "http://sdrtdg.com/player/adunanze/api.xml"; // "http://territori.xyz:8649/api/"; //"http://sdrtdg.com:8088/api/";
        $data = curl_get_file_contents($url);
        if($data["status"]) {
            $xmlstr = $data["data"];
            $XML = new SimpleXMLElement($xmlstr);
            $active = (string)$XML->active;
            $recording = (string)$XML->recording;
            $external = (string)$XML->external;
            $streaming = (string)$XML->streaming;
            $result["Data"] = array("Active" => $active, "Recording" => $recording, "External" => $external, "Streaming" => $streaming);
            $result["Message"] = "Data retrieved!";
        } else {
            $result["Data"] = "";
            $result["Message"] = "Invalid Data!";
        }
        return $result;
    }

    function curl_get_file_contents($URL)
    {
        $agent= 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)';    
        $c = curl_init();
        curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($c, CURLOPT_VERBOSE, true);
        curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($c, CURLOPT_USERAGENT, $agent);
        curl_setopt($c, CURLOPT_URL, $URL);
        $contents = curl_exec($c);
        $logMessage = curl_getinfo($c);

        print_r($logMessage);
        exit;
        curl_close($c);

        $data = array("status" => FALSE, "data" => []);

        if ($contents) {
            $data["status"] = TRUE;
            $data["data"] = $contents;
            return $data;
        }
        else {
            $data["status"] = FALSE;
            return $data;
        }
    }

0 个答案:

没有答案