IOS到PHP POST参数? [英] IOS to PHP POST parameters?

查看:461
本文介绍了IOS到PHP POST参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建联系我们ViewController,它可以发送电子邮件,但我不想使用 MFMailComposeViewController 因为我想要设置接收邮件的电子邮件地址。

I'm trying to create 'Contact Us' ViewController, which can send an e-mail , but I don't want to use the MFMailComposeViewController because I want to set the e-mail address which receive the message.

我试图创建一个 viewController ,它从用户那里获取所有消息数据并将其发送到 PHP 页面,该页面将执行其余的进度,因此我尝试使用 GET方法到 PHP 页面,这适用于'name''phone number'等简短参数,但我无法将此方法用于长文本,例如'message content'。所以我决定使用 POST方法,但我找不到可以使用 POST方法将数据从我的应用程序发送到 PHP 的方法

I tried to create a viewController which take the all the message data from the user and send it to a PHP page which will do the rest of progress, so I tried to send the parameters using GET Method to the PHP page, which is good for short parameters such as 'name' or 'phone number', but I couldn't use this method for long text such as 'message content'. So I decided to use POST Method, but I couldn't find a way that could send the data from my app to PHP using POST Method

我正在尝试发送三个带有名称的 NSString (MsgTitle,MsgCity,MsgContent)

I'm trying to send three NSString with the names (MsgTitle, MsgCity, MsgContent)

我的 PHP 代码

<?php
    $MsgTitle = $_POST['title'];
    $MsgCity = $_POST['city'];
    $MsgContent = $_POST['description'];
?>

我希望有一个简单的方法可以做到这一点,并提前感谢你......

I hope there is an easy way to do that, and thank you in advance ...

推荐答案

创建 NSMutableURLRequest 对象,设置 HTTPMethod @POST,将参数放在HTTP正文中作为格式正确的字符串 - 与GET参数相同:

Create an NSMutableURLRequest object, set the HTTPMethod to @"POST", put the parameters in the HTTP body as a properly formatted string — the same as GET parameters:

[request setHTTPBody:@"subject=foo&message=lorem%20ipsum"];

使用 NSURLConnection 发送请求并检查错误:

Send the request using NSURLConnection and check for an error:

NSHTTPURLResponse *urlResponse = nil;
NSError *error = NULL;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];

if (!responseData || urlResponse.statusCode != 200) {
  NSLog(@"error: %@", error); // probably want to also tell the user that the message failed
}

这篇关于IOS到PHP POST参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆