Reputation: 5494
i have a website, written in PHP with a MySQL Database. In order to learn how to program an iPhone and Android App i want to create a webservice that can be used by the iOS and Android App.
How do i continue? The functions to access the MySQL database are in place, because they are used for the site today - but what are the next steps to make these functions/classes usable for my webservice and therefore for the Apps? What are my options? OAuth? Node.js? Anyone knows great tutorials?
Thanks.
Upvotes: 0
Views: 1297
Reputation: 8572
For requesting information from the device to your web site's API, I'd recommend using GET or POST:
GET
http://www.example.com/api/v1/getUsers?username=User*
POST
http://www.example.com/api/v1/getUsers
Post the following information to the above URL:
username = User*
Regarding responses from the web server, I'd recommend outputting information in either JSON or XML. Android and iOS both have built-in XML parsing libraries, and there are 3rd-party libraries to parse JSON as well. They may even be built-in these days.
My recommendation would be to support both JSON and XML, so you can use whichever method best fits the constraints of the development environment you're working with:
JSON
{
'success':True,
'users': [
{'id': 1, 'username': 'User1'},
{'id': 2, 'username': 'User2'},
{'id': 3, 'username': 'User3'}
]
}
XML (utilizing attributes)
<response>
<success>1</success>
<users>
<user id="1" username="User1" />
<user id="2" username="User2" />
<user id="3" username="User3" />
</users>
</response>
XML (using only tags)
<response>
<success>1</success>
<users>
<user>
<id>1</id>
<username>User1</username>
</user>
<user>
<id>2</id>
<username>User2</username>
</user>
<user>
<id>3</id>
<username>User3</username>
</user>
</users>
</response>
In PHP, you can output JSON in the following manner:
<?php
$response = array(
'success': True,
'users': array(
array('id' => 1, 'username' => 'User1'),
array('id' => 2, 'username' => 'User2'),
array('id' => 3, 'username' => 'User3')
)
)
print json_encode($response);
Outputting XML is a little more complicated depending on whether or not you want to utilize tag attributes or not...
Upvotes: 1
Reputation: 152
What you want here is an API that your apps can access. You can define these in any way you like, but some ways are more common than others, for instance RESTful imlpementations.
Using rest you can define access points for your data on a domain using 4 verbs: GET, POST, UPDATE and DELETE. You probably recognize two of these.
Using a library such as Slim Framework for php you can define access points like so:
self::$Slim->post('/document', '\MyApp\DocumentController::newDocument');
// Get all documents for authenticated user
self::$Slim->get('/document', '\MyApp\DocumentController::getAllDocuments');
In this example the web service will accept either post or get requests to /document, where get means "get all documents" and post means "create new". These functions are static, so that you don't have to instantiate all your controllers for any given case.
In either case, the result is issued using JSON. Both iOS and Android can interpret JSON using built in functions or third party libraries. Personally i like AFNetworking for iOS.
So in short, create www.yourdomain.com/api, and add entry points like /api/document using Slim Framework and have these points of entry do your logic. Respond using JSON, for instance with json_encode() in php. On the app side use AFNetworking to make requests and interpret the JSON data.
Again, these are my preferences, there are other ways as well.
Best of luck!
Upvotes: 0