How to create webservice using PHP and REST?

Asked

Viewed 1,366 times

5

I have been researching a lot about webservices lately and would like your help to know how I can build a webservice using REST and PHP. I chose PHP because I am more familiar with the language, and REST because during my researches I saw that it was the most used standard today and simpler to do. First of all, I’ve seen this link about webservice which is quite informative by the way: Doubts in the web service , but that did not completely clear my doubt. My doubt actually is what I need to do for my project to be considered a webservice? For example, I created a PHP platform that queries the database and displays this data on the screen (on a php/html page) to the user depending on the request he makes. Is that considered a webservice? What differentiates it 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 you how to make or snippets of code. I really appreciate your cooperation.

1 answer

2

It’s an old question but it’s worth answering. Let’s go in pieces:

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

Usually you use webservices when you want to allow interoperability (basically communication) between different platforms (e.g., windows, linux, Ios) written in different languages. Of course to allow this there must be a message format understood by both (commonly json or xml).

Basically you create an application (with some server-side language, for example 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 (http://www.meusite.com/listarUsuarios), instead of returning an html page, full of tags and css styles, only user data is returned in plain text, commonly json or xml. In json could be 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 (stylization), or how it will be presented to the user in an application. The client application is that would worry about how the consumed web service data would be graphically presented. Of course the creator of the webservice could very well create a client application, in html and javascript, or an app for android. (native or hybrid).

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

No. Because among other things there is no interoperability (it is even possible with a lot of 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.) it would not be easy task to receive an html document (without well defined structure, it would have to look) and search for the relevant information (the data returned by the sql query in the database, without any html formatting) in the middle of various html tags.

You could even have this page that lists the information in html, but it will be an application consuming the 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 snippets of code

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.