2010-04-14 100 views
2

我有一个PHP脚本的服务器发送POST到一个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"; 
?> 

上我现在该如何从我的iPhone应用程序发送POST请求在...

香港专业教育学院试过这种的东西...

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); 

任何想法?谢谢你们

山姆

编辑:

我觉得有什么不对我的请求主体

NSData *requestBody = [requestBodyString dataUsingEncoding:NSUTF8StringEncoding]; 
[request setHTTPMethod:@"POST" 
[request setHTTPBody:requestBody]; 

记得我的PHP变量$评论和$电子邮件

+0

我不知道Objective C,也没有任何iPhone API,但是您应该设置正确的MIME类型(application/x-www-form-urlencoded)。 POST数据应该基本上是URL编码的:'key = value&etc' – Thorarin 2010-04-14 10:30:07

+0

我该怎么做? Thorarin可能不知道...其他人? – 2010-04-14 10:39:04

回答

3

试添加:

[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"]; 

后:

[request setHTTPBody:requestBody]; 

在脚本

编辑:

因为我没有可用的SDK,我不能测试它的你,但你可以试试这个?:

NSURL *url = [NSURL URLWithString:@"mysite.com/script.php"]; 
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 

NSString *requestBodyString = [NSString stringWithFormat:@"comment=%@&email=%@", message , userEmailString]; 
NSData *requestBody = [NSData dataWithBytes:[requestBodyString UTF8String]length:[requestBodyString length]]; 

[request setHTTPMethod:@"POST" 
[request setHTTPBody:requestBody]; 
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"]; // added from first suggestion 
+0

我从脚本中得到了一个确定....但变量是不正确的......我编辑了这个问题 – 2010-04-14 11:02:54

+0

尝试在我的文章中编辑^^ – RJD22 2010-04-14 11:19:38

+1

工作是一样的 - 但仍然没有正确地通过变量 然后我试过这个 NSString * requestBodyString = [NSString stringWithFormat:@“comment =%@&email =%@”,message,userEmailString]; 注意=取代:.....似乎解决了问题! YAY! – 2010-04-14 11:50:17

1

尝试使用 @"comment=%@&email=%@"代替@"comment:%@&email%@"

相关问题