从iOS到php服务器的POST数据总是为NULL – iOS 10,Swift 3,php
我正在实现一个FCM应用服务器,并希望将设备注册令牌ID存储到我自己的数据库中(用PHP编写的服务器)。
我意识到设备ID令牌中的数据总是为空 ,有人能指出正确的方式将令牌存储到数据库中吗?
真的很感激也许有人可以指导我这个问题,谢谢!
请参考下面的代码/截图: –
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(向服务器端脚本发送POST请求的Callquery方法)
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):
使用真实设备运行应用程序日志如下: –
数据库用户表(设备ID总是什么都没有): –
用户表结构: –
tokenString: –
我通过使用GET方法而不是POST方法解决了这个问题,如下所示: –
Appdelegate.swift:
var request = URLRequest(url: URL(string:"https://YourURL.com/admin/registerToken.php?token=\(token)")!)
服务器端脚本:
$token = $_GET['token'];