I introduced ASP.NET Web API in some of my previous posts. Although Web API is easy to use in ASP.NET web applications you can use Web API also from other platforms. This post shows you how to consume ASP.NET Web API from PHP scripts.
Here are my previous posts about Web API:
- How content negotiation works?
- ASP.NET Web API: Extending content negotiation with new formats
- Query string based content formatting
Although these posts cover content negotiation they give you some idea about how Web API works.
On Web API side I use the same sample application as in previous Web API posts – very primitive web application to manage contacts.
On the other machine I will run the following PHP script that works against my Web API application:
Notice how easy it is to handle JSON data in PHP! My PHP script produces the following output:
Looks like data is here as it should be.
Now let’s write code to delete contacts. Add this block of code before any other code in PHP script.
Again simple code. If we write also insert and update methods we may want to bundle those operations to single class.
ASP.NET Web API is not only ASP.NET fun. It is available also for all other platforms. In this posting we wrote simple PHP client that is able to communicate with our Web API application. We wrote only some simple code, nothing complex. Same way we can use also platforms like Java, PERL and Ruby.