joethemow joethemow - 1 month ago 8
iOS Question

Nothing is getting posted to my php script

I have a php script on my server and I am trying to post data from my ios app, but when I print the post variables I get a nil. Any idea why that is?

Swift

let url: NSURL = NSURL(string: "https://xxxxxx.com/myFile.php")!
let request:NSMutableURLRequest = NSMutableURLRequest(url: url as URL)
let bodyData = "username=\(username.text)&password=\(password.text)"
request.httpMethod = "POST"
request.httpBody = bodyData.data(using: String.Encoding.utf8);
request.setValue("application/x-www-form-urlencoded; charset=utf-8", forHTTPHeaderField: "Content-Type")
NSURLConnection.sendAsynchronousRequest(request as URLRequest, queue: OperationQueue.main)
{
(response, data, error) in
let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
//
print("response \(responseString)")

}


PHP

print_r($_POST); //prints: ([username] => xxxxxx [password] => yyyyyy)
json_encode($postVarUser);
json_encode($postVarPass);

Answer

May be it is the problem of echo($postUser); just use json_encode($postUser);

Comments