从Swift到php脚本的Http post请求 require("Conn.php"); require("MySQLDao.php"); $name=htmlentities($_POST["name"]); $email=htmlentities($_POST["email"]); $password=htmlentities($_POST["password"]); $returnValue = array(); if(empty($email) || empty($password)) { $returnValue["status"] = "error"; $returnValue["message"] = "Missing required field"; echo json_encode($returnValue); return; } $dao = new MySQLDao(); $dao->openConnection(); $userDetails = $dao->getUserDetails($email); if(!empty($userDetails)) { $returnValue["status"] = "error"; $returnValue["message"] = "User already exists"; echo json_encode($returnValue); return; } $secure_password = md5($password); $result = $dao->registerUser($name, $email, $secure_password); if($result) { $returnValue["status"] = "Success"; $returnValue["message"] = "User is registered"; echo json_encode($returnValue); return; } $dao->closeConnection();
我有一个php脚本名UserRegister.php。在这个脚本中,我有一些Post变量。这些变量来自我的iOS应用程序,然后脚本应该将其保存到我的mysql数据库中从Swift到php脚本的Http post请求 require("Conn.php"); require("MySQLDao.php"); $name=htmlentities($_POST["name"]); $email=htmlentities($_POST["email"]); $password=htmlentities($_POST["password"]); $returnValue = array(); if(empty($email) || empty($password)) { $returnValue["status"] = "error"; $returnValue["message"] = "Missing required field"; echo json_encode($returnValue); return; } $dao = new MySQLDao(); $dao->openConnection(); $userDetails = $dao->getUserDetails($email); if(!empty($userDetails)) { $returnValue["status"] = "error"; $returnValue["message"] = "User already exists"; echo json_encode($returnValue); return; } $secure_password = md5($password); $result = $dao->registerUser($name, $email, $secure_password); if($result) { $returnValue["status"] = "Success"; $returnValue["message"] = "User is registered"; echo json_encode($returnValue); return; } $dao->closeConnection();,php,ios,mysql,swift,http-post,Php,Ios,Mysql,Swift,Http Post,我有一个php脚本名UserRegister.php。在这个脚本中,我有一些Post变量。这些变量来自我的iOS应用程序,然后脚本应该将其保存到我的mysql数据库中 require("Conn.php"); require("MySQLDao.php"); $name=htmlentities($_POST["name"]); $email=htmlentities($_POST["email"]); $password=htmlentities($_POST["password"]);
require("Conn.php");
require("MySQLDao.php");
$name=htmlentities($_POST["name"]);
$email=htmlentities($_POST["email"]);
$password=htmlentities($_POST["password"]);
$returnValue = array();
if(empty($email) || empty($password))
{
$returnValue["status"] = "error";
$returnValue["message"] = "Missing required field";
echo json_encode($returnValue);
return;
}
$dao = new MySQLDao();
$dao->openConnection();
$userDetails = $dao->getUserDetails($email);
if(!empty($userDetails))
{
$returnValue["status"] = "error";
$returnValue["message"] = "User already exists";
echo json_encode($returnValue);
return;
}
$secure_password = md5($password);
$result = $dao->registerUser($name, $email, $secure_password);
if($result)
{
$returnValue["status"] = "Success";
$returnValue["message"] = "User is registered";
echo json_encode($returnValue);
return;
}
$dao->closeConnection();
my.php脚本
require("Conn.php");
require("MySQLDao.php");
$name=htmlentities($_POST["name"]);
$email=htmlentities($_POST["email"]);
$password=htmlentities($_POST["password"]);
$returnValue = array();
if(empty($email) || empty($password))
{
$returnValue["status"] = "error";
$returnValue["message"] = "Missing required field";
echo json_encode($returnValue);
return;
}
$dao = new MySQLDao();
$dao->openConnection();
$userDetails = $dao->getUserDetails($email);
if(!empty($userDetails))
{
$returnValue["status"] = "error";
$returnValue["message"] = "User already exists";
echo json_encode($returnValue);
return;
}
$secure_password = md5($password);
$result = $dao->registerUser($name, $email, $secure_password);
if($result)
{
$returnValue["status"] = "Success";
$returnValue["message"] = "User is registered";
echo json_encode($returnValue);
return;
}
$dao->closeConnection();
我的swift密码
//Store data
//Send user data to server side
let myURL: NSURL! = NSURL(string: "http://localhost:8888/iOSDatabase/UserRegister.php")
let request: NSMutableURLRequest = NSMutableURLRequest(URL: myURL!)
request.HTTPMethod = "POST"
let postString = "name=" + UserName + "&email=" + UserEmail + "&password=" + UserPassword
println(postString)
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
data, response, error in
println("response =\(response)")
if error != nil {
println("error=\(error)")
return
}
var responseString = NSString(data: data, encoding: NSUTF8StringEncoding)
println("responseString =\(responseString)")
var err: NSError?
if var json: NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options: .MutableContainers, error: &err) as? NSDictionary {
if error == nil
{
println(json)
}
}
}
task.resume()
我打印出我的响应,得到状态代码:500,在我的标题中显示Connection=close。它不会将任何JSON文件返回到我的iOS应用程序。您使用PHP的
$\u POST
命令检索变量,但使用Swift中的GET请求发送变量。您可以在Swift中使用request.HTTPMethod=“GET”
更改请求类型,或者在PHP中使用$\u GET
500=内部服务器错误。您需要检查服务器的错误日志以获取详细信息。@MarcB如何或在哪里可以获得此服务器的错误日志?不知道。您必须查看php服务器的配置。如果是LinuxISH,它可能是/var/log/httpd/error\u log
或类似的东西。