simple web services with php

22
Simple Web Services with PHP Date: 02/23/17 Presented by John Paul Ada GAPLabs

Upload: john-paul-ada

Post on 12-Apr-2017

64 views

Category:

Technology


1 download

TRANSCRIPT

Simple Web Services with PHPDate: 02/23/17Presented by John Paul Ada

GAPLabs

John Paul Ada

- BA Psychology Graduate

- Desktop, Web, Mobile Developer

- Three years industry dev experience

- Dev Community Contributor

- Tech Evangelist

John Paul AdaSoftware Engineer

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 ArchitectureThe Bad and the Bad

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

Database

Server

Client

All in the same

server/unit

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 ServicesA more modern approach/solution

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

Web Services

Web Service

Desktop Web Mobile

REST APIsMost common approach to Web Services

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”}

HTTP Codes

● 2XX - Request success

● 3XX - Redirects

● 4XX - Client’s fault

● 5XX - Server’s fault

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

CRUD with REST

Read all booksGET /books

Read a bookGET /books/1

Create a bookPOST /books

Update a bookPUT /books/1

Remove a bookDELETE /books/1