How to create webservice using PHP and REST?

5

I have been researching a lot on webservices lately and would like your help to know how I can build a webservice using REST and PHP. I chose PHP because I'm more familiar with the language, and REST because during my research I saw that it was the most commonly used and simplest standard to do. First of all, I've seen this link on webservice which is very informative by the way: Questions in web service , but that did not completely heal my doubt. My question is actually what do I need to do for my project to be considered a webservice? For example, I created a platform in PHP that queries the database and shows this data on the screen (on a php / html page) to the user depending on the request he makes. Is this considered a webservice? What sets it apart from a webservice and what can I change in it to make it a webservice? How do I apply REST to this? If possible, leave tutorials that teach how to do or snippets of code. Thank you very much for your cooperation.

    
asked by anonymous 05.04.2017 / 23:40

1 answer

2

It's an old question but it's worth answering. Let's break it down:

  

My question is actually what I need to do for my project to be   considered a webservice?

Generally, you use webservices when you want to allow interoperability (basically communication) between different platforms (for example, windows, linux, ios) written in different languages. Of course to allow this there should be a message format understood by both (commonly json or xml).

Basically you create an application (with some server-side language eg php, asp, ruby, python, etc.) but instead of returning content in html, css, and javascript, you return the content, say "raw", without any formatting. For example, you define a url ( link ), instead of returning an html page, full of css tags and styles, are returned only user data in plain text, usually json or xml. In json it could look something like this: [{nome: 'usuario 1', idade: 20}, {nome: 'usuario 2', idade: 30}] . Instead of returning something like <html><body><table><tr><td>.... .

So when you are creating your webservice you would not be worrying about the presentation (styling), or how it will be presented to the user in an application. The client application would be concerned with how the consumed webservice data would be graphically presented. Of course the creator of webservice could very well create a client application, in html and javascript, or an android app. (native or hybrid).

  

For example, I created a PHP platform that queries the database   and display these data on the screen (on a php / html page) for the   depending on the request it makes. This is considered a   webservice?

No. For among other things there is no interoperability (it is even possible with much effort). If someone wants to create a native android application (written in java) to access this url, and show the content, with native elements of android (ListView, TextView, etc.) would not be easy task to receive an html document (without well defined structure, it would have to look) and look for the relevant information (the data returned by the sql query in the database, without any html formatting) in the middle of several html tags.

You could even have this page that lists the information in html, but it will be an application consuming webservice that returns only the data as it was returned by the sql query.

  

How do I apply REST to this? If possible, leave tutorials that teach   how to make or code snippets

No there is a good tutorial showing how to build the webservice (server side application).

    
13.01.2018 / 22:57