doujian7132 2015-09-15 21:05
浏览 51

Http从Swift发送请求到PHP脚本

I have a php script name UserRegister.php. In this script I have some Post variables. These variable comes from my iOS app and then the script should save it to my mysql database.

my .php script

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 swift code

//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()

I print out my response and get status code: 500 and in my header it says Connection = close. And it doesn't return any of the JSON file to my iOS app.

  • 写回答

1条回答 默认 最新

  • douba6365 2016-02-02 22:18
    关注

    You are retrieving the variables using PHP's $_POST command but sending them using a GET request in Swift. You can change the request type in Swift using request.HTTPMethod = "GET" or alternatively use $_GET in PHP.

    评论

报告相同问题?

悬赏问题

  • ¥15 gwas 分析-数据质控之过滤稀有突变中出现的问题
  • ¥15 没有注册类 (异常来自 HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
  • ¥15 知识蒸馏实战博客问题
  • ¥15 用PLC设计纸袋糊底机送料系统
  • ¥15 simulink仿真中dtc控制永磁同步电机如何控制开关频率
  • ¥15 用C语言输入方程怎么
  • ¥15 网站显示不安全连接问题
  • ¥15 51单片机显示器问题
  • ¥20 关于#qt#的问题:Qt代码的移植问题
  • ¥50 求图像处理的matlab方案