php


Passing Data from Swift to PHP for JSON using file_get_contents?


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);
?>
file_get_contents('php://input') will give you a string containing whatever data was posted to PHP. It will not automatically work out what format that data is in, so you'll need to parse the data somehow, e.g. with json_decode().
Remember to check for errors in the JSON formatting, and that all input is untrusted - you don't know for sure that it's your app sending the data, somebody could figure out the URL and send something weird to break your PHP code.
Swift seems to be sending correct data using POST.
let url:NSURL = NSURL(string: url)!
let session = URLSession.shared
let request = NSMutableURLRequest(url: url as URL)
request.httpMethod = "POST"
let paramString = "table_id='1'"
request.httpBody = paramString.data(using: String.Encoding.utf8)
However, when the PHP receives it,
it returns nothing.
$contents = $_REQUEST['table_id'];
$abc = $contents;
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = "SELECT * FROM menu_main_items WHERE table_id='".$abc."'";
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);
}
If I manually type $content="1"; then the data is retrieved.
What do you think the problem seems to be?
// 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);
?>

Related Links

How to move the featured image below the title in Marketify single post
Every ajax query on my website is printing red dot ? what might be wrong?
How can I get the (real) HTTP response status code from a CORS request in AngularJS 1.5?
PHP - JSON empty values in database
Find consecutive repited value in array
Ubuntu Apache PHP - Create and write to an Excel Document
PHP fopen() Cannot Execute Opened Php Extension File
Woocommerce two shipping options at same time
PHP- How to create a text file in network shared folder
not getting response from Mixpanel
input of the form is not updating [duplicate]
How to setup Memcached with failover support in Heroku/PHP 5.6 environment?
Cartesian Product on unknown number of php arrays
How can I repeat IF twice or infinite times in PHP? [duplicate]
How to add extension Add global in twig
PhpStorm: Only Link to Function in included Code

Categories

HOME
events
schema.org
writefile
vsts-build
android-emulator
openflow
stored-procedures
angular2-template
nsstring
orc
pm2
google-cloud-logging
string-formatting
jitsi
workday
coreldraw
liferay-6.2
connection-refused
pymongo
mime-types
amazon-sns
chronicle-queue
sipp
beego
xdebug
android-service
uilocalnotification
fatfs
ng-repeat
sspi
iscroll
right-click
system-on-chip
vb.net-to-c#
doctrine-extensions
kitura
jenkins-slave
sonicmq
openstack-glance
punctuation
freshdesk
clrs
1010
mechanicalturk
rustdoc
oid
yargs
yui3
java-2d
fragmentstatepageradapter
adp
odp.net-managed
mercurial-extension
outlook.com
directx-9
android-studio-import
gql
supersocket.net
fantom
bootstrap-wysiwyg
unidata
flow-js
web-api
dnsjava
relationships
mobile-country-code
squeezebox
rails-api
jchartfx
jomsocial
notifyjs
2d-vector
session-0-isolation
red-system
handshake
trialware
firebird1.5
sigar
fbml
server-variables
nsrangeexception
ccnet-config
iphone-sdk-4.3
cleartype
browser-based
projectpier
eai

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App