PHP Webservice tutorial [closed]
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 9 years ago.
Improve this questionI know PHP - I have developed websites in PHP - I also know basic apache configuration. I have also read up on RESTful webservices. I have learnt how to write a PHP-CLI program which consumes a webservice.
However, I have never developed a webservice. And am trying to find a good tutorial which explains how to write a simple webservice in PHP & also how to host it on Apache. Most tutorials I have googled seem to use all kinds of frameworks.
I am just trying to write something simple like
call http://mysite/addup. if you call it with 2 and 4 as 2 parameters, it will add them up and send a 6 back as response.
Is something like this not possible without any additional frameworks. If it is possible, can someone point me to a tutorial.
If not, what's the simplest framework I can use for doing something & a tutorial which explains the same.
I am not looking at SOAP or WSDLs or XML. Just basic stuff.
A web service is exactly like a php application, but instead of returning html, you return data for client consumption.
Let's say you have a AddNumbers service which takes two parameters.
AddNumbers.php?num1=25&num2=30
in AddNumbers.php, you check if num1 and num2 are numeric and if they are, you just print the value of num1 + num2.
Then, in the caller of the webservice, you read the returned data and that's it.
However, for interoperability, you might want to format your data with json, xml or whatever, since it might be easier when you have complicated datasets returned from the service. In a real exemple, you might as well use posts instead of gets.
In a real example, in your webservice, you might it a database, fetch some rows and return them in a particular format, insert data in a table or just about anything you can imagine.
If you want to have a restful api, you might want to use mod-rewrite directives in a .htdocs file in the directory on your webserver.
精彩评论