POST data to a PHP method from Swift

前端 未结 2 829
忘了有多久
忘了有多久 2020-12-16 23:09

I\'m trying to post some info to my PHP file from Swift. My php file is executed, but the posted variables just don\'t get through to the php file. What am I doing wrong?

相关标签:
2条回答
  • 2020-12-16 23:37

    This worked for me.

    Video - https://youtu.be/wYkZ47Rz8iU

    Swift Code - Example

    let request = NSMutableURLRequest(URL: NSURL(string: "http://www.kandidlabs.com/YouTube/SwiftToMySQL/insert.php")!)
            request.HTTPMethod = "POST"
            let postString = "a=\(usernametext.text!)&b=\(password.text!)&c=\(info.text!)&d=\(number.text!)"
            request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
    
            let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
                data, response, error in
    
                if error != nil {
                    print("error=\(error)")
                    return
                }
    
                print("response = \(response)")
    
                let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
                print("responseString = \(responseString)")
            }
            task.resume()
    

    PHP Code - Example

     <?php
        $host='localhost';
        $user='root';
        $password='';
    
        $connection = mysql_connect($host,$user,$password);
    
        $usernmae = $_POST['a'];
        $pass = $_POST['b'];
        $info = $_POST['c'];
        $num = $_POST['d'];
    
        if(!$connection)
        {
            die('Connection Failed');
        }
        else
        {
            $dbconnect = @mysql_select_db('YoutubeTutorialDB', $connection);
    
            if(!$dbconnect)
            {
                die('Could not connect to Database');
            }
            else
            {
                $query = "INSERT INTO `YoutubeTutorialDB`.`Users` (`Username`, `Password`, `Info`, `FavoriteNumber`)
                    VALUES ('$username','$pass','$info','$num');";
                mysql_query($query, $connection) or die(mysql_error());
    
                echo 'Successfully added.';
                echo $query;
            }
        }
    ?>
    
    0 讨论(0)
  • 2020-12-16 23:48

    this is the updated version (swift 4) from the example above

    // the only one that work
    func sendJson(){
        let usernametext = "new person"
        let passwordtext = "nice"
        let request = NSMutableURLRequest(url: NSURL(string: "http://localhost/example/todatabase.php")! as URL)
        request.httpMethod = "POST"
        let postString = "Title=\(usernametext)&content=\(passwordtext)"
        request.httpBody = postString.data(using: String.Encoding.utf8)
    
        let task = URLSession.shared.dataTask(with: request as URLRequest) {
            data, response, error in
    
            if error != nil {
                print("error=\(error)")
                return
            }
    
            print("response = \(response)")
    
            let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
            print("responseString = \(responseString)")
        }
        task.resume()
    }
    

    simply change the variable usernamtext,passwordtext or whatever inside the postString,and the url to your liking. this actually the only one that work i found that many other tutorial does not work.

    0 讨论(0)
提交回复
热议问题