本文介绍了从iOS到php服务器的POST数据总是为NULL - iOS 10,Swift 3,php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在实现一个FCM应用服务器,并希望将设备注册令牌ID存储到我自己的数据库中(用PHP编写的服务器)。i am implementing a FCM app server and wanted to store the device registration token id into my own database (Server written in PHP).我意识到数据在设备ID令牌总是 null ,有人能指出正确的方式将令牌存储到数据库吗?I realise the data in device id token is always null, Can someone point the right way to store the token accordingly into database?真的很感激也许有人可以指导我关于这个问题,谢谢!Really appreciated perhaps someone could guide me on this issue, thanks!请参考下面的代码/截图: - Kindly refer the code/screenshots below:- AppDelegate。 swift func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { var token = "" for i in 0..<deviceToken.count { token += String(format: "%02.2hhx", arguments: [deviceToken[i]]) } print("Registration succeeded!") print("Token: ", token) Callquery(token)} AppDelegate.swift(Cal lquery方法,向服务器端脚本发送POST请求) func Callquery(_ token: String){ // append parameter to oneDictionary let tokenString = ["token": token] as [String: Any] // create the request var request = URLRequest(url: URL(string:"https://YourURL.com/admin/registerToken.php")!) // set the method as POST request.httpMethod = "POST" // append the paramter to body request.httpBody = try! JSONSerialization.data(withJSONObject: tokenString, options: []) // create the session URLSession.shared.dataTask(with:request, completionHandler: {(data, response, error) in if error != nil { print("There was error during datatask session") print(error) } else { do { guard let json = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String: Any] else { return } guard let errors = json?["errors"] as? [[String: Any]] else { return } if errors.count > 0 { // show error print("There is an error during parse JSON datatask") return } else { // show confirmation print("datatask with JSON format performed successfully") } } } print(request) }).resume()} 服务端脚本(registerToken.php): <?phpinclude 'config.php';$token = $_POST['token'];$sql = "INSERT INTO users (email,device_id) VALUES ('email','$token')";mysqli_query($conn, $sql);mysqli_close($conn);?> 使用真实设备运行应用程序日志如下: 数据库用户表(设备ID始终没有任何内容): - 用户表格结构: - tokenString: - 推荐答案我用GET方法解决了这个问题od而不是POST方法如下: - I had solved this issue by using GET method instead of POST method as below:- Appdelegate.swift: var request = URLRequest(url: URL(string:"https://YourURL.com/admin/registerToken.php?token=\(token)")!) 服务器端脚本: $token = $_GET['token']; 这篇关于从iOS到php服务器的POST数据总是为NULL - iOS 10,Swift 3,php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
11-01 10:41