简体   繁体   English

POST 数据从 iOS 到 php 服务器始终为 NULL - iOS 10、Swift 3、php

[英]POST data from iOS to php server always NULL - iOS 10, Swift 3, 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).我正在实现一个 FCM 应用服务器,并希望将设备注册令牌 ID 存储到我自己的数据库(用 PHP 编写的服务器)中。

I realise the data in device id token is always null , Can someone point the right way to store the token accordingly into database?我意识到设备 ID 令牌中的数据始终为null ,有人可以指出将令牌相应地存储到数据库中的正确方法吗?

Really appreciated perhaps someone could guide me on this issue, thanks!真的很感激,也许有人可以指导我解决这个问题,谢谢!

Kindly refer the code/screenshots below:-请参考以下代码/屏幕截图:-

AppDelegate.swift 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 (Callquery method which send a POST request to server side script) 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()
}

Service Side Script(registerToken.php):服务端脚本(registerToken.php):

<?php
include 'config.php';

$token = $_POST['token'];

$sql = "INSERT INTO users (email,device_id) VALUES ('email','$token')";

mysqli_query($conn, $sql);



mysqli_close($conn);
?>

Running App with real devices log as below:-使用真实设备运行应用程序日志如下:- 在此处输入图片说明

Database users table (device id always has nothing):-数据库用户表(设备 id 总是什么都没有):- 在此处输入图片说明

users table structure:-用户表结构:- 在此处输入图片说明

tokenString :-令牌字符串:- 在此处输入图片说明

I had solved this issue by using GET method instead of POST method as below:-我已经通过使用 GET 方法而不是 POST 方法解决了这个问题,如下所示:-

Appdelegate.swift : Appdelegate.swift :

var request = URLRequest(url: URL(string:"https://YourURL.com/admin/registerToken.php?token=\(token)")!)

Server side script :服务器端脚本:

$token = $_GET['token'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM