i have tried to understand how to connect to a database with an IOS application.
i built an application that connects to a database on my localhost. i wrote a web service in PHP that returns a JSON encoded response, i then wrote the rest of the code in obj C with NSURL etc.
that is app-->web service-->database, right?
but im trying to understand things i read on the internet, like someone mentioning using the REST API, i dont understand what to use REST for, and what REST is really (noob i know).
i understand that REST gives JSON encoded responses, but is it something you use to create a web service ?
so is their someone who can explain to me what REST, web services in PHP, JAVA and JSON etc is in relation to mobile development ? please provide examples, thats the way i understand best.
Let me tell you, for a newbie you are on the right path and have the done it right! Once you get a hang of the basic json request / response webservices, you will yearn for more. Like security, uniform interface etc.
REST is nothing different from what you did. REST is an acronym for REpresentational State Transfer. The key part is statelessness.
REST uses HTTP verbs POST,GET, PUT/PATCH & DELETE to do the basic CRUD (Create, Read, Update and Delete).
Two additional verbs are OPTIONS (to retrieve the list of actions that can be performed) and HEAD (to return headers)
Read this to get a fair idea:
http://restcookbook.com/
http://www.drdobbs.com/web-development/restful-web-services-a-tutorial/240169069
Related
I'm quite new to PHP. As a learning project, I'm currently building a website on which users can order products.
I don't want my website to connect to the database I have directly, but I want that process to go through an API I've made. The API has a quite simple structure: the index.php receives the call and the variables (through post) and then, depending on the type of data received, runs one of the functions in one of it's controllers.
So, the question:
How do I set up a connection between my website and my php api (on the same server) to access my database?
I have searched the web and SO for API connections but most of the questions are about the FB API and oAuth etcetera. If I have missed a similar question please inform me because then I'll delete this question.
Any help would be much appreciated, Thank you in advance!
Sounds like you want to implement a REST API. There are a boatload of tutorials and helpful links that you can find very easily to read up on this subject. (Here is a decent starting point). There are also many, many frameworks that you can use that handle RESTful interactions automatically.
EDIT:
Once you have a REST API setup, the best way to connect and interact with your API in PHP is using the cURL module. This is a good intro to the subject of using cURL in PHP.
The current preferred structure for passing data from API -> client is JSON. PHP makes it trivial to work with JSON. Within your API use json_encode to convert a PHP variable into it's JSON equivalent string. Inside your client, convert the JSON response from your API into a PHP object using the inverse function: json_decode
This is a very well known/widely used technique and there are many more nuances to consider, but this should be a sufficient intro for testing purposes. Once you understand the ideas I strongly recommend doing some google/stackoverflow searches and reading more on the subject.
I'm asking this question because some of the websites is visited seems to be using a RESTful API to access the data even if it's on the website...
For example: my website will have 6 pages and 5 of them use the DB. But, I will also have a REST api for my partners...
So, the question is:
On my website, is it better to access directly the DB via mysqli_query or to use a RESTful API with Ajax calls to load data?
Just a note: I'll be using Zend Framework 2 for my RESTful API except if someone has a better option... I know Node.js and PHP... I'm able to write it in Ruby or something if it's better for me... Need a opinion on that...
Use the RESTful API.
The specification of REST is that we use the HTTP methods, which he calls verbs.(GET, POST, PUT, DELETE).
A direct request would be limiting it, or you would be using at most two method (verbs) - GET and POST.
For that you have to do this:
GET /user/frederick/edit
GET /user/frederick/update
GET /user/frederick/delete
GET /user/new
And with a RESTful API:
GET /user/frederick/
POST /user/new/
PUT /user/frederick/
DELETE /user/frederick/
The advantage of using your own API is that you don't have to write duplicate code. For example, you might have generate_for_rest and generate_for_server functions that do the same thing and just emit data in different formats. It's a good idea to reuse your own APIs as much as you can.
That said, I do find it a bit unusual that a website would communicate to itself with its own RESTful API. That requires an HTTP request (though it should be extremely fast) and conversion of the data twice. Instead it would make more sense to have an API that generates the data that you need and a facade that converts that data into formats for it to be used.
For example you could have a function get_all_users. Internally you can use get_all_users to get the results as php data structures that you can use immediately. In your controller that responds to HTTP requests you may do a JSON conversion, but you shouldn't be doing any duplicate work to get the data for either internal or external use.
This is a Homework task. It involves creating a simple DB and making CURL calls to the server to get results from the DB, accordingly I have a DBClass file with the required methods. I understand what REST architecture is in general, but I am kind of unable to put the pieces together. Here's what I have so far:
Model.class.php -> this is the Database class that instantiates connections to the DB and has methods that execute DB queries and return the result.
Simulator.php -> helper class, simulates HTTP requests (POST or GET only) to the localhost so my curl call is made to 'http://localhost/app/index.php'
index.php -> here is where I receive the CURL requests, in effect, I decode the HTTP requests to understand the request method, parameters, URI as such.
At this point, I am lost. Understandably a RESTful API request essentially is of the kind server/getMeMyBananas and getMeMyBananas is a DB method that looks for bananas for the user and returns the ID's. I am confused how this maps into the index.php file and the missing pieces.
Suggestions and links to awesome resources are most welcome. I am not interested in security or creating a state of the art Web service.
getMeMyBananas is an example of an RPC route
In REST, the four main HTTP verbs GET, POST, PUT and DELETE are the verbs to act upon a noun (a resource).
REST is not a standard. It's a loose recommendation on how to form an API for a remote system using HTTP as its foundation.
There's nothing to say that you can't design RPC-like routes in a REST API. People do it all of the time. It's just that you mainly use the verbs to create (POST), retrieve (GET), update (PUT) or delete (DELETE). That makes the acronym CRUD. So, with REST, you are able to cover a lot of scenarios in information exchange simply by sticking to CRUD.
So, you can start by designing your URLs (routes) to resemble nouns (resources) and build a switch case in PHP to switch on the HTTP verb. Keep in mind, there's nothing stopping from and there's nothing wrong with having RPC-like routes. In fact, you can't handle all cases with the simple REST CRUD scenario, so you'll have to handle cases that don't fit into that scenario with RPC-like routes.
See:
http://dojotoolkit.org/reference-guide/1.8/quickstart/rest.html
Later, if you are interested in a built out API in PHP, I built an API infrastructure and open sourced it. I'm not sure if it'll help you, but here it is:
https://github.com/homer6/blank_altumo
You can map any url to any path you want! For example, when using Apache you can use ModRewrite to turn http://ex.com/rest/bananas into http://ex.com/index.php?p1=rest&p2=bananas
From there you can now you can fetch your request parameters with the global variable get for example: $_GET["p1"].
I would suggest you to perform isset() test on those.
After that when you've got the data, I'd suggest to package it in JSON so almost any client can read it.
That's basically how I'd do it! If you've got more questions go ahead :)
I will be writing an IOS app that will query data from a server. What is the proper method to request and retrieve data in that manner. None of the books I'm reading seem to be covering it. I imagine the server would be using something like PHP and it would have access to the database. Would the iOS app do something like XML-RPC and i write php functions to listen, retrieve, and respond? Is XML-RPC even still the hip solution or is there something more modern I've not used yet?
The iOS app will be ultimately trying to get information from a Firebird database.
Well, if you're creating an iOS 5 app, then you're on luck, you can use restful webservices very easily:
NSString* response = [NSString stringWithContentsOfURL:<url pointing to your server> error:&error];
Then you can use NSJsonSerialization to convert the response into a property list. Also there are convenience methods for parsing XML files.
I would highly suggest looking into ASIHTTPRequest.
It is amazingly documented, Extremely powerful and easy to pick up.
Download the files drag and drop them into your project. Import ASIHTTPRequest.h.
Then you can do all of this
http://allseeing-i.com/ASIHTTPRequest/How-to-use
I figure I should put the disclaimer that the developer no longer supports the project, However, it works amazing and since it is so widely used i'm sure if there is ever a major issue with it in the future someone will take the liberty of fixing it and putting it up on github for all.
You can use various methods like this:
Simple Get or Post Request.
XML SOAP Services.
REST JSON Services.
This depends the data you want to retrieve.
I'm developing an iPhone APP and need to implement also an Web Service.
First of all I'm not a Developer and never made something big in PHP, Objective-C, xCode.
My PHP knowledge isn't also good. But let's start with my Environment.
iPhone APP (xCode 4.2, iOS5), PHP Web Service, MySQL DB
I was researching the WEB and most People tend more to REST than SOAP. I think i see also the advantages of REST (using of simple HTTP Verbs (get, post, delete etc...), but that's not the main point here...
I think I understand the main goal of the REST Architecture and tried to make a little concept with an URI and Verb Mapping. Here just a simple example of the mapping:
/location/{location_id}/product
/location/{location_id}/product/{product_id}
Both are GET operations who should get me ether a single product or all products of a location.
How would a simple PHP REST Web Server look like with these functions?
Another part should implement a User Authentication from the iPhone. Somehow i need to store the user session, right now I don't have any idea how to make that. The goald is that if only a user is logged in, he could review the product.
Now I've researched also the Web but couldn't find an easy step-by-step Tutorial.
Do you know any good Tutorials which will help me achieve my goal? :)
A lot of people prefer using PHP Frameworks like ZEND. This seems very interesting, but it seems like a big package with a lot of modules.
Does someone know exactly which Modules are needed to get my Web Service working?
This is quite a good tutorial, it uses the codeigniter framework which makes the learning curve a bit steeper but makes it a lot more powerful in the long run.
http://net.tutsplus.com/tutorials/php/working-with-restful-services-in-codeigniter-2/
If you want to build this the custom way it's actually very easy to do if you want to return information in the JSON format especially for php5 which is generally well supported amongst hosts these days.
Essentially the steps are like this:
Pass in product id via url and retrieve using GET i.e. service.php?product_id=10
Query database and return data for product id that was passed in
Store returned data in an array
Set header content-type to application/json
json_encode the result (json_encode)
That way when you call that url in a browser you will get a nice JSON formatted array result in a key:value pair manner. And as of iOS5 json parser comes with the framework (for earlier versions SBJson is a good framework to use (SB JSON))