9

I get a lot of clients asking me about making mobile apps that connect to their websites to retrieve data, allow users to login, etc. Most of them have PHP-based sites, but don't have any clue about making APIs to interface with them. They ask me why I can't just connect directly to their SQL databases. I don't think that's a good thing to do from a mobile app. I would prefer they have some sort of API in place.

As far as PHP-based sites go, what are the best options when it comes to implementing an API for this purpose?

8
  • Tell them there's 6 bajillion phones out there on 3 gazillion mobile OS variants, and there aren't database drivers for every phone/os variant. Commented Jul 7, 2011 at 19:02
  • 1
    Also, it's probably a bad idea to hand out your database credentials to the entire world, which is what you're doing if you allow an app to directly run SQL queries. Commented Jul 7, 2011 at 19:16
  • @Marc $.getJSON("http://example.com/db", {query: "SELECT * FROM tags"}, function(res) { }); Super secure AND you get to write SQL in UI code which is great for maintainability. Commented Jul 7, 2011 at 19:17
  • @Nokikov: Yes, but that still requires a server-side script to pass the query to the database. And how about {query: DROP TABLE tags}? You do not EVER expose a database like that and allow direct query execution. It's about as safe as holding a gun to your head and trusting no one'll pull the trigger. Commented Jul 7, 2011 at 19:20
  • @Marc B - I think @Novikov was being ironic. If not... Wtf? Queries from ajax? Are you serious? Commented Jul 7, 2011 at 20:20

7 Answers 7

4

Just make an api subdomain with a single controller, and a method for each specific task - output the data in json, and you're set:

http://api.theirsite.com/get-users http://api.theirsite.com/get-news 

etc...

Sign up to request clarification or add additional context in comments.

1 Comment

Correct, Making the PHP query the database and wrap it up as a JSON document is very easy.
2

You want to look into RESTful web services. Have a look at the wiki for this here. Your client's need to essentially build PHP applications that serve the underlying data of their websites through some REST-compliant data type e.g. JSON, XML, SOAP, etc. There are a number of in-built PHP functions that enable the quick conversion of PHP data structures into these formats. This would enable you to build mobile apps that make HTTP requests to get data which it can then display in it's own unique way.

An example for a JSON powered service could be as follows:

$action = $_GET['action']; switch($action) { case 'get-newest-products': echo json_encode(getNewestProducts()); break; case 'get-best-products': echo json_encode(getBestProducts()); break; . . . default: echo json_encode(array()); break; } function getNewestProducts($limit = 10) { $rs = mysql_query("SELECT * FROM products ORDER BY created DESC LIMIT $limit"); $products = array(); if (mysql_num_rows($rs) > 0) { while ($obj = mysql_fetch_object($rs)) { $products[] $obj; } } return $products; } function getBestProducts($limit = 10) { $rs = mysql_query("SELECT * FROM products ORDER BY likes DESC LIMIT $limit"); $products = array(); if (mysql_num_rows($rs) > 0) { while ($obj = mysql_fetch_object($rs)) { $products[] $obj; } } return $products; } 

You could query the API as follows (with mod_rewrite on) http://myapi.mywebsite.com/get-newest-products

Comments

1

REST is a very good way of structuring resources for retrieval. Creating a REST API is a very useful way to define the set of operations that can be performed on your data, while maintaining a relatively thin layer of abstraction between the API and the data in the database. That said, it's not exactly trivial, but it does force one to consider the usages and security concerns, and is very extensible and future-proof.

Comments

0

Well, I have seen a lot of websites either on Drupal, Joomla or Wordpress. If that is the case, those have a API module which you could enable and then build an Android app using those.

Comments

0

We faced this problem often enough that we introduced a simple Java servlet using XQUERY as layer between our own format and the target site, stored primarly in Databases. It converts that data into JSON via REST services. That could make us flexible and fast for every new system like Joomla, Wordpress, etc... It workes since many years for us. Adapting a new system last usally a day. You can use Stylus-Enterprise to design the Xquery files with an UI. If you want to stick to PHP, Luracast Restler does a good job, indeed.

Comments

0

Use Php Slim Micro Framework for creating REST APi . Its very easy to setup and write the code.

thanks

1 Comment

Please provide details.
-2

Instead of apps suggest them to use their mobile site and a wrapper just to open a browser and redirect them to the mobile site.

1 Comment

umm, no. do not pass go. do not collect $200.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.