simple web services with php
TRANSCRIPT
John Paul Ada
- BA Psychology Graduate
- Desktop, Web, Mobile Developer
- Three years industry dev experience
- Dev Community Contributor
- Tech Evangelist
Objectives
● Recognize a monolithic architecture and its problems
● Know what a web service is and its characteristics
● Know the differences between the two approaches
● Build a simple web service with PHP
By the end of this session, you should be able to:
Requirements
● Books Demo API code - https://github.com/johnpaulada/books-demo-api
● Books Demo Web code -
https://github.com/johnpaulada/books-demo-web
● PHP: http://192.168.254.102:8001/php-7.1.2-Win32-VC14-x86.zip
● MySQL:
http://192.168.254.102:8001/mysql-installer-community-5.7.17.0.msi
● XAMPP:
http://192.168.254.102:8001/xampp-win32-7.1.1-0-VC14-installer.exe
● Insomnia: http://192.168.254.102:8001/Insomnia+Setup+4.2.14.exe
● Git: http://192.168.254.102:8001/Git-2.11.1-32-bit.exe
You’ll need these tools: (SSID: CPE Laboratory; Password: CCSIT_OFFICE_1980)
Monolithic Architecture
What is a monolithic architecture?
It’s a software “structure” wherein all the parts of the system are interwoven and are not separate, reusable parts.
Don’t use it.
It’s hard to manage these when they get huge with a lot of code and you don’t have much flexibility.
Monolithic Architecture Problems
● Tightly coupled; changing one part might change the others
● Slow; a whole page instead of just the data
● Opinionated; you’re not giving the users a choice on how to use your data
● Not flexible; you’re limited to using only a web page
Monolithic Architecture Code Snippet
<?php
$title = “Art of Seduction”;
$author = “Robert Greene”;
?>
<div>
<p>Title: <?php echo $title; ?></p>
<p>Author: <?php echo $author; ?></p>
</div>
Web Services
What is a web service?
A web service is an application or a fraction of it which allows other applications to use it and is not tightly coupled to another application.
Use this instead of monolithic architectures.
This approach eliminates most of the problems posed by monolithic architectures.
Web Service Characteristics
● Loosely coupled; changing the web service will not change the client
and vice versa
● Fast; loads just the data instead of a whole web page
● Unopinionated; you’re giving the users a choice on how to use your data
● Flexible; you’re not limited to using only a web page -- you can use a
desktop or mobile app
REST APIs
What does REST stand for?
REST stands for REpresentational State Transfer.
What is it really?
It’s just a fancy term for an approach to building web services that revolves around HTTP Methods and endpoints.
What are APIs?
Application Programming Interfaces. They’re the services or the libraries that allow you to use a particular service. Take for example, Facebook’s or Twitter’s APIs.
HTTP Response
HTTP/1.1 201 CREATED
Date: Wed, 22 Feb 2017 12:28:53 GMT
Server: Apache/2.2.14 (Win32)
Last-Modified: Wed, 22 Feb 2017 12:28:53 GMT
Content-Length: 29
Content-Type: application/json
Connection: Closed
{“title”: “Art of Seduction”}
HTTP Request
POST /books HTTP/1.1
Host: localhost
Content-Type: application/json
{“title”: “Art of Seduction”}
Common HTTP Codes
● 200 - OK● 201 - Created● 204 - No Content● 301 - Moved Permanently● 400 - Bad Request● 401 - Unauthorized● 403 - Forbidden● 404 - Not Found● 500 - Internal Server Error
REST Endpoints
Request to/from collection/collection_name; e.g. /books
Request to/from a specific object/collection_name/:id; e.g. /books/1