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

KEY
Zendcon 09
PPT
Framework
PDF
Arduino、Web 到 IoT
PDF
Open Policy Agent Deep Dive Seattle 2018
PPTX
Introduction to CodeIgniter (RefreshAugusta, 20 May 2009)
PDF
Dealing With Legacy PHP Applications
PDF
Forget about Index.php and build you applications around HTTP - PHPers Cracow
PPTX
E.D.D.I - Open Source Chatbot Platform
Zendcon 09
Framework
Arduino、Web 到 IoT
Open Policy Agent Deep Dive Seattle 2018
Introduction to CodeIgniter (RefreshAugusta, 20 May 2009)
Dealing With Legacy PHP Applications
Forget about Index.php and build you applications around HTTP - PHPers Cracow
E.D.D.I - Open Source Chatbot Platform

What's hot (10)

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

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

PDF
Develop web APIs in PHP using middleware with Expressive (Code Europe)
PDF
Developing web APIs using middleware in PHP 7
PDF
Develop microservices in php
PPTX
Building and managing applications fast for IBM i
PDF
PSR-7, middlewares e o futuro dos frameworks
PDF
Stacking Up Middleware
PDF
Criando uma API com Zend Expressive 3
PDF
Speed up web APIs with Expressive and Swoole (PHP Day 2018)
PDF
Zend Expressive 3 e PSR-15
PDF
Make your application expressive
PDF
Zend/Expressive 3 – The Next Generation
PDF
Web services tutorial
PDF
Web Services Tutorial
PDF
Introducción a aplicaciones php basadas en middleware y psr 7
PDF
ODP
Web Scraping with PHP
ODP
One Person's Perspective on a Pragmatic REST Interface
PPTX
Zend Expressive in 15 Minutes
PDF
Middleware web APIs in PHP 7.x
PDF
Diving into guzzle
Develop web APIs in PHP using middleware with Expressive (Code Europe)
Developing web APIs using middleware in PHP 7
Develop microservices in php
Building and managing applications fast for IBM i
PSR-7, middlewares e o futuro dos frameworks
Stacking Up Middleware
Criando uma API com Zend Expressive 3
Speed up web APIs with Expressive and Swoole (PHP Day 2018)
Zend Expressive 3 e PSR-15
Make your application expressive
Zend/Expressive 3 – The Next Generation
Web services tutorial
Web Services Tutorial
Introducción a aplicaciones php basadas en middleware y psr 7
Web Scraping with PHP
One Person's Perspective on a Pragmatic REST Interface
Zend Expressive in 15 Minutes
Middleware web APIs in PHP 7.x
Diving into guzzle
Ad

More from Zend by Rogue Wave Software (20)

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

Recently uploaded (20)

PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
top salesforce developer skills in 2025.pdf
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
Understanding Forklifts - TECH EHS Solution
PPTX
Introduction to Artificial Intelligence
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
System and Network Administraation Chapter 3
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
AI in Product Development-omnex systems
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
PTS Company Brochure 2025 (1).pdf.......
PPTX
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
Odoo Companies in India – Driving Business Transformation.pdf
top salesforce developer skills in 2025.pdf
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Understanding Forklifts - TECH EHS Solution
Introduction to Artificial Intelligence
Design an Analysis of Algorithms I-SECS-1021-03
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Softaken Excel to vCard Converter Software.pdf
System and Network Administraation Chapter 3
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
AI in Product Development-omnex systems
Design an Analysis of Algorithms II-SECS-1021-03
Upgrade and Innovation Strategies for SAP ERP Customers
Navsoft: AI-Powered Business Solutions & Custom Software Development
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PTS Company Brochure 2025 (1).pdf.......
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx

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.