1
我正在使用下面的代碼從我們的服務提供商爲LBS繪製數據。我想要做的也是加載這些信息,我收到到MySQL。插入PHP和XML到MYSQL
使用此代碼我沒有收到任何錯誤,位置和地圖API工作良好,但它並沒有將數據繪製到MySQL中。
<?php
function parseXML($fstr,$tstr_start,$tstr_end) {
if (! strstr($fstr,$tstr_start) || !strstr($fstr,$tstr_end)){
return ;
}
$start = strpos($fstr,$tstr_start) + strlen($tstr_start);
$stop = strpos($fstr,$tstr_end, $start);
$length = $stop - $start;
return trim(substr($fstr, $start, $length));
}
define ("MAP_API_KEY","***");
define ("MAP_BASE_URL","http://maps.googleapis.com/maps/api/staticmap?");
$Data = $GLOBALS["HTTP_RAW_POST_DATA"];
//--- Get XML data into variables
$DataAry['ResponseType'] = parseXML($Data,'Response Type="','"');
$DataAry['RefNo'] = parseXML($Data,'RefNo="','"');
$DataAry['SeqNo'] = parseXML($Data,'SeqNo="','"');
$DataAry['Network'] = parseXML($Data,'<NetworkID>','</NetworkID>');
$DataAry['Lat'] = (int) parseXML($Data,'<Lat>','</Lat>');
$DataAry['Lon'] = (int) parseXML($Data,'<Lon>','</Lon>');
$DataAry['Accuracy'] = parseXML($Data,'<Accuracy>','</Accuracy>');
$DataAry['DateTime'] = parseXML($Data,'<DateTime>','</DateTime>');
$DataAry["Lat"] = $DataAry['Lat']/1000000;
$DataAry["Lon"] = $DataAry["Lon"]/1000000;
$con = mysql_connect("localhost","****","****");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("****", $con);
$sql="INSERT INTO trace (Lat, Lon, DateTime, RefNo)
VALUES
('$_POST[Lat]','$_POST[Lon]','$_POST[DateTime]','$_POST[RefNo]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
// ---Create link to map
$link = MAP_BASE_URL . "center={$DataAry['Lat']},
{$DataAry['Lon']}&zoom=15&size=500x500&key=" . MAP_API_KEY . "&sensor=true&markers=
{$DataAry['Lat']},{$DataAry['Lon']}";
$handle = fopen("requests.php","c");
fwrite($handle, "<br/>Location Request @ ". date("Y-m-d H:i:s") . "<br/><textarea
style='width:500px;height:200px' readonly>" . $GLOBALS["HTTP_RAW_POST_DATA"] . "
</textarea><br/><img style='border:1px solid #000;width:500px;height:500px'
src='$link'/><hr/>");
fclose ($handle);
echo "done!";
?>
但它確實扔到requests.php頁的要求,但我需要提取信息到mSQL的
[**請不要在新代碼中使用'mysql_ *'函數**](http://bit.ly/phpmsql)。他們不再被維護[並被正式棄用](https://wiki.php.net/rfc/mysql_deprecation)。看到[**紅框**](http://j.mp/Te9zIL)?學習[*準備的語句*](http://j.mp/T9hLWi),並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli) - [這篇文章](http://j.mp/QEx8IB)將幫助你決定哪個。如果你選擇PDO,[這裏是一個很好的教程](http://j.mp/PoWehJ)。 –
您在插入查詢中使用$ _POST將其更改爲$ DataAry –
您可能想在某人決定嘗試使用它之前從代碼中刪除該地圖API密鑰。在公共網站上發佈私鑰並不是一個好主意。 – SDC