24 February 2018

How to develop php web service part 1:Basic set-up for development

web service in php

Php is an easy and quick learning language. As PHP version increasing, new feature is coming to PHP.
I have spent so much time learning and develop PHP web service for my android app and website. There are a lot of tutorials available but what I feel from those tutorials that they are not writing for a basic PHP learner. I want to solve this. Therefore I am starting this tutorial for all PHP beginner learners. We will go step by step with a detailed analysis of code. You will learn here to develop web service, how to consume rest API in PHP website and android app. So bookmark this website.

For a beginner, What is REST or RESTful API development

A RESTful API is an application program interface (API) that uses HTTP requests to GET, PUT, POST, and DELETE data.
A RESTful API -- also referred to as a RESTful web service -- is based on representational state transfer (REST) technology, an architectural style, and approach to communications often used in web services development.
Here you have to consider few points like you have knowledge about OOPS and class-based PHP programming.


Environment setup for PHP web service or REST API development

For the development of web service in PHP, you have to use a lightweight PHP framework named SLIM. It makes rest API development so much easy and manageable. For downloading SLIM framework you have to download composer.
You have no idea what is a Composer? Don't worry, I will tell you basic about the composer.

Composer will manage the dependencies you require on a project by project basis. This means that Composer will pull in all the required libraries, dependencies and manage them all in one place.
This kind of management for dependencies in a project is not a new concept, and in fact, much of Composer is actually inspired from npm from Node.js and Bundler from Ruby.
You can understand it by an example. If you need some library in your project for working and that library depends on some other library then if you will not use composer you have downloaded all these libraries, files one by one, if using composer then it will download all library and you can access this class file using just one line code like include 'vender/autoloader.php';
Download Composer-Setup file from here in htdocs/www folder(I am using XAMPP server) your working server.
Open cmd in your window computer using Win+r and type cmd or as you open in the different OS.

First check, composer install on your system or not. So write composer in your cmd. It will print composer with a version than everything is okay.

Second, go to your project folder. i.e htdocs (if you are using xampp server)/yourProjectName. Copy your project path from the computer address bar in your explorer section and write in your cmd as cd and right click and paste copied Address. After that press enter.

Note:- Copied text can paste on cmd using the only mouse using left click and after that click on the paste option.

Third, write below the line and press enter.
composer require slim/slim "^3.0". 

After download, you have folders in your project location named composer.json, vender, composer.lock, etc. Create index.php which is inside the public folder. We will write our API code here. Below Image will be our project file structure


WebAppTest is our project folder which is inside htdocs. Inside this, there are include, V1 for REST API version control and vendor folders. Include and V1 is created by me. The vendor folder keeps all necessary files downloaded by composer. We will discuss more in the next tutorial.

Next tutorial- set-up route, remove index.php from URL, and understand each line of code.

No comments:

Post a Comment

Ads Inside Post