这是我得到的json字符串:
[data] => {"id":"0612833398","longitude":"-143.406417","latitude":"32.785834","timestamp":"10-10 07:56"}
我需要它把它像这样放在我的 php 文件中,但我只得到 NULL 值。
<?php
file_put_contents('dump.txt', "POST: \n" . print_r($_POST, true) . "\n\n\n GET: \n" . print_r($_GET, true));
$link = mysql_connect('server', 'bla', 'bla')
or die('Could not connect: ' . mysql_error());
mysql_select_db('md267052db227433') or die('Could not select database');
// Performing SQL query
if (isset($_POST['data'])) {
$data = json_decode($_POST['data']);
$id = $data['id'];
$latitude = $data['latitude'];
$longitude = $data['longitude'];
$timestamp = $data['timestamp'];
}
$query = sprintf("INSERT INTO locatie (id, longitude, latitude, timestamp) VALUES ('%s', '%s', '%s', '%s')", mysql_real_escape_string($id), mysql_real_escape_string($longitude), mysql_real_escape_string($latitude), mysql_real_escape_string($timestamp)) ;
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
echo "OK";
// Free resultset
mysql_free_result($result);
// Closing connection
mysql_close($link);
?>
我觉得我真的很接近:第 1 行的 dump.txt 看起来像这样:
POST: 数组 ( )
GET:
Array
(
[data] => {"id":"0612833398","longitude":"-143.406417","latitude":"32.785834","timestamp":"10-10 07:56"}
)