SlideShare a Scribd company logo
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 (10)

PPTX
Xenogenetics for PL/SQL - infusing with Java best practices
Lucas Jellema
 
KEY
And the Greatest of These Is ... Rack Support
Ben Scofield
 
PPTX
Angular Tutorial Freshers and Experienced
rajkamaltibacademy
 
PDF
PHP an intro -1
Kanchilug
 
PPTX
Top 5 magento secure coding best practices Alex Zarichnyi
Magento Dev
 
PDF
PHPSpec BDD Framework
Marcello Duarte
 
PDF
Storytelling By Numbers
Michael King
 
PDF
November Camp - Spec BDD with PHPSpec 2
Kacper Gunia
 
PDF
Django Environment
Loren Davie
 
PDF
ngMess: AngularJS Dependency Injection
Dzmitry Ivashutsin
 
Xenogenetics for PL/SQL - infusing with Java best practices
Lucas Jellema
 
And the Greatest of These Is ... Rack Support
Ben Scofield
 
Angular Tutorial Freshers and Experienced
rajkamaltibacademy
 
PHP an intro -1
Kanchilug
 
Top 5 magento secure coding best practices Alex Zarichnyi
Magento Dev
 
PHPSpec BDD Framework
Marcello Duarte
 
Storytelling By Numbers
Michael King
 
November Camp - Spec BDD with PHPSpec 2
Kacper Gunia
 
Django Environment
Loren Davie
 
ngMess: AngularJS Dependency Injection
Dzmitry Ivashutsin
 

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

PDF
Develop web APIs in PHP using middleware with Expressive (Code Europe)
Zend by Rogue Wave Software
 
PDF
Developing web APIs using middleware in PHP 7
Zend by Rogue Wave Software
 
PDF
Develop microservices in php
Zend by Rogue Wave Software
 
PPTX
Building and managing applications fast for IBM i
Zend by Rogue Wave Software
 
PDF
PSR-7, middlewares e o futuro dos frameworks
Elton Minetto
 
PDF
Stacking Up Middleware
Mark Niebergall
 
PDF
Criando uma API com Zend Expressive 3
Juciellen Cabrera
 
PDF
Speed up web APIs with Expressive and Swoole (PHP Day 2018)
Zend by Rogue Wave Software
 
PDF
Zend Expressive 3 e PSR-15
Juciellen Cabrera
 
PDF
Make your application expressive
Christian Varela
 
PDF
Zend/Expressive 3 – The Next Generation
Ralf Eggert
 
PDF
Web services tutorial
Lorna Mitchell
 
PDF
Web Services Tutorial
Lorna Mitchell
 
PDF
Introducción a aplicaciones php basadas en middleware y psr 7
Alejandro Celaya Alastrué
 
PDF
Psr-7
Marco Perone
 
ODP
Web Scraping with PHP
Matthew Turland
 
ODP
One Person's Perspective on a Pragmatic REST Interface
abrummett
 
PPTX
Zend Expressive in 15 Minutes
Chris Tankersley
 
PDF
Middleware web APIs in PHP 7.x
Zend by Rogue Wave Software
 
PDF
Diving into guzzle
Steven Wade
 
Develop web APIs in PHP using middleware with Expressive (Code Europe)
Zend by Rogue Wave Software
 
Developing web APIs using middleware in PHP 7
Zend by Rogue Wave Software
 
Develop microservices in php
Zend by Rogue Wave Software
 
Building and managing applications fast for IBM i
Zend by Rogue Wave Software
 
PSR-7, middlewares e o futuro dos frameworks
Elton Minetto
 
Stacking Up Middleware
Mark Niebergall
 
Criando uma API com Zend Expressive 3
Juciellen Cabrera
 
Speed up web APIs with Expressive and Swoole (PHP Day 2018)
Zend by Rogue Wave Software
 
Zend Expressive 3 e PSR-15
Juciellen Cabrera
 
Make your application expressive
Christian Varela
 
Zend/Expressive 3 – The Next Generation
Ralf Eggert
 
Web services tutorial
Lorna Mitchell
 
Web Services Tutorial
Lorna Mitchell
 
Introducción a aplicaciones php basadas en middleware y psr 7
Alejandro Celaya Alastrué
 
Web Scraping with PHP
Matthew Turland
 
One Person's Perspective on a Pragmatic REST Interface
abrummett
 
Zend Expressive in 15 Minutes
Chris Tankersley
 
Middleware web APIs in PHP 7.x
Zend by Rogue Wave Software
 
Diving into guzzle
Steven Wade
 
Ad

More from Zend by Rogue Wave Software (20)

PPTX
Speed and security for your PHP application
Zend by Rogue Wave Software
 
PPTX
To PHP 7 and beyond
Zend by Rogue Wave Software
 
PDF
The Sodium crypto library of PHP 7.2 (PHP Day 2018)
Zend by Rogue Wave Software
 
PPTX
Ongoing management of your PHP 7 application
Zend by Rogue Wave Software
 
PDF
The Docker development template for PHP
Zend by Rogue Wave Software
 
PDF
The most exciting features of PHP 7.1
Zend by Rogue Wave Software
 
PPTX
Unit testing for project managers
Zend by Rogue Wave Software
 
PDF
The new features of PHP 7
Zend by Rogue Wave Software
 
PPTX
Deploying PHP apps on the cloud
Zend by Rogue Wave Software
 
PPTX
Data is dead. Long live data!
Zend by Rogue Wave Software
 
PPTX
Optimizing performance
Zend by Rogue Wave Software
 
PPTX
Resolving problems & high availability
Zend by Rogue Wave Software
 
PPTX
Developing apps faster
Zend by Rogue Wave Software
 
PPTX
Keeping up with PHP
Zend by Rogue Wave Software
 
PPTX
Fundamentals of performance tuning PHP on IBM i
Zend by Rogue Wave Software
 
PPTX
Getting started with PHP on IBM i
Zend by Rogue Wave Software
 
PDF
Continuous Delivery e-book
Zend by Rogue Wave Software
 
PDF
Standard CMS on standard PHP Stack - Drupal and Zend Server
Zend by Rogue Wave Software
 
PDF
Dev & Prod - PHP Applications in the Cloud
Zend by Rogue Wave Software
 
PDF
The Truth about Lambdas and Closures in PHP
Zend by Rogue Wave Software
 
Speed and security for your PHP application
Zend by Rogue Wave Software
 
To PHP 7 and beyond
Zend by Rogue Wave Software
 
The Sodium crypto library of PHP 7.2 (PHP Day 2018)
Zend by Rogue Wave Software
 
Ongoing management of your PHP 7 application
Zend by Rogue Wave Software
 
The Docker development template for PHP
Zend by Rogue Wave Software
 
The most exciting features of PHP 7.1
Zend by Rogue Wave Software
 
Unit testing for project managers
Zend by Rogue Wave Software
 
The new features of PHP 7
Zend by Rogue Wave Software
 
Deploying PHP apps on the cloud
Zend by Rogue Wave Software
 
Data is dead. Long live data!
Zend by Rogue Wave Software
 
Optimizing performance
Zend by Rogue Wave Software
 
Resolving problems & high availability
Zend by Rogue Wave Software
 
Developing apps faster
Zend by Rogue Wave Software
 
Keeping up with PHP
Zend by Rogue Wave Software
 
Fundamentals of performance tuning PHP on IBM i
Zend by Rogue Wave Software
 
Getting started with PHP on IBM i
Zend by Rogue Wave Software
 
Continuous Delivery e-book
Zend by Rogue Wave Software
 
Standard CMS on standard PHP Stack - Drupal and Zend Server
Zend by Rogue Wave Software
 
Dev & Prod - PHP Applications in the Cloud
Zend by Rogue Wave Software
 
The Truth about Lambdas and Closures in PHP
Zend by Rogue Wave Software
 
Ad

Recently uploaded (20)

PDF
4K Video Downloader Plus Pro Crack for MacOS New Download 2025
bashirkhan333g
 
PDF
NEW-Viral>Wondershare Filmora 14.5.18.12900 Crack Free
sherryg1122g
 
PPTX
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PPTX
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
PDF
Top Agile Project Management Tools for Teams in 2025
Orangescrum
 
PPTX
Milwaukee Marketo User Group - Summer Road Trip: Mapping and Personalizing Yo...
bbedford2
 
PDF
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
PPTX
Agentic Automation: Build & Deploy Your First UiPath Agent
klpathrudu
 
PDF
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
PDF
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
PPTX
Home Care Tools: Benefits, features and more
Third Rock Techkno
 
PDF
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
PDF
Adobe Premiere Pro Crack / Full Version / Free Download
hashhshs786
 
PPTX
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
PPTX
Coefficient of Variance in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PPTX
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
PDF
IObit Driver Booster Pro 12.4.0.585 Crack Free Download
henryc1122g
 
PDF
Driver Easy Pro 6.1.1 Crack Licensce key 2025 FREE
utfefguu
 
PDF
How to Hire AI Developers_ Step-by-Step Guide in 2025.pdf
DianApps Technologies
 
4K Video Downloader Plus Pro Crack for MacOS New Download 2025
bashirkhan333g
 
NEW-Viral>Wondershare Filmora 14.5.18.12900 Crack Free
sherryg1122g
 
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
Top Agile Project Management Tools for Teams in 2025
Orangescrum
 
Milwaukee Marketo User Group - Summer Road Trip: Mapping and Personalizing Yo...
bbedford2
 
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
Agentic Automation: Build & Deploy Your First UiPath Agent
klpathrudu
 
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
Home Care Tools: Benefits, features and more
Third Rock Techkno
 
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
Adobe Premiere Pro Crack / Full Version / Free Download
hashhshs786
 
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
Coefficient of Variance in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
IObit Driver Booster Pro 12.4.0.585 Crack Free Download
henryc1122g
 
Driver Easy Pro 6.1.1 Crack Licensce key 2025 FREE
utfefguu
 
How to Hire AI Developers_ Step-by-Step Guide in 2025.pdf
DianApps Technologies
 

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.