0

Say I have a php file on my web server which contains a class 'Database'. Inside that class is a public function, lets call it 'data_select', which queries MySql database:

public function data_select($query) {
...
return $rows;

How would I pass in the '$query' parameter to this function from a Swift iOS application?

There are a number of answers online regarding $_POST from swift, but these often concern SQL queries such as 'INSERT INTO ...', whereas my case is a 'SELECT...' query. I also cannot seem to find anything regarding triggering class functions from Swift, although I know it must be possible.

Have I got the wrong idea here? I understand how to pull data from a php file on web server when the sql query and everything is included in the same file, but here the parameters of the query can differ, thus I have the basic SELECT query as part of a database class which can be called with different parameters on my test website, but I cannot work out how to pass in the query parameter from swift and trigger the function.

I guess I could trigger the functions by making a call to a different php file containing an instance of the function with the $query parameter passed in from swift. But would I then need to create multiple php files for each different 'SELECT...' query which sort of defeats the point of having the functions in a class???

Chris Wright
  • 89
  • 2
  • 8

1 Answers1

0

PHP is scripting language, mainly used as a server side web language. Swift can communicate with any server side language that uses HTTP. You can use any of the HTTP verbs such as GET, POST, PUT etc to send and receive data from PHP using Swift.

So you'd want to create an endpoint with PHP. You can make an NSMutableURLRequestin swift to send data as a URL parameter.

Take a look at this link to make a GET request in swift.

And take a look at this link to handle a GET request in PHP.

For the multiple files part of your question, you could put many of these endpoints all within one file but that is an implementation and design choice that depends on how you want your application and server to flow.

Community
  • 1
  • 1
Jonathan Eustace
  • 2,469
  • 12
  • 31
  • 54
  • Hey, yeah figured this out since I posted the question by making a single php endpoint file and GETting sql query parameters from the http. Next question I guess is how safe is it to be GETting parameters for a SELECT query in this endpoint from http? If I am adding the parameters into a predefined sql query, is all good? (e.g. "SELECT * FROM $parameter_1" Or is it still open to malicious injection? (I'm assuming it is...) – Chris Wright Apr 24 '16 at 23:52
  • You shouldn't be allowing direct queries to be executed from any HTTP request coming from Swift. It's a major security issue. I strongly suggest you perform major input validation on the server side. Fortunately you have PHP on your side as your middle man between the iOS device and your database. – Jonathan Eustace Apr 24 '16 at 23:55
  • I understand that, so the SELECT query would be constructed in the php endpoint file from GETting HTTP parameters that have been passed in from Swift. Is that right? – Chris Wright Apr 25 '16 at 00:09
  • You hit the nail on the head. :) Just make sure you escape all your inputs. Remember sanitize, sanitize, sanitize. – Jonathan Eustace Apr 25 '16 at 00:10