У меня есть скрипт PHP на сервере
<?php
// retrieve POST vars
$comment = $_POST['comment'];
$email = $_POST['email'];
// remove trailing whitespace etc
$comment = trim($comment);
$email = trim($email);
// format date accoring to http://www.w3schools.com/php/func_date_date.asp
$date_time = date("Y-m-d-H-i-s"); // i.e. 2010-04-15-09-45-23
// prepare message
$to = "(removed my email addy)"; //who to send the mail to
$subject = "Feedback from iPhone app"; //what to put in the subject line
$message = "Name/Email: $email \r\n Comment: $comment";
mail($to, $subject,$message) or "bad";
echo "ok";
?>
Как мне теперь отправить запрос POST из моего приложения для iPhone ...
Я пробовал подобные вещи ...
NSURL *url = [NSURL URLWithString:@"mysite.com/script.php"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
NSString *message = boxForSuggestion.text;
NSString *userEmailString = usersEmail.text;
NSString *requestBodyString = [NSString stringWithFormat:@"comment:%@&email%@", message , userEmailString];
NSData *requestBody = [requestBodyString dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPMethod:@"POST"
[request setHTTPBody:requestBody];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"]; // added from first suggestion
NSURLResponse *response = NULL;
NSError *requestError = NULL;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&requestError];
NSString *responseString = [[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding] autorelease];
NSLog(@"%@", responseString);
Есть идеи? Спасибо, ребята
Sam
EDIT:
я думаю, что с моим телом запроса что-то не так
NSData *requestBody = [requestBodyString dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPMethod:@"POST"
[request setHTTPBody:requestBody];
помните, что мои php-переменные - $ comment и $ email