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?
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.