发布从 swift 到 PHP 的 NSArray


Post an NSArray from swift to PHP

我试图将一个整数数组发布到我的服务器,以便PHP可以使用它。我的问题是它不起作用。我刚开始使用PHP编程,所以我没有太多的知识...

这是我的快速代码:

let url = NSURL(string: myURL)!
    var session: NSURLSession!
    let request = NSMutableURLRequest(URL: url)
    request.HTTPMethod = "POST"
    do {
        dataNotice = try context.executeFetchRequest(fetchRequestForNotice) as! [Notice]
    } catch {
        print(error)
    }
    //create array of existing IDs
    var noticeIDs: [Int] = [Int]()
    for var i = 0; i < dataNotice.count; i++ {
        noticeIDs.append(dataNotice[i].id as! Int)
        print("Notice ID: " + String(dataNotice[i].id))
    }
    let postString = "server='(server)&username='(username)&password='(password)&database='(database)&noticeIDs='(noticeIDs)"
    request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
    let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
    session = NSURLSession(configuration: configuration, delegate: self, delegateQueue: nil)
    let task = session.dataTaskWithRequest(request)
    task.resume()

更新这是我捕获数组的PHP代码:

    <?php
$method = $_POST['method'];
$db_server = $_POST['server'];
$db_benutzer = $_POST['username'];
$db_passwort = $_POST['password'];
$db_name = $_POST['database'];
$applicationNotices = $_POST['noticeIDs'];
$databaseNotices;
$con=mysqli_connect($db_server,$db_benutzer,$db_passwort,$db_name);
if (mysqli_connect_errno())
{
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = "SELECT * FROM `Notice`";
$sqlID = "SELECT `ID` FROM `Notice`";
if ($result = mysqli_query($con, $sql)) {
    if ($resultID = mysqli_query($con, $sqlID)) {
        $resultArray = array();
        $tempArray = array();
        $idArray = array();
        $tempIDArray = array();
        while($rowID = $resultID->fetch_object())
        {
            $tempIDArray = intval($rowID->ID);
            array_push($idArray, $tempIDArray);
        }
        while($row = $result->fetch_object())
        {
            $tempArray = $row;
            array_push($resultArray, $tempArray);
        }
        $neededNotices = array_diff($idArray, $applicationNotices);
        $array = array_map('intval', $neededNotices);
        $array = implode("','",$array);
        $finalSql = "SELECT * FROM `Notice` WHERE `ID` IN ('".$array."')";
        if ($finalResult = mysqli_query($con, $finalSql)) {
            $finalArray = array();
            $tempFinalArray = array();
            while($finalRow = $finalResult->fetch_object())
            {
                $tempFinalArray = $finalRow;
                array_push($finalArray, $tempFinalArray);
            }
            echo json_encode($finalArray);
        }
    }
}
mysqli_close($con);
?>

此代码应获取我的核心数据数据库中的现有 ID,并将其传输到我的服务器,以便将此 id 与我的 sql 数据库的 id 进行比较,并返回当前不在应用程序中的所有条目。

如果我运行代码,则会出现错误:

无法将类型"__NSArray0"(0x110469780) 的值强制转换为"NSMutableArray"(0x11046a978)。

如何将数据正确传输到我的服务器?

谢谢!

好吧,对于一个我不会每次都传递用户名和密码以及主机名,因为这是过多的网络,即使它看起来很小而且不太安全。其次,您将HTTPMethod设置为POST,因此在php代码中,您应该从$_POST而不是请求中检索值。