Я хочу отправить данные в текстовом поле через HTTPS на веб-сервер, используя Swift и PHP, и попробовал этот пример Данные POST в метод PHP из Swift , но он не совместим с PHP7.
Теперь это для PHP7 и Swift 4, но я получил пустую запись в базе данных MySql.
Я думаю, что это PHP-файл ...
Использовать Xcode 10.1. Есть ли решение?
Swift:
import UIKit
class MessageViewController: UIViewController {
@IBOutlet weak var nachricht: UITextField!
@IBAction func submit(_ sender: Any) {
let url = NSURL(string: "http://localhost.com") // localhost MAMP - change to point to your database server
var request = URLRequest(url: url! as URL)
request.httpMethod = "POST"
var dataString = "secretWord=???" // starting POST string with a secretWord
// the POST string has entries separated by &
dataString = dataString + "&nachricht=\(nachricht.text!)" // add items as name and value
// convert the post string to utf8 format
let dataD = dataString.data(using: .utf8) // convert to utf8 string
do {
// the upload task, uploadJob, is defined here
let uploadJob = URLSession.shared.uploadTask(with: request, from: dataD) { data, response, error in
if error != nil {
// display an alert if there is an error inside the DispatchQueue.main.async
DispatchQueue.main.async {
let alert = UIAlertController(title: "Upload Didn't Work?", message: "Looks like the connection to the server didn't work. Do you have Internet access?", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
}
} else {
if let unwrappedData = data {
let returnedData = NSString(data: unwrappedData, encoding: String.Encoding.utf8.rawValue) // Response from web server hosting the database
if returnedData == "1" { // insert into database worked
// display an alert if no error and database insert worked (return = 1) inside the DispatchQueue.main.async
DispatchQueue.main.async {
let alert = UIAlertController(title: "Upload OK?", message: "Looks like the upload and insert into the database worked.", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
}
} else {
// display an alert if an error and database insert didn't worked (return != 1) inside the DispatchQueue.main.async
DispatchQueue.main.async {
let alert = UIAlertController(title: "Upload Didn't Work", message: "Looks like the insert into the database did not worked.", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}
}
}
}
uploadJob.resume()
}
}
}
Файл PHP:
<?php
$secret = $_POST["secretWord"];
if ("???" != $secret) exit; // note the same secret as the app - could be let out if this check is not required. secretWord is not entered by the user and is used to prevent unauthorized access to the database
$nachricht = $_POST['nachricht'];
// POST items should be checked for bad information before being added to the database.
// Create connection
$mysqli=mysqli_connect("localhost","db","db_pass","db_usr"); // localhost, user name, user password, database name
// Check connection
if (mysqli_connect_errno())
{
echo "
Failed to connect to MySQL: " . mysqli_connect_error();
}
$query = "insert into `db` (nachricht) value ('".$nachricht."')";
$result = mysqli_query($mysqli,$query);
echo $result; // sends 1 if insert worked
?>