正在将 Swift 连接到 php 服务器
Connecting Swift with php server
我正在尝试 post 从 swift 到 php 然后 return json 编码数据返回 swift。
然而,print(task.response)
为零。我的 link 做了它应该做的,但我的应用程序仍然以 nil 响应。知道为什么吗?
let request = NSMutableURLRequest(URL: NSURL(string: "https://www.flashkase.com/API/createAccount.php")!)
request.HTTPMethod = "POST"
let postString = "username=frogg222"
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in
guard error == nil && data != nil else { // check for fundamental networking error
print("error=\(error)")
return
}
if let httpStatus = response as? NSHTTPURLResponse where httpStatus.statusCode != 200 { // check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(response)")
}
let responseString = String(data: data!, encoding: NSUTF8StringEncoding)
print("responseString = \(responseString)")
}
task.resume()
print(task.response)
还有我的 PHP 文件
<?php
include($_SERVER["DOCUMENT_ROOT"] . "/inc/database.php");
$arr = [];
$arr[] = 'Reporting for duty';
if($_SERVER['REQUEST_METHOD'] == 'POST'){
$arr[] = 'Post received';
$arr[] = $_POST['username'];
}
echo json_encode($arr);
?>
更新:完成后如何查看数据?如果我取消对 sleep(3) 的注释,它将显示结果...但这显然不理想。
func postToServer(postURL: String, postString: String) {
let request = NSMutableURLRequest(URL: NSURL(string: postURL)!)
request.HTTPMethod = "POST"
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request){
data, response, error in
print("\(data), \(response), \(error)")
}
task.resume()
//sleep(3)
}
let sendURL: String = "https://www.flashkase.com/API/createAccount.php"
let sendData: String = "username=frogg222&email=fake123@gmail.com&password=fake1234"
postToServer(sendURL,postString: sendData)
dataTaskWithRequest
是一个异步操作。在任务开始后立即打印响应代码显然会打印 nil,因为它还没有时间获取值。完成处理程序已经有一个可以使用的 response
对象,因此在您的完成处理程序中,调用 print(response)
查看属性并尝试获取响应代码。
我正在尝试 post 从 swift 到 php 然后 return json 编码数据返回 swift。
然而,print(task.response)
为零。我的 link 做了它应该做的,但我的应用程序仍然以 nil 响应。知道为什么吗?
let request = NSMutableURLRequest(URL: NSURL(string: "https://www.flashkase.com/API/createAccount.php")!)
request.HTTPMethod = "POST"
let postString = "username=frogg222"
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in
guard error == nil && data != nil else { // check for fundamental networking error
print("error=\(error)")
return
}
if let httpStatus = response as? NSHTTPURLResponse where httpStatus.statusCode != 200 { // check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(response)")
}
let responseString = String(data: data!, encoding: NSUTF8StringEncoding)
print("responseString = \(responseString)")
}
task.resume()
print(task.response)
还有我的 PHP 文件
<?php
include($_SERVER["DOCUMENT_ROOT"] . "/inc/database.php");
$arr = [];
$arr[] = 'Reporting for duty';
if($_SERVER['REQUEST_METHOD'] == 'POST'){
$arr[] = 'Post received';
$arr[] = $_POST['username'];
}
echo json_encode($arr);
?>
更新:完成后如何查看数据?如果我取消对 sleep(3) 的注释,它将显示结果...但这显然不理想。
func postToServer(postURL: String, postString: String) {
let request = NSMutableURLRequest(URL: NSURL(string: postURL)!)
request.HTTPMethod = "POST"
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request){
data, response, error in
print("\(data), \(response), \(error)")
}
task.resume()
//sleep(3)
}
let sendURL: String = "https://www.flashkase.com/API/createAccount.php"
let sendData: String = "username=frogg222&email=fake123@gmail.com&password=fake1234"
postToServer(sendURL,postString: sendData)
dataTaskWithRequest
是一个异步操作。在任务开始后立即打印响应代码显然会打印 nil,因为它还没有时间获取值。完成处理程序已经有一个可以使用的 response
对象,因此在您的完成处理程序中,调用 print(response)
查看属性并尝试获取响应代码。