gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 20:49:09 25 4
gpt4 key购买 nike

我的服务器上有一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com