php - 将 POST 发送到服务器上的 PHP 脚本

标签 php iphone post nsurlconnection

我的服务器上有一个 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 变量是 $comment 和 $email

最佳答案

尝试添加:

[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 

关于php - 将 POST 发送到服务器上的 PHP 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2636564/

相关文章:

iphone - 初始加载后使用 CGContextStrokePath 重绘路径

api - 我收到错误 AccessRules : Account does not have the right to perform the operation when I am using postman to hit the register api of ejabberd

php - 从 php 文件访问样式表

php - array_diff() 删除了太多元素

ios - 如何在 iOS 设备连接到配件时使用 Xcode 调试器?

Java Rest Service POST 对 InnerClasses 属性存在分歧

javascript - Ajax POST XMLHttpRequest() 无法发送带有 & 字符的参数

php - 图像无法正确保存

php - 连接 2 个 BASH 变量会产生奇怪的值

ios - 在没有开发者计划计划的情况下,我是否需要 iOS 9 或更高版本才能在 iPhone 上测试应用程序?