How to send data from iOS to MySql database in swift 4 - php

I tried to google it but all the tutorials are in swift 3 or below. All I know is that you have to use a JSONEncoder in swift and have to send the JSON data from swift.
Thanks for the help.

Go through this link to know how to install Alamofire.
How to install the Alamofire 4.0 in Xcode 8.0
Use this code to send your data to server.
// https://www.yourAPIHostedWebsite.com/endpoints/
let url = "http://www.example.com/index.html?name=santosh&country=nepal"
Alamofire.request(url, method: .get)
.validate(statusCode: 200..<300)
.responseJSON { response in
print(response)
}.resume()
Now you need a code in your server to store datas. Here a very simple code which gets value. I hope you know how to store data to Mysql database on server side. If not this link might be helpful to start.
<?php
$name = $_GET["name"];
$country = $_GET["country"];
if($name || $country) {
// Now store these values to your database.
}
?>

Related

Display JSON data from POST request in PHP

I have an apache2 server and php file to receive json via POST in my php code like this
$json = json_decode(file_get_contents('php://input'), true);
$mac_ = $json["code"];
$userId_ = $json["msg"];
and saving it to mysql. This work when I send post via python's requests but not when I'm recieving data from API android device. - When I look at the apache log file, there is a connection, so I'm assuming that some data are received, but because I can't touch that API android device I'm not sure if this part $json["code"] has a right parameter. I would like to know a way to save somewhere all recieved data from this part
$json = json_decode(file_get_contents('php://input'), true);
because I can't check it by echo or so

Ionic 2 Post Request with php codeigniter api

I am using ionic 2 Http.post method with php API. My problem is i am getting Disallowed Key Characters. error on client side. While i figured out in postman that this error arrives when i select form-data option from postman it says "Disallowed Key Characters." in response.
As well as when i select x-www-form-urlencoded option i get the response and my info updated successfully in mySQL server.
Here is my .ts file code:
let headers = new Headers();
headers.append('Access-Control-Allow-Origin:','*');
headers.append('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
let postData = {
FirstName:"testname",
LastName:"testlastname",
PhoneNumber:"12345",
EmailAddress:"test#gmail.com",
InsertedBy:"21"
};
this.http.post('http://localhost:889/api/user/users.json', JSON.stringify(postData),options)
.subscribe((data) => {
//var data = res.json();
console.log(data);
}
I think there is a problem while passing header. Somebody guide me where i am doing wrong?
Here is a screenshot of error:
This api works fine when selecting x-www-form-urlencoded with postman:

Talking to PHP get commands from Android app

I have an android app and am trying to send data to the PHP on the server. The server gets the php data with
$this->get('uname');
$this->get('pass');
We do use codeigniter if that matters
The Java code, inside of an Async method, I currently have is
InputStream response = null;
URLConnection connection = new URL(urls[0]).openConnection();
connection.setRequestProperty("uname" , Username);
connection.setRequestProperty("pass", Password);
response = connection.getInputStream();
When I run this, the code always returns null but it is supposed to return a JSON array either way. I have checked the URL and it is correct. What am I doing wrong? Thanks!
You code should be like this
For your android side ,as mentioned by #Ichigo Kurosaki at Sending POST data in Android
For Codeigniter side , cosider you function name is user_login
function user_login(){
$uname = $this->input->get_post('uname'); //get_post will work for both type of GET/POST request
$pass = $this->input->get_post('pass');
$result=$this->authenticate->actLogin($uname,$pass ); //considering your authenticating user and returning 1,0 array as success/failure
$this->output
->set_content_type('application/json')
->set_output(json_encode($result));
exit;
}

How can I sending json data using Spring RestTemplate to PHP server

I am developing an android application and i using spring RestTemplate to get data from and post data to a PHP server. I tried to post data to PHP server, but PHP server got nothing. How can I post json data using Spring RestTemplate to PHP server and get the json from PHP server?
I solved the problem by myself. I add FormHttpMessageConverter to RestTemplate like this:
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new FormHttpMessageConverter());
And then I can post json data to PHP server. I got the json data in PHP server using:
$jsonData = $_POST['json'];
I fixed with
MultiValueMap<String, ArrayList<persona>> parts = new LinkedMultiValueMap<>();
parts.add("json", aaper);
restTemplate.postForLocation(url,parts);

Get response of MySql query

I have an algorithm about chat program in android, but I have a problem in server-side section.
I can store my data like username, password and e-mail through json into my database from my app but I do not know that How can I check them into my app! (e.g Get the response of username checking query into my app.)
Thanks in advance.
Basically when you run httpClient.execute it will return a response, you need to use that response.
Client side:
HttpResponse resp = httpClient.execute( post );
DataInputStream is = new DataInputStream( resp.getEntity().getContent() );
Server side depends on what programming language you use. For example using python:
self.response.headers['Content-Type'] = 'text/vnd.aexp.json.resp'
self.response.set_status( 200,"OK" )
self.response.out.write( json.dumps( responseList ) )
See this example for full source code and details:
http://mylifewithandroid.blogspot.fi/2010/10/client-server-communication-with-json.html
EDIT
Check this for php server side:
http://www.happycode.info/php-json-response/

Categories