检查XML输出是否有效

时间:2017-08-24 14:28:45

标签: php xml google-maps-api-3

自从一周以来,我一直坚持这个错误,我已经用Google搜索并尝试了所有可能的解决方案,包括Stackoverflow上的解决方案。这是我的代码:

<?php
require("phpsqlsearch_dbinfo.php");
// Get parameters from URL
$center_lat = $_GET["lat"];
$center_lng = $_GET["lng"];
$radius = $_GET["radius"];
// Start XML file, create parent node
$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node);
// Opens a connection to a mySQL server
$connection=mysqli_connect ("localhost", $username, $password);
if (!$connection) {
    die("Not connected : " . mysqli_error());
}
// Set the active mySQL database
$db_selected = mysqli_select_db($connection, $database);
if (!$db_selected) {
    die ("Can\'t use db : " . mysqli_error($connection));
}
// Search the rows in the markers table
$query = sprintf("SELECT id, name, address, lat, lng, ( 3959 * acos( cos( 
  radians('%s') ) * cos( radians( lat ) ) * cos( radians( lng ) - 
  radians('%s') ) + sin( radians('%s') ) * sin( radians( lat ) ) ) ) AS 
  distance FROM markers HAVING distance < '%s' ORDER BY distance LIMIT 0 , 
  20",
  mysqli_real_escape_string($connection, $center_lat),
  mysqli_real_escape_string($connection, $center_lng),
  mysqli_real_escape_string($connection, $center_lat),
  mysqli_real_escape_string($connection, $radius));
$result = mysqli_query($connection, $query);
$result = mysqli_query($connection, $query);
if (!$result) {
    die("Invalid query: " . mysqli_error($connection));
}
header("Content-type: text/xml");
// Iterate through the rows, adding XML nodes for each
while ($row = @mysqli_fetch_assoc($result)){
    $node = $dom->createElement("marker");
    $newnode = $parnode->appendChild($node);
    $newnode->setAttribute("id", $row['id']);
    $newnode->setAttribute("name", $row['name']);
    $newnode->setAttribute("address", $row['address']);
    $newnode->setAttribute("lat", $row['lat']);
    $newnode->setAttribute("lng", $row['lng']);
    $newnode->setAttribute("distance", $row['distance']);
}
echo $dom->saveXML();
?>

我的目标是将XML输出到浏览器以确保它正常工作,但我一直在收到这些错误:

enter image description here 有关详细信息,请参阅:https://developers.google.com/maps/solutions/store-locator/clothing-store-locator 有人可以帮我吗???

2 个答案:

答案 0 :(得分:0)

实际上没有足够的方法来解决你的问题。您可以使用以下内容确保获得所需的输入。

$required = ['lat', 'lng'];
$missing = array_diff($required, $_GET);
if ( ! empty($missing) ) {
    $missingString = implode(',', $missing);
    die("missing {$missingString}");
}

使用XML,开头

&lt;?xml version =“1.0”encoding =“UTF-8”?&gt;

之前没有空格。你的PHP代码在echo输出XML之前输出空格。

答案 1 :(得分:-1)

这是一条浏览器消息,而不是PHP error_log消息。

在XML之前输出了一些东西。很可能是PHP警告或通知。压制垃圾!

要获得最佳的错误记录体验,请将error_reporting设置为-1,关闭display_errors,然后设置自定义error_log。然后在终端中输入tail -f /path/to/error_log。您的通知,警告和错误现在将实时滚动过去,而不会扭曲您的网页显示。