PHP: how to get variables from a JSON(sent from a Swift iOS app) and respond with a JSON -
i developing ios app swift should fetch data mysql database according user's location. don't know php , couldn't find resource explains how receive data app.
i have php code:
<?php // create connection $con=mysqli_connect("localhost","*******","*******","*******"); // check connection if (mysqli_connect_errno()) { echo "failed connect mysql: " . mysqli_connect_error(); } // sql statement selects table 'locations' $sql = "select * *******"; // check if there results if ($result = mysqli_query($con, $sql)) { // if so, create results array , temporary 1 // hold data $resultarray = array(); $temparray = array(); // loop through each row in result set while($row = $result->fetch_object()) { // add each row our results array $temparray = $row; array_push($resultarray, $temparray); } // finally, encode array json , output results echo "{ \"posts\": "; echo json_encode($resultarray); echo "}"; } // close connections mysqli_close($con); ?>
as can see when called gets data table , returns json. next step want send location swift app code:
@ibaction func submitaction(sender: anyobject) { //declare parameter dictionary contains string key , value combination. var parameters = ["name": nametextfield.text, "password": passwordtextfield.text] dictionary<string, string> //create url nsurl let url = nsurl(string: "http://myservername.com/api") //change url //create session object var session = nsurlsession.sharedsession() //now create nsmutablerequest object using url object let request = nsmutableurlrequest(url: url!) request.httpmethod = "post" //set http method post var err: nserror? request.httpbody = nsjsonserialization.datawithjsonobject(parameters, options: nil, error: &err) // pass dictionary nsdata object , set request body request.addvalue("application/json", forhttpheaderfield: "content-type") request.addvalue("application/json", forhttpheaderfield: "accept") //create datatask using session object send data server var task = session.datataskwithrequest(request, completionhandler: {data, response, error -> void in println("response: \(response)") var strdata = nsstring(data: data, encoding: nsutf8stringencoding) println("body: \(strdata)") var err: nserror? var json = nsjsonserialization.jsonobjectwithdata(data, options: .mutableleaves, error: &err) as? nsdictionary // did jsonobjectwithdata constructor return error? if so, log error console if(err != nil) { println(err!.localizeddescription) let jsonstr = nsstring(data: data, encoding: nsutf8stringencoding) println("error not parse json: '\(jsonstr)'") } else { // jsonobjectwithdata constructor didn't return error. but, should still // check , make sure json has value using optional binding. if let parsejson = json { // okay, parsedjson here, let's value 'success' out of var success = parsejson["success"] as? int println("succes: \(success)") } else { // woa, okay json object nil, went worng. maybe server isn't running? let jsonstr = nsstring(data: data, encoding: nsutf8stringencoding) println("error not parse json: \(jsonstr)") } } }) task.resume() }
courtesy http://jamesonquave.com/blog/making-a-post-request-in-swift/
and don't know how "get"(accept, function use) json:
{"items": [ { "minlat": "43.000000", "maxlat": "44.000000", "minlon": "-79.000000", "maxlon": "-78.000000", } ] }
from app in order have in php:
$minlat = $json['minlat']; $maxlat = $json['maxlat']; $minlon = $json['minlon']; $maxlon = $json['maxlon']; $sql = "select * ******* latitude between".$minlat." , ".$maxlat." , longitude between ".$minlon." , ".$maxlon;
thank you
the answer incredibly stupid-easy:
firstly nothing worked before commented these 2 lines:
request.addvalue("application/json", forhttpheaderfield: "content--type") request.addvalue("application/json", forhttpheaderfield: "accept")
then used string instead of json send post data(it surely works json also, works @ moment):
let request = nsmutableurlrequest(url:myurl!); request.httpmethod = "post"; let poststring = "minlat=43.0&maxlat=44.0&minlon=26.0&maxlon=27.0"; request.httpbody = poststring.datausingencoding(nsutf8stringencoding);
and @ server side simply:
$minlat = $_request["minlat"]; $maxlat = $_request["maxlat"]; $minlon = $_request["minlat"]; $maxlon = $_request["maxlat"];
:|
Comments
Post a Comment