I am wondering in the ios app, how to get php echo response after posting to the php script using swift
Now, when i print the response out in the ios app, it shows lots of info, such as status code,header,content type, date. But i want neither of them
All i want is the ios app can return the php echo string <?php echo 'You have successfully registered';?>
SWIFT CODE
let myUrl = URL(string: "http://www.swiftdeveloperblog.com/http-post-example-script/");
var request = URLRequest(url:myUrl!)
request.httpMethod = "POST"// Compose a query string
let postString = "firstName=James&lastName=Bond";
request.httpBody = postString.data(using: String.Encoding.utf8);
let task = URLSession.shared.dataTask(with: request) { (data: Data?, response: URLResponse?, error: Error?) in
if error != nil
{
print("error=\(error)")
return
}
// You can print out response object
print("response = \(response)")
//Let's convert response sent from a server side script to a NSDictionary object:
do {
let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
if let parseJSON = json {
// Now we can access value of First Name by its key
let firstNameValue = parseJSON["firstName"] as? String
print("firstNameValue: \(firstNameValue)")
}
} catch {
print(error)
}
}
task.resume()
PHP CODE
<?php echo 'You have successfully registered';?>