SlideShare a Scribd company logo
1 of 24
Building web APIs in PHPBuilding web APIs in PHP
with Zend Expressivewith Zend Expressive
Enrico Zimuel
Senior software engineer, Rogue Wave Software
About meAbout me
PHP developer since 1999
Senior Software Engineer at
Inc.
Core team of ,
and
and international speaker
Research Programmer at
Co-founder of (Italy)
Rogue Wave Software
Apigility
Expressive Zend Framework
TEDx
Amsterdam University
PUG Torino
© 2018 Rogue Wave Software, Inc. All Rights Reserved.
Web APIWeb API
Building a Web APIBuilding a Web API
Managing the HTTP request and response
Choosing a representation format
Choosing an error format
Filtering & validating input data
Authenticating HTTP requests
Authorizing HTTP requests
Documentation
© 2018 Rogue Wave Software, Inc. All Rights Reserved.
HTTP request in PHPHTTP request in PHP
Managed using global variables 
$_SERVER
$_POST
$_GET
$_FILES
$_COOKIE
© 2018 Rogue Wave Software, Inc. All Rights Reserved.
HTTP response in PHPHTTP response in PHP
http_response_code()
header(), header_remove(), headers_list(),
headers_sent()
setcookie(), setrawcookie()
direct output for body content (bu ered)
© 2018 Rogue Wave Software, Inc. All Rights Reserved.
Please OOP!Please OOP!
PSR-7PSR-7
PHP Standards Recommendations (PSR)
Part of PHP Framework Interop Group (PHP FIG)
PSR-7 is a collection of interfaces for representing
HTTP messages as described in and
, and URIs for use with HTTP messages as
described in
RFC 7230 RFC
7231
RFC 3986
© 2018 Rogue Wave Software, Inc. All Rights Reserved.
ExampleExample
// Request
$header = $request->getHeader('Accept');
$query = $request->getQueryParams();
$body = $request->getBodyParams();
// Response
$response = $response->withStatus(418, "I'm a teapot");
$response = $response->withBodyParams(json_encode($body));
© 2018 Rogue Wave Software, Inc. All Rights Reserved.
MiddlewareMiddleware
MiddlewareMiddleware
A function that gets a request and generates a response
function ($request)
{
// do something with $request
return $response;
}
© 2018 Rogue Wave Software, Inc. All Rights Reserved.
Delegating middlewareDelegating middleware
Create a pipeline of execution
function ($request, $delegate)
{
// delegating another middleware
$response = $delegate($request);
return $response;
}
© 2018 Rogue Wave Software, Inc. All Rights Reserved.
© 2018 Rogue Wave Software, Inc. All Rights Reserved.
Example: cacheExample: cache
function ($request, $delegate) use ($cache)
{
if ($cache->has($request)) {
return $cache->get($request);
}
$response = $delegate($request);
$cache->set($request, $response);
return $response;
}
© 2018 Rogue Wave Software, Inc. All Rights Reserved.
ExpressiveExpressive
The PHP framework for middleware applications
PSR-7 support (using )
PSR-15 support
Piping work ow (using )
Features: routing, dependency injection, templating,
error handling
Last release 3.1.0, 22th June 2018
zend-diactoros
zend-stratigility
© 2018 Rogue Wave Software, Inc. All Rights Reserved.
A basic web APIA basic web API
use ZendDiactorosResponseJsonResponse;
use ZendExpressiveApplication;
$container = require 'config/container.php';
$app = $container->get(Application::class);
$app->pipe('/api/ping', function($request) {
return new JsonResponse(['ack' => time()]);
});
// or $app->pipe('/api/ping', AppHandlerPingHandler::class);
$app->run();
© 2018 Rogue Wave Software, Inc. All Rights Reserved.
Request Handler (PSR-15)Request Handler (PSR-15)
use PsrHttpMessageResponseInterface; // PSR-7
use PsrHttpMessageServerRequestInterface; // PSR-7
use PsrHttpServerRequestHandlerInterface; // PSR-15
use ZendDiactorosResponseJsonResponse;
class PingHandler implements RequestHandlerInterface
{
public function handle(
ServerRequestInterface $request
) : ResponseInterface
{
return new JsonResponse(['ack' => time()]);
}
}
A request handler generates a response from an HTTP request
© 2018 Rogue Wave Software, Inc. All Rights Reserved.
Middleware (PSR-15)Middleware (PSR-15)
use PsrHttpServerMiddlewareInterface; // PSR-15
class CacheMiddleware implements MiddlewareInterface
{
// ...
public function process(
ServerRequestInterface $request,
RequestHandlerInterface $handler
): ResponseInterface {
if ($this->cache->has($request)) {
return $this->cache->get($request);
}
$response = $handler($request);
$this->cache->set($request, $response);
return $response;
}
}
A middleware participates in processing an HTTP message, it may deletegate
© 2018 Rogue Wave Software, Inc. All Rights Reserved.
Expressive tool for APIExpressive tool for API
HAL-JSON:
Problem details:
Filtering & validation:
Authentication (HTTP Basic, OAuth2):
Authorization (ACL, RBAC):
zend-expressive-hal
zend-problem-details
zend-input lter
zend-expressive-
authentication
zend-expressive-
authorization
REST API example: ezimuel/zend-expressive-api
© 2018 Rogue Wave Software, Inc. All Rights Reserved.
DEMODEMO
Thanks!Thanks!
More information: getexpressive.org
Free Expressive ebook
© 2018 Rogue Wave Software, Inc. All Rights Reserved.
Q & AQ & A
Join our seriesJoin our series
2018 PHP expert talks2018 PHP expert talks
August 23: –
Zeev Suraski
A match made in heaven, learn the capabilities in Zend Server that help optimize apps and boost
performance.
Maxing out performance with Zend Server on PHP 7
September 20: –
Massimiliano Cavicchioli
As the top platform for modernizing IBM i applications, learn how to build PHP apps on i using Zend
Expressive, Zend Server, IBM i Toolkit, and DB2.
Building PHP applications fast for IBM i
On demand: – Zeev SuraskiTo PHP 7 and beyond
© 2018 Rogue Wave Software, Inc. All Rights Reserved.

More Related Content

What's hot

Xenogenetics for PL/SQL - infusing with Java best practices
Xenogenetics for PL/SQL - infusing with Java best practicesXenogenetics for PL/SQL - infusing with Java best practices
Xenogenetics for PL/SQL - infusing with Java best practicesLucas Jellema
 
And the Greatest of These Is ... Rack Support
And the Greatest of These Is ... Rack SupportAnd the Greatest of These Is ... Rack Support
And the Greatest of These Is ... Rack SupportBen Scofield
 
Angular Tutorial Freshers and Experienced
Angular Tutorial Freshers and ExperiencedAngular Tutorial Freshers and Experienced
Angular Tutorial Freshers and Experiencedrajkamaltibacademy
 
PHP an intro -1
PHP an intro -1PHP an intro -1
PHP an intro -1Kanchilug
 
Top 5 magento secure coding best practices Alex Zarichnyi
Top 5 magento secure coding best practices   Alex ZarichnyiTop 5 magento secure coding best practices   Alex Zarichnyi
Top 5 magento secure coding best practices Alex ZarichnyiMagento Dev
 
Storytelling By Numbers
Storytelling By NumbersStorytelling By Numbers
Storytelling By NumbersMichael King
 
November Camp - Spec BDD with PHPSpec 2
November Camp - Spec BDD with PHPSpec 2November Camp - Spec BDD with PHPSpec 2
November Camp - Spec BDD with PHPSpec 2Kacper Gunia
 
Django Environment
Django EnvironmentDjango Environment
Django EnvironmentLoren Davie
 
ngMess: AngularJS Dependency Injection
ngMess: AngularJS Dependency InjectionngMess: AngularJS Dependency Injection
ngMess: AngularJS Dependency InjectionDzmitry Ivashutsin
 

What's hot (10)

Xenogenetics for PL/SQL - infusing with Java best practices
Xenogenetics for PL/SQL - infusing with Java best practicesXenogenetics for PL/SQL - infusing with Java best practices
Xenogenetics for PL/SQL - infusing with Java best practices
 
And the Greatest of These Is ... Rack Support
And the Greatest of These Is ... Rack SupportAnd the Greatest of These Is ... Rack Support
And the Greatest of These Is ... Rack Support
 
Angular Tutorial Freshers and Experienced
Angular Tutorial Freshers and ExperiencedAngular Tutorial Freshers and Experienced
Angular Tutorial Freshers and Experienced
 
PHP an intro -1
PHP an intro -1PHP an intro -1
PHP an intro -1
 
Top 5 magento secure coding best practices Alex Zarichnyi
Top 5 magento secure coding best practices   Alex ZarichnyiTop 5 magento secure coding best practices   Alex Zarichnyi
Top 5 magento secure coding best practices Alex Zarichnyi
 
PHPSpec BDD Framework
PHPSpec BDD FrameworkPHPSpec BDD Framework
PHPSpec BDD Framework
 
Storytelling By Numbers
Storytelling By NumbersStorytelling By Numbers
Storytelling By Numbers
 
November Camp - Spec BDD with PHPSpec 2
November Camp - Spec BDD with PHPSpec 2November Camp - Spec BDD with PHPSpec 2
November Camp - Spec BDD with PHPSpec 2
 
Django Environment
Django EnvironmentDjango Environment
Django Environment
 
ngMess: AngularJS Dependency Injection
ngMess: AngularJS Dependency InjectionngMess: AngularJS Dependency Injection
ngMess: AngularJS Dependency Injection
 

Similar to Building web APIs in PHP with Zend Expressive

Speed up web APIs with Expressive and Swoole (PHP Day 2018)
Speed up web APIs with Expressive and Swoole (PHP Day 2018) Speed up web APIs with Expressive and Swoole (PHP Day 2018)
Speed up web APIs with Expressive and Swoole (PHP Day 2018) Zend by Rogue Wave Software
 
Java web programming
Java web programmingJava web programming
Java web programmingChing Yi Chan
 
JAX-RS 2.0: New and Noteworthy in RESTful Web services API at JAX London
JAX-RS 2.0: New and Noteworthy in RESTful Web services API at JAX LondonJAX-RS 2.0: New and Noteworthy in RESTful Web services API at JAX London
JAX-RS 2.0: New and Noteworthy in RESTful Web services API at JAX LondonArun Gupta
 
Executing a Large Scale Migration to AWS (ENT337-R2) - AWS re:Invent 2018
Executing a Large Scale Migration to AWS (ENT337-R2) - AWS re:Invent 2018Executing a Large Scale Migration to AWS (ENT337-R2) - AWS re:Invent 2018
Executing a Large Scale Migration to AWS (ENT337-R2) - AWS re:Invent 2018Amazon Web Services
 
Creating REST Applications with the Slim Micro-Framework by Vikram Vaswani
Creating REST Applications with the Slim Micro-Framework by Vikram VaswaniCreating REST Applications with the Slim Micro-Framework by Vikram Vaswani
Creating REST Applications with the Slim Micro-Framework by Vikram Vaswanivvaswani
 
Real World Dependency Injection - IPC11 Spring Edition
Real World Dependency Injection - IPC11 Spring EditionReal World Dependency Injection - IPC11 Spring Edition
Real World Dependency Injection - IPC11 Spring EditionStephan Hochdörfer
 
Real World Dependency Injection - phpday
Real World Dependency Injection - phpdayReal World Dependency Injection - phpday
Real World Dependency Injection - phpdayStephan Hochdörfer
 
OWASP TOP 10 for PHP Programmers
OWASP TOP 10 for PHP ProgrammersOWASP TOP 10 for PHP Programmers
OWASP TOP 10 for PHP Programmersrjsmelo
 
Clojure and the Web
Clojure and the WebClojure and the Web
Clojure and the Webnickmbailey
 
How to build a High Performance PSGI/Plack Server
How to build a High Performance PSGI/Plack Server How to build a High Performance PSGI/Plack Server
How to build a High Performance PSGI/Plack Server Masahiro Nagano
 
Bootstrat REST APIs with Laravel 5
Bootstrat REST APIs with Laravel 5Bootstrat REST APIs with Laravel 5
Bootstrat REST APIs with Laravel 5Elena Kolevska
 
Building and managing applications fast for IBM i
Building and managing applications fast for IBM iBuilding and managing applications fast for IBM i
Building and managing applications fast for IBM iZend by Rogue Wave Software
 
Modern Web Development with Perl
Modern Web Development with PerlModern Web Development with Perl
Modern Web Development with PerlDave Cross
 
HTTP Middlewares in PHP by Eugene Dounar
HTTP Middlewares in PHP by Eugene DounarHTTP Middlewares in PHP by Eugene Dounar
HTTP Middlewares in PHP by Eugene DounarMinsk PHP User Group
 

Similar to Building web APIs in PHP with Zend Expressive (20)

Speed up web APIs with Expressive and Swoole (PHP Day 2018)
Speed up web APIs with Expressive and Swoole (PHP Day 2018) Speed up web APIs with Expressive and Swoole (PHP Day 2018)
Speed up web APIs with Expressive and Swoole (PHP Day 2018)
 
Develop microservices in php
Develop microservices in phpDevelop microservices in php
Develop microservices in php
 
Zend Framework
Zend FrameworkZend Framework
Zend Framework
 
Java web programming
Java web programmingJava web programming
Java web programming
 
JAX-RS 2.0: New and Noteworthy in RESTful Web services API at JAX London
JAX-RS 2.0: New and Noteworthy in RESTful Web services API at JAX LondonJAX-RS 2.0: New and Noteworthy in RESTful Web services API at JAX London
JAX-RS 2.0: New and Noteworthy in RESTful Web services API at JAX London
 
Executing a Large Scale Migration to AWS (ENT337-R2) - AWS re:Invent 2018
Executing a Large Scale Migration to AWS (ENT337-R2) - AWS re:Invent 2018Executing a Large Scale Migration to AWS (ENT337-R2) - AWS re:Invent 2018
Executing a Large Scale Migration to AWS (ENT337-R2) - AWS re:Invent 2018
 
Creating REST Applications with the Slim Micro-Framework by Vikram Vaswani
Creating REST Applications with the Slim Micro-Framework by Vikram VaswaniCreating REST Applications with the Slim Micro-Framework by Vikram Vaswani
Creating REST Applications with the Slim Micro-Framework by Vikram Vaswani
 
BEAR (Suday) design
BEAR (Suday) designBEAR (Suday) design
BEAR (Suday) design
 
Real World Dependency Injection - IPC11 Spring Edition
Real World Dependency Injection - IPC11 Spring EditionReal World Dependency Injection - IPC11 Spring Edition
Real World Dependency Injection - IPC11 Spring Edition
 
Real World Dependency Injection - phpday
Real World Dependency Injection - phpdayReal World Dependency Injection - phpday
Real World Dependency Injection - phpday
 
OWASP TOP 10 for PHP Programmers
OWASP TOP 10 for PHP ProgrammersOWASP TOP 10 for PHP Programmers
OWASP TOP 10 for PHP Programmers
 
Clojure and the Web
Clojure and the WebClojure and the Web
Clojure and the Web
 
Intro to PSGI and Plack
Intro to PSGI and PlackIntro to PSGI and Plack
Intro to PSGI and Plack
 
How to build a High Performance PSGI/Plack Server
How to build a High Performance PSGI/Plack Server How to build a High Performance PSGI/Plack Server
How to build a High Performance PSGI/Plack Server
 
JAX-RS.next
JAX-RS.nextJAX-RS.next
JAX-RS.next
 
Bootstrat REST APIs with Laravel 5
Bootstrat REST APIs with Laravel 5Bootstrat REST APIs with Laravel 5
Bootstrat REST APIs with Laravel 5
 
Building and managing applications fast for IBM i
Building and managing applications fast for IBM iBuilding and managing applications fast for IBM i
Building and managing applications fast for IBM i
 
Modern Web Development with Perl
Modern Web Development with PerlModern Web Development with Perl
Modern Web Development with Perl
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
 
HTTP Middlewares in PHP by Eugene Dounar
HTTP Middlewares in PHP by Eugene DounarHTTP Middlewares in PHP by Eugene Dounar
HTTP Middlewares in PHP by Eugene Dounar
 

More from Zend by Rogue Wave Software

The Sodium crypto library of PHP 7.2 (PHP Day 2018)
The Sodium crypto library of PHP 7.2 (PHP Day 2018)The Sodium crypto library of PHP 7.2 (PHP Day 2018)
The Sodium crypto library of PHP 7.2 (PHP Day 2018)Zend by Rogue Wave Software
 
Develop web APIs in PHP using middleware with Expressive (Code Europe)
Develop web APIs in PHP using middleware with Expressive (Code Europe)Develop web APIs in PHP using middleware with Expressive (Code Europe)
Develop web APIs in PHP using middleware with Expressive (Code Europe)Zend by Rogue Wave Software
 
Fundamentals of performance tuning PHP on IBM i
Fundamentals of performance tuning PHP on IBM i  Fundamentals of performance tuning PHP on IBM i
Fundamentals of performance tuning PHP on IBM i Zend by Rogue Wave Software
 

More from Zend by Rogue Wave Software (20)

Speed and security for your PHP application
Speed and security for your PHP applicationSpeed and security for your PHP application
Speed and security for your PHP application
 
To PHP 7 and beyond
To PHP 7 and beyondTo PHP 7 and beyond
To PHP 7 and beyond
 
The Sodium crypto library of PHP 7.2 (PHP Day 2018)
The Sodium crypto library of PHP 7.2 (PHP Day 2018)The Sodium crypto library of PHP 7.2 (PHP Day 2018)
The Sodium crypto library of PHP 7.2 (PHP Day 2018)
 
Develop web APIs in PHP using middleware with Expressive (Code Europe)
Develop web APIs in PHP using middleware with Expressive (Code Europe)Develop web APIs in PHP using middleware with Expressive (Code Europe)
Develop web APIs in PHP using middleware with Expressive (Code Europe)
 
Middleware web APIs in PHP 7.x
Middleware web APIs in PHP 7.xMiddleware web APIs in PHP 7.x
Middleware web APIs in PHP 7.x
 
Ongoing management of your PHP 7 application
Ongoing management of your PHP 7 applicationOngoing management of your PHP 7 application
Ongoing management of your PHP 7 application
 
Developing web APIs using middleware in PHP 7
Developing web APIs using middleware in PHP 7Developing web APIs using middleware in PHP 7
Developing web APIs using middleware in PHP 7
 
The Docker development template for PHP
The Docker development template for PHPThe Docker development template for PHP
The Docker development template for PHP
 
The most exciting features of PHP 7.1
The most exciting features of PHP 7.1The most exciting features of PHP 7.1
The most exciting features of PHP 7.1
 
Unit testing for project managers
Unit testing for project managersUnit testing for project managers
Unit testing for project managers
 
The new features of PHP 7
The new features of PHP 7The new features of PHP 7
The new features of PHP 7
 
Deploying PHP apps on the cloud
Deploying PHP apps on the cloudDeploying PHP apps on the cloud
Deploying PHP apps on the cloud
 
Data is dead. Long live data!
Data is dead. Long live data! Data is dead. Long live data!
Data is dead. Long live data!
 
Optimizing performance
Optimizing performanceOptimizing performance
Optimizing performance
 
Resolving problems & high availability
Resolving problems & high availabilityResolving problems & high availability
Resolving problems & high availability
 
Developing apps faster
Developing apps fasterDeveloping apps faster
Developing apps faster
 
Keeping up with PHP
Keeping up with PHPKeeping up with PHP
Keeping up with PHP
 
Fundamentals of performance tuning PHP on IBM i
Fundamentals of performance tuning PHP on IBM i  Fundamentals of performance tuning PHP on IBM i
Fundamentals of performance tuning PHP on IBM i
 
Getting started with PHP on IBM i
Getting started with PHP on IBM iGetting started with PHP on IBM i
Getting started with PHP on IBM i
 
Continuous Delivery e-book
Continuous Delivery e-bookContinuous Delivery e-book
Continuous Delivery e-book
 

Recently uploaded

Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsMehedi Hasan Shohan
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationkaushalgiri8080
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...aditisharan08
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
cybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningcybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningVitsRangannavar
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 

Recently uploaded (20)

Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software Solutions
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanation
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
cybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningcybersecurity notes for mca students for learning
cybersecurity notes for mca students for learning
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 

Building web APIs in PHP with Zend Expressive

  • 1. Building web APIs in PHPBuilding web APIs in PHP with Zend Expressivewith Zend Expressive Enrico Zimuel Senior software engineer, Rogue Wave Software
  • 2. About meAbout me PHP developer since 1999 Senior Software Engineer at Inc. Core team of , and and international speaker Research Programmer at Co-founder of (Italy) Rogue Wave Software Apigility Expressive Zend Framework TEDx Amsterdam University PUG Torino © 2018 Rogue Wave Software, Inc. All Rights Reserved.
  • 4. Building a Web APIBuilding a Web API Managing the HTTP request and response Choosing a representation format Choosing an error format Filtering & validating input data Authenticating HTTP requests Authorizing HTTP requests Documentation © 2018 Rogue Wave Software, Inc. All Rights Reserved.
  • 5. HTTP request in PHPHTTP request in PHP Managed using global variables  $_SERVER $_POST $_GET $_FILES $_COOKIE © 2018 Rogue Wave Software, Inc. All Rights Reserved.
  • 6. HTTP response in PHPHTTP response in PHP http_response_code() header(), header_remove(), headers_list(), headers_sent() setcookie(), setrawcookie() direct output for body content (bu ered) © 2018 Rogue Wave Software, Inc. All Rights Reserved.
  • 8. PSR-7PSR-7 PHP Standards Recommendations (PSR) Part of PHP Framework Interop Group (PHP FIG) PSR-7 is a collection of interfaces for representing HTTP messages as described in and , and URIs for use with HTTP messages as described in RFC 7230 RFC 7231 RFC 3986 © 2018 Rogue Wave Software, Inc. All Rights Reserved.
  • 9. ExampleExample // Request $header = $request->getHeader('Accept'); $query = $request->getQueryParams(); $body = $request->getBodyParams(); // Response $response = $response->withStatus(418, "I'm a teapot"); $response = $response->withBodyParams(json_encode($body)); © 2018 Rogue Wave Software, Inc. All Rights Reserved.
  • 11. MiddlewareMiddleware A function that gets a request and generates a response function ($request) { // do something with $request return $response; } © 2018 Rogue Wave Software, Inc. All Rights Reserved.
  • 12. Delegating middlewareDelegating middleware Create a pipeline of execution function ($request, $delegate) { // delegating another middleware $response = $delegate($request); return $response; } © 2018 Rogue Wave Software, Inc. All Rights Reserved.
  • 13. © 2018 Rogue Wave Software, Inc. All Rights Reserved.
  • 14. Example: cacheExample: cache function ($request, $delegate) use ($cache) { if ($cache->has($request)) { return $cache->get($request); } $response = $delegate($request); $cache->set($request, $response); return $response; } © 2018 Rogue Wave Software, Inc. All Rights Reserved.
  • 16. The PHP framework for middleware applications PSR-7 support (using ) PSR-15 support Piping work ow (using ) Features: routing, dependency injection, templating, error handling Last release 3.1.0, 22th June 2018 zend-diactoros zend-stratigility © 2018 Rogue Wave Software, Inc. All Rights Reserved.
  • 17. A basic web APIA basic web API use ZendDiactorosResponseJsonResponse; use ZendExpressiveApplication; $container = require 'config/container.php'; $app = $container->get(Application::class); $app->pipe('/api/ping', function($request) { return new JsonResponse(['ack' => time()]); }); // or $app->pipe('/api/ping', AppHandlerPingHandler::class); $app->run(); © 2018 Rogue Wave Software, Inc. All Rights Reserved.
  • 18. Request Handler (PSR-15)Request Handler (PSR-15) use PsrHttpMessageResponseInterface; // PSR-7 use PsrHttpMessageServerRequestInterface; // PSR-7 use PsrHttpServerRequestHandlerInterface; // PSR-15 use ZendDiactorosResponseJsonResponse; class PingHandler implements RequestHandlerInterface { public function handle( ServerRequestInterface $request ) : ResponseInterface { return new JsonResponse(['ack' => time()]); } } A request handler generates a response from an HTTP request © 2018 Rogue Wave Software, Inc. All Rights Reserved.
  • 19. Middleware (PSR-15)Middleware (PSR-15) use PsrHttpServerMiddlewareInterface; // PSR-15 class CacheMiddleware implements MiddlewareInterface { // ... public function process( ServerRequestInterface $request, RequestHandlerInterface $handler ): ResponseInterface { if ($this->cache->has($request)) { return $this->cache->get($request); } $response = $handler($request); $this->cache->set($request, $response); return $response; } } A middleware participates in processing an HTTP message, it may deletegate © 2018 Rogue Wave Software, Inc. All Rights Reserved.
  • 20. Expressive tool for APIExpressive tool for API HAL-JSON: Problem details: Filtering & validation: Authentication (HTTP Basic, OAuth2): Authorization (ACL, RBAC): zend-expressive-hal zend-problem-details zend-input lter zend-expressive- authentication zend-expressive- authorization REST API example: ezimuel/zend-expressive-api © 2018 Rogue Wave Software, Inc. All Rights Reserved.
  • 22. Thanks!Thanks! More information: getexpressive.org Free Expressive ebook © 2018 Rogue Wave Software, Inc. All Rights Reserved.
  • 23. Q & AQ & A
  • 24. Join our seriesJoin our series 2018 PHP expert talks2018 PHP expert talks August 23: – Zeev Suraski A match made in heaven, learn the capabilities in Zend Server that help optimize apps and boost performance. Maxing out performance with Zend Server on PHP 7 September 20: – Massimiliano Cavicchioli As the top platform for modernizing IBM i applications, learn how to build PHP apps on i using Zend Expressive, Zend Server, IBM i Toolkit, and DB2. Building PHP applications fast for IBM i On demand: – Zeev SuraskiTo PHP 7 and beyond © 2018 Rogue Wave Software, Inc. All Rights Reserved.