为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
慕哥9229398 2023-09-08 16:44:45
我遇到了一点问题。我有一个从文本输入获取值的结构设置: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 = .prettyPrintedlet 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 = encodedURLSession.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 端。抱歉,这有点代码转储,但我无法弄清楚这一点。非常感谢!
查看完整描述

1 回答

?
明月笑刀无情

TA贡献1828条经验 获得超4个赞

以下是通过远程服务器上的 PHP 文件发送电子邮件的示例。


// View controller //

import UIKit


class HomeViewController: UIViewController {

    @IBAction func sendTapped(_ sender: UIButton) {

        DispatchQueue.global().async() {

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

            request.httpMethod = "POST"

            let to = "tom123@apple.com"

            let sub = "Just testing..."

            let msg = "How are you doing?"

            let from = "George H. Aniston <ghaniston@gmail.com>"

            let postString = "a=\(to)&b=\(sub)&c=\(msg)&d=\(from)"

            request.httpBody = postString.data(using: .utf8)

            let task = URLSession.shared.dataTask(with: request) { data, response, error in

                guard let data = data, error == nil else {

                    print("error=\(String(describing: error))")

                    return

                }

                

                if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {

                    print("statusCode should be 200, but is \(httpStatus.statusCode)")

                    print("response = \(String(describing: response))")

                }

                

                let responseString = String(data: data, encoding: .utf8)

                print("responseString = \(String(describing: responseString))")

            }

            task.resume()

        }

    }

}


// 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 email

mail($to, $sub, $msg, $headers);

?>



查看完整回答
反对 回复 2023-09-08
  • 1 回答
  • 0 关注
  • 95 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信