0

我从 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 应用程序发送数据。

我希望我可以看到 id、经度、纬度和时间戳的 jSonstring 并将它们放入 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

4 回答 4

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”中的结果。

此外,查询应该是这样的(如果我猜对了你的列):

$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

编辑:

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

if (isset($_POST['data'])) {
    $data = json_decode($_POST['data']);
    $latitude = $data['latitude'];
    $longitude = $data['longitude'];
    $timestamp = $data['stringFromDate'];
    // rest of your code
}
于 2012-10-10T07:06:35.167 回答
0

你做了一个 GET 请求而不是 POST。因此,您必须使用$_GET而不是使用$_POST.
您还需要首先json_decode从字符串中获取值。
您还可以使用 GET 而不使用 JSON 发送所有值。为此,您只需发送多个 GET 参数,例如http://example.org/index.php?key1=parameter1&key2=parameter2.

于 2012-10-10T07:02:28.570 回答
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);

?>
于 2012-10-10T07:10:25.823 回答
-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
于 2012-10-10T07:09:25.590 回答