myS中的jSonString到PHP

时间:2012-10-10 06:59:48

标签: php mysql json

我从iOs设备发送一个jsonString

看起来像这样:

2012-10-10 08:50:32.011 Appname[4049:c07] Post String =http://www.yourdomain.nl/locatie.php?data=%7B%22id%22:%220612833397%22,%22longitude%22:%22-143.406417%22,%22latitude%22:%2232.785834%22,%22timestamp%22:%2210-10%2007:56%22%7D

那是我NSLog的时候......

所以PHP文件看起来像这样:

<?php

$id = $_POST['id'];
$longitude = $_POST['longitude'];
$latitude = $_POST['latitude'];
$timestamp = $_POST['stringFromDate'];



$link = mysql_connect('server', 'sbla', 'bla')
or die('Could not connect: ' . mysql_error());

mysql_select_db('md267052db227433') or die('Could not select database');

// Performing SQL query
$query = "INSERT INTO locatie (id, longitude, latitude, timestamp) VALUES (NULL," .   $longitude . "," . $latitude . "," .$timestamp." )";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
echo "OK";


// Free resultset
mysql_free_result($result);

// Closing connection
mysql_close($link);
?>

当我去yourdomain.nl/locatie.php时,它说它是空的,但那应该是因为我从我的iOs应用程序发送数据。

我想要jSonstring,我可以看到id,经度,纬度和时间戳,并把它们放在我的mySQL数据库中,但奇怪的是它没有工作。

我知道来自iOs设备的字符串已被发送。

抱歉我的英文不好,

任何帮助都将不胜感激。

修改

像这样发送:

- (void)myFuntionThatWritesToDatabaseInBackgroundWithLatitude:(NSString *)latitude longitude:(NSString *)longitude date:

(NSString *)stringFromDate {


_phonenumber = [[NSUserDefaults standardUserDefaults] objectForKey:@"phoneNumber"];

NSMutableString *postString = [NSMutableString stringWithString:kPostURL];
NSString*jsonString = [[NSString alloc] initWithFormat:@"{\"id\":\"%@\",\"longitude\":\"%@\",\"latitude\":\"%@\",\"timestamp\":\"%@\"}",_phonenumber, longitude , latitude, stringFromDate];

[postString appendString:[NSString stringWithFormat:@"?data=%@", jsonString]];
[postString setString:[postString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:postString]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];


[[NSURLConnection alloc] initWithRequest:request delegate:self ];
NSLog(@"Post String =%@", postString);


//    LocationTestViewController*locationTestViewController = [[LocationTestViewController alloc]init];
//    phonenumber = locationTestViewController.telefoonnummer;
NSLog(@"telnr : %@", _phonenumber);

NSURLResponse* response;
NSHTTPURLResponse* httpResponse;
NSError* error;
NSData* responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString* stringResponse = [[NSString alloc] initWithData:responseData encoding:NSASCIIStringEncoding];

httpResponse = (NSHTTPURLResponse*) response;
int statuscode = [httpResponse statusCode];

if (statuscode == 200)
{
    NSLog(@"Verstuurd");
    // Handle the response here if needed
}
else
{
    NSLog(@"niet verstuurd: %@", stringResponse);
    // Show some form of alert here if needed
}
// release all objects saved to memory
[request release];
request = nil;
[stringResponse release];
stringResponse = nil;

}

4 个答案:

答案 0 :(得分:1)

首先,这不是JSON,它的POST FORMDATA可能。其次,当您打开链接(使用浏览器)时 - 它变为GET请求,而不是POST。尝试从iOS设备执行请求时记录$ _POST文件。像这样(PHP文件的第一行):

file_put_contents('dump.txt', "POST: \n" . print_r($_POST, true) . "\n\n\n GET: \n" . print_r($_GET, true));

然后执行多个请求以在同一目录中的“dump.txt”中查看结果。

此外,查询应该如下所示(如果我猜对了你的colums):

$query = sprintf("INSERT INTO locatie (longitude, latitude, timestamp) VALUES ('%s', '%s', '%d')", mysql_real_escape_string($longitude), mysql_real_escape_string($latitude), (int)$timestamp)  ;

PHP手册不鼓励使用mysql_real_escape_string - 更多内容如下:

http://php.net/manual/en/function.mysql-real-escape-string.php

修改

我的错误,所以POSTed数据实际上是JSON字符串 - 试试这个:

if (isset($_POST['data'])) {
    $data = json_decode($_POST['data']);
    $latitude = $data['latitude'];
    $longitude = $data['longitude'];
    $timestamp = $data['stringFromDate'];
    // rest of your code
}

答案 1 :(得分:0)

您执行GET请求而没有POST。因此,您必须使用$_GET而非$_POST获取数据 您还需要先输入json_decode字符串才能从中获取值 您也可以使用GET发送所有值而不使用JSON。为此,您只需发送多个GET参数,例如http://example.org/index.php?key1=parameter1&key2=parameter2

答案 2 :(得分:0)

试试这样。

<?php

$id = $_POST['id'];
$longitude = $_POST['longitude'];
$latitude = $_POST['latitude'];
$timestamp = $_POST['stringFromDate'];

$datarray['id']=$id;
$datarray['longitude']=$longitude;
$datarray['latitude']=$latitude ;
$datarray['stringFromDate']=$timestamp;

$link = mysql_connect('db.babboemedia.nl', 'md267052db227433', 't7cjxOm3')
or die('Could not connect: ' . mysql_error());

mysql_select_db('md267052db227433') or die('Could not select database');

// Performing SQL query
$query = "INSERT INTO locatie (id, longitude, latitude, timestamp) VALUES (NULL," .   $longitude . "," . $latitude . "," .$timestamp." )";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());


// Free resultset
mysql_free_result($result);



if($result)
{
    $datarray['status']="OK";
}
else
{
    $datarray['stringFromDate']="NOTOK";
}

// Closing connection
mysql_close($link);

header('Content-Type: application/json');
json_encode($datarray);

?>

答案 3 :(得分:-1)

您需要检查$_POST['data']然后删除斜杠。然后解析JSON。

<?php
$json=json_decode(stripslashes($_POST['data']));
//print_r($json);
$id = $json['id'];
$longitude = $json['longitude'];
$latitude = $json['latitude'];
$timestamp = $json['stringFromDate'];
//the rest of your code goes here