我对objective-c 相当陌生,并且希望使用POST 将一些键值对传递给PHP 脚本。我正在使用以下代码,但数据似乎没有被发布。我也尝试通过使用 NSData 发送东西,但似乎都没有工作。
NSDictionary* data = [NSDictionary dictionaryWithObjectsAndKeys:
@"bob", @"sender",
@"aaron", @"rcpt",
@"hi there", @"message",
nil];
NSURL *url = [NSURL URLWithString:@"http://myserver.com/script.php"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[NSData dataWithBytes:data length:[data count]]];
NSURLResponse *response;
NSError *err;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSLog(@"responseData: %@", content);
这将被发送到这个简单的脚本以执行数据库插入:
<?php $sender = $_POST['sender'];
$rcpt = $_POST['rcpt'];
$message = $_POST['message'];
//script variables
include ("vars.php");
$con = mysql_connect($host, $user, $pass);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("mydb", $con);
mysql_query("INSERT INTO php_test (SENDER, RCPT, MESSAGE)
VALUES ($sender, $rcpt, $message)");
echo "complete"
?>
有任何想法吗?