gpt4 book ai didi

php - 来自swift的JSON请求在php中显示空数组

转载 作者:行者123 更新时间:2023-12-04 10:53:51 25 4
gpt4 key购买 nike

我正在尝试从我的 swift 应用程序向 PHP 服务器发出请求。出于某种原因,php 将一个空数组显示为 $_REQUEST 变量。我已经查看了堆栈溢出并实现了我能找到的所有可能有帮助的东西,但仍然在 php 中得到一个空数组。这是相关的快速代码...

func connect(_ pin: String, completion: @escaping(Result<ConnectResponse?, Error>) -> ()) {
let params: [String : Any] = [
"mobile_pin_connect": pin,
"device_info": UIDevice().model,
"additional_info": UIDevice().systemVersion
]
doRequest(params: params) { (data) in
if let data = data {
do {
let res = try JSONDecoder().decode(Dictionary<String, String>.self, from: data)
completion(.success(
ConnectResponse(success: (res["success"] == "true"), connect_id: res["connect_id"] ?? nil, error: res["error"] ?? nil)))
} catch {
completion(.failure(error))
}
} else {
print("in else block")
}
}
}

fileprivate func doRequest(params: [String: Any], completion: @escaping (Data?) -> ()) {
let body = createJsonBody(params)!
self.request.httpBody = body
print("Sending request with thw following variables")
print(String(data: body, encoding: .utf8)!)
print(String(data: self.request.httpBody!, encoding: .utf8))
URLSession.shared.dataTask(with: self.request) { (data, response, error) in
if let error = error {
print("Error in request: \(error)")
completion(nil)
}
let stringResult = String(data: data!, encoding: .utf8)!
let properResult = String(stringResult.map {
$0 == "." ? "=" : $0
})
let decodedData = Data(base64Encoded: properResult)
completion(decodedData)
}.resume()
}

fileprivate func createJsonBody(_ params: [String: Any]) -> Data? {
do {
let jsonData = try JSONSerialization.data(withJSONObject: params)
let body = Data(jsonData).base64EncodedData()
return body
} catch {
print("Unable to create json body: " + error.localizedDescription, error)
return nil
}
}

将请求发送到服务器,请求的设置在静态 var 设置中...
private static var sharedConnector: ApiConnector = {
let url = URL(string: "https://mywebsiteURLhere.com/api/mobile/challenge")
var request = URLRequest(url: url!)
request.httpMethod = "POST"
request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
let connector = ApiConnector(request)
return connector
}()

所以我为 application/json 设置了正确的 header 值,我将请求方法设置为 post,我对 json 数据进行 base64encoding,在 PHP 中我设置了获取 php://input...

$rawRequest = file_get_contents("php://input");

并将 $_REQUEST 变量转储到错误日志,但我总是得到数组\n(\n)\n
它只是显示一个空数组
我什至做了
error_log("Raw request from index.php");
error_log(print_r($rawRequest, true));

它记录了一个完全空的行。
我无法弄清楚为什么 PHP 在请求中什么都没有,从我在网上看到的所有内容来看,我都在 swift 中正确地执行了请求。任何帮助都非常感谢。谢谢

最佳答案

根据您的 Swift 代码,您能否替换以下方法。

fileprivate func createJsonBody(_ params: [String: Any]) -> Data? {
do {

let jsonData = try JSONSerialization.data(withJSONObject: params)
let body = Data(jsonData)
return body
} catch {
print("Unable to create json body: " + error.localizedDescription, error)
return nil
}
}

您需要替换此行 let body = Data(jsonData)let body = Data(jsonData).base64EncodedData()

关于php - 来自swift的JSON请求在php中显示空数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59329179/

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