我試圖將由swift發佈的設備令牌存儲到未來引用的php變量中(例如檢查令牌是否已存在數據庫)。Swift 2.0 - 將json值從swift存儲到php變量並使用php顯示
這是我的銀行代碼:
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
let sampleToken = deviceToken.hexString()
let session = NSURLSession.sharedSession()
let postBody = NSString(format: "token=\(sampleToken)")
let endBody = NSURL(string: "http://www.samplesite.com/sub1/sub2/testing.php")
let request = NSMutableURLRequest(URL: endBody!, cachePolicy: NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData, timeoutInterval: 30.0)
request.HTTPMethod = "POST";
request.HTTPBody = postBody.dataUsingEncoding(NSUTF8StringEncoding)
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
let dataTask = session.dataTaskWithRequest(request) { (data:NSData?, response:NSURLResponse?, error:NSError?) -> Void in
if data != nil {
print("data: \(response)")
} else {
print("failed: \(error!.localizedDescription)")
}
}
dataTask.resume()
}
這是令牌存儲我的PHP文件:
<?php
$token = $_REQUEST['token'];
var_dump(json_decode($token)); //returns null
var_dump(json_decode($token, true)); //returns null
include_once("includes/dbi_connect.php");
$sql1 = "INSERT INTO tbs_main . userTokenInfo (token) VALUES ('".$token."')";
$result = mysqli_query($conn, $sql1);
?>
什麼讓我困惑的是,該令牌仍然在數據庫中記錄但無法顯示。我在這裏錯過了一些東西,我真的不知道在哪裏和什麼。設備令牌的傳遞和轉換是否有問題?或者它在PHP方面?
UPDATE
我嘗試使用的var_dump( '$令牌');就像彈藥建議,但它只返回空值
我試過使用var_dump,它仍然顯示/返回一個空值。快速部分有問題嗎? – Scar
我也試過** var_dump('$ _ POST'); **並返回**數組(0){} **。所以看來php無法從swift讀取發佈請求 – Scar