Creating a web service in PHP
I developed a class that is the PHP native SoapServer class' REST equivalent.
You just include the RestServer.php file and then use it as follows.
class Hello
{
public static function sayHello($name)
{
return "Hello, " . $name;
}
}
$rest = new RestServer(Hello);
$rest->handle();
Then you can make calls from another language like this:
http://myserver.com/path/to/api?method=sayHello&name=World
(Note that it doesn't matter what order the params are provided in the query string. Also, the param key names as well as the method name are case-insensitive.)
Related videos on Youtube
Tariq
Technical Architect specializing in Cloud architecture and development.
Updated on June 04, 2022Comments
-
Tariq almost 2 years
I would like to create a web service in PHP which can be consumed by different consumers (Web page, Android device, iOS device).
I come from a Microsoft background so am confortable in how I would do it in C# etc. Ideally I would like to be able to provide a REST service which can send JSON.
Can you let me know how I can achieve this in PHP?
Thanks
Tariq
-
Tariq about 12 yearsThanks. That is a good starter for me. I started looking at frameworks and came across FRAPI but this does not seem to be actively developed. I will take a look at CakePHP
-
Jake Sankey over 11 yearsThanks! I recently posted the RestServer class on GitHub and made some further improvements! (The link above will take you to my blog post about it with the GitHub link)
-
Mr. Lance E Sloan almost 11 yearsLooks great. I'll have to give that a try. I wonder why neither the PHP standard library nor PEAR have a class to support REST.