I am new to Swift 3 and I am trying to pass some data to server using PHP. I am a beginner in both languages to be sure.
I am trying to receive a data from SWIFT to PHP.
However, I realized that with $_POST or $_REQUEST in PHP would not work for JSON. Instead, I heard that it is necessary to use 'file_get_contents' or 'PHP://input'.
So I seriously spent a lot of hours on figuring out how to make it work.
The following is the code from the PHP side. However, it seems to be working only with the Raw Data, not with JSON encode.
Does anyone think the code below is incorrect to receive the data from 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);
?>
// You Can try this code
<?php
define('HOST','localhost');
define('USER','DatabaseUserName');
define('PASS','Password');
define('DB','DataBaseName');
$con = mysqli_connect(HOST,USER,PASS,DB);
$sql = "select * from DATABAS_TABLE_NAME";
$res = mysqli_query($con,$sql);
$result = array();
while($row = mysqli_fetch_array($res)){
array_push($result,
array('Username'=>$row[0],
'Cell'=>$row[1],
));
}
echo json_encode(array("result"=>$result));
mysqli_close($con);
?>