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

Laravel filtering parts of a resource route by declaring it twice
Understanding the basics on PHP and MySQL security [closed]
Changing a language then redirecting back
PHP: CURL - Trying to hit website with &not in the URL
PhantomJS scraping through a proxy with a php wrapper
How to show sub category inside parent category for woo commerce products
php : header redirect failing to redirect
Why is the PHP localeconv() returning an empty array on Windows 7?
Magento Fatal error: Call to a member function addData()
Finding links on a page which are on same domain as the page
Getting an ordered ics file from google Calendar
php ltrim() only remove whole words
syntax of query sql in php
CakePHP Model Validation
How do I get information from an unknown number of checkboxes
Remove extra iframe tag added by laravel 4

Categories

HOME
python-2.7
macos-sierra
ontouchlistener
whmcs
typeerror
magento-2.0
informatica
celery
elisp
swift2
phpmqtt
minecraft
rangy
vimeo
jquery-select2-4
django-rq
global-variables
user-defined-types
checkout
susy-sass
nose
weex
candlestick-chart
substring
tortoisemerge
python-appium
strophe.js
sap-lumira
network-analysis
react-bootstrap-table
basic-authentication
winsock
objectmapper
instructions
reporting
markov-chains
adminer
apptentive
amazon-kms
xvfb
ms-dos
unification
widestring
rdp
export-to-pdf
oracle-bmcs
axis
webalizer
ilrepack
extjs4.1
android-sharing
watir-webdriver
dtd
emf-compare
perl-data-structures
bpms
deviare
jenkins-workflow
static-methods
node-glob
android-async-http
swiffy
lovefield
openshift-cartridge
gluon-desktop
hmisc
unhandled
org-babel
phpredis
tree-structure
directorysearcher
famo.us
relationships
git-reset
miglayout
webaii
ivalueconverter
meteorite
application-server
eager-loading
web-frameworks
cmath
wndproc
msinfo32
websphere-6.1
multidrop-bus
server-variables
net-use
canonicalization
django-pagination
pyjamas
android-2.1-eclair
mongrel
multiple-users
fgetc
asdoc
source-code-protection
server-load
acceleration
webclient.uploaddata

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App