如何从 Swift JSONEncoder 向 PHP 文件发送 POST 请求

我遇到了一点问题。我有一个从文本输入获取值的结构设置:


struct CheckoutData: Codable {

    var firstName: String

    var lastName: String

    var email: String

    var streetAddress: String

    var streetAddress2: String

    var city: String

    var state: String

    var zipCode: String

    var total: Double

}

然后我初始化它并对其进行编码:


let checkoutData = CheckoutData(

    firstName: firstName.trimmingCharacters(in: .whitespacesAndNewlines),

    lastName: lastName.trimmingCharacters(in: .whitespacesAndNewlines),

    email: email.trimmingCharacters(in: .whitespacesAndNewlines),

    streetAddress: streetAddress.trimmingCharacters(in: .whitespacesAndNewlines),

    streetAddress2: streetAddress2.trimmingCharacters(in: .whitespacesAndNewlines),

    city: city.trimmingCharacters(in: .whitespacesAndNewlines),

    state: state.trimmingCharacters(in: .whitespacesAndNewlines),

    zipCode: zipCode.trimmingCharacters(in: .whitespacesAndNewlines),

    total: total

)


let encoder = JSONEncoder()

encoder.outputFormatting = .prettyPrinted


let encoded = try! encoder.encode(checkoutData)


var request = URLRequest(url: URL(string: "https://www.MyApp.com/file.php")!)

request.setValue("application/json", forHTTPHeaderField: "Content-Type")

request.httpMethod = "POST"

request.httpBody = encoded

URLSession.shared.dataTask(with: request) { data, response, error in }.resume()

这是处理发送电子邮件的 PHP:


$emailto = "myemail@gmail.com";

$subject = "Mobile Order";


$firstName = $_POST["firstName"];

$lastName = $_POST["lastName"];

$email = $_POST["email"];

$streetAddress = $_POST["streetAddress"];

$streetAddress2 = $_POST["streetAddress2"];

$city = $_POST["city"];

$state = $_POST["state"];

$zipCode = $_POST["zipCode"];

$total = $_POST["total"];



然而,当我收到电子邮件时,所有变量都是空白的。我已经打印了编码版本,它似乎编码正确,因此它很可能位于 PHP 端。抱歉,这有点代码转储,但我无法弄清楚这一点。非常感谢!


慕哥9229398
浏览 96回答 1
1回答

明月笑刀无情

以下是通过远程服务器上的 PHP 文件发送电子邮件的示例。// View controller //import UIKitclass HomeViewController: UIViewController {&nbsp; &nbsp; @IBAction func sendTapped(_ sender: UIButton) {&nbsp; &nbsp; &nbsp; &nbsp; DispatchQueue.global().async() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var request = URLRequest(url: URL(string: "https://www.MyApp.com/file.php")!)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; request.httpMethod = "POST"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let to = "tom123@apple.com"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let sub = "Just testing..."&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let msg = "How are you doing?"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let from = "George H. Aniston <ghaniston@gmail.com>"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let postString = "a=\(to)&b=\(sub)&c=\(msg)&d=\(from)"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; request.httpBody = postString.data(using: .utf8)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let task = URLSession.shared.dataTask(with: request) { data, response, error in&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; guard let data = data, error == nil else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("error=\(String(describing: error))")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("statusCode should be 200, but is \(httpStatus.statusCode)")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("response = \(String(describing: response))")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let responseString = String(data: data, encoding: .utf8)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("responseString = \(String(describing: responseString))")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; task.resume()&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}// file.php //<?php$to = $_POST['a'];$sub = $_POST['b'];$msg = $_POST['c'];$from = $_POST['d'];// use wordwrap() if lines are longer than 70 characters$msg = wordwrap($msg,70);$headers = 'From: '.$from."\r\n".'Reply-To: '.$from."\r\n" .'X-Mailer: PHP/' . phpversion();// send emailmail($to, $sub, $msg, $headers);?>
打开App,查看更多内容
随时随地看视频慕课网APP