Create JSON based REST Api in PHP

bitwickedIndia

Maroon Man
Aug 13, 2013
19
0
0
India
witkind.blogspot.in
I need good tutes about creating web services ( REST Api with JSON ) using PHP - MySql / MongoDB / Whatever the DB. I would prefer to use a framework like Yii 2.0.3. What should be my approach? :1zhelp:
 


If you're looking for a framework, you might checkout apigility . It is based on Zend Framework 2, so it's quite powerful. It's from the company that created PHP, so it's not going away. It also has an admin panel you can use to generate routes and the basic skeleton of a service. Saves time and will definitely help you start quickly if you're new to REST.

Good luck!
 
I checked out laravel and yii. Still need to spend some time with apigility. Interesting. However, how would I actually implement the REST API? Can I get some idea. Sorry newbie to all these. What is REST all about? Ya don't need to spoon-feed me! Just help me out with some good resources.
 
I think it's easier to understand how apigility can help you if you first understand what REST is all about. You can find all sorts of long, academic explanations of REST, but for me it all boils down to using the full power of the HTTP protocol.

If you've done any web programming, you are probably familiar with the verbs GET and POST, but HTTP also specifies several other verbs that you probably haven't used including PUT, PATCH, and DELETE. The standard way of using these verbs is like this:

GET - retrieve one or more existing resources
POST - create a new resource
PUT - replace an existing resource with a new one
PATCH - modify a resource in place (update only certain fields)
DELETE - delete an existing resource

You can see that HTTP provides you everything you need--right out of the box-- to create, read, update, and delete data. By leveraging these standards, you save yourself time in designing your API and make it easier for users of your API who are familiar with REST to get started using it.

What apigility does for you is take away a lot of the drudge work like: setting up routes, creating classes, and generating documentation for you. It also allows you to use ZF2's tools for things like input filtering and validation that you will want as your API grows.

For example, you might want to allow your users to interact with "widgets" in your system. With just a couple of clicks, apigility will generate stubs of all the classes you need for someone to be able to do all the common REST verbs on your "widget", along with basic documentation. It will also give you friendly urls like /widget instead of /script.php?thing=widget or some nonsense like that.
 
MongoDB and PHP don't play well together. You can do it, but certain things about data types make it pretty obnoxious.

Basically, whatever framework you use, you're just going to send your response formatted as JSON. echo json_encode($yourThing); It's actually much easier than building a site with PHP.

When I was still using PHP I liked the Kohana framework. The ORM is a nice balance between raw SQL and something bloated like Doctrine.
 
There's a microframework for php called slim that is pretty popular for creating rest services. A potential upside for you is that it's not a big a learning curve as a full blown php framework, but it does provide the basics (routing, etc)

Slim Framework: Slim Framework

Google for "slim framework json rest" and you'll find several examples of it in use.