我是Swift 3的新手,我試圖將一些數據傳遞給使用PHP的服務器。我確定這兩種語言都是初學者。使用file_get_contents從Swift向JSON傳遞數據?
我正在嘗試從SWIFT接收數據到PHP。
但是,我意識到在PHP中使用$ _POST或$ _REQUEST對JSON不起作用。相反,我聽說有必要使用'file_get_contents'或'PHP:// input'。
所以我認真地花了很多時間弄清楚如何使它工作。
以下是PHP端的代碼。但是,它似乎只適用於原始數據,而不適用於JSON編碼。
有沒有人認爲下面的代碼是不正確的從SWIFT接收數據?
<?php
header("Content-Type: application/json");
$con=mysqli_connect("localhost","123","123!!","123");
$menu_main_item_store_id = file_get_contents('php://input');
$menu_main_item_store_id_received = $menu_main_item_store_id;
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = "SELECT * FROM menu_main_items WHERE menu_main_item_store_id=$menu_main_item_store_id_received AND menu_main_item_in_use_check = 'yes'";
if ($result = mysqli_query($con, $sql)) {
$resultArray = array();
$tempArray = array();
while($row = $result->fetch_object()){
$tempArray = $row;
array_push($resultArray, $tempArray);
}
echo json_encode($resultArray);
}
mysqli_close($con);
?>
請將您的相關代碼發佈到代碼中,而不是代碼的屏幕截圖。 – segFault
謝謝。我把代碼放在裏面。:) – Jay