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.

Building web APIs in PHP with Zend Expressive

  • 1.
    Building web APIsin PHPBuilding web APIs in PHP with Zend Expressivewith Zend Expressive Enrico Zimuel Senior software engineer, Rogue Wave Software
  • 2.
    About meAbout me PHPdeveloper 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.
  • 3.
  • 4.
    Building a WebAPIBuilding 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 inPHPHTTP request in PHP Managed using global variables  $_SERVER $_POST $_GET $_FILES $_COOKIE © 2018 Rogue Wave Software, Inc. All Rights Reserved.
  • 6.
    HTTP response inPHPHTTP 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.
  • 7.
  • 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.
  • 10.
  • 11.
    MiddlewareMiddleware A function thatgets 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 Createa pipeline of execution function ($request, $delegate) { // delegating another middleware $response = $delegate($request); return $response; } © 2018 Rogue Wave Software, Inc. All Rights Reserved.
  • 13.
    © 2018 RogueWave 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.
  • 15.
  • 16.
    The PHP frameworkfor 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 webAPIA 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)RequestHandler (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) usePsrHttpServerMiddlewareInterface; // 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 forAPIExpressive 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.
  • 21.
  • 22.
    Thanks!Thanks! More information: getexpressive.org FreeExpressive ebook © 2018 Rogue Wave Software, Inc. All Rights Reserved.
  • 23.
    Q & AQ& A
  • 24.
    Join our seriesJoinour 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.