Start future-proofing your business logic by building web APIs. Do you think it’s too hard and time-consuming? Zend has made the process easier by releasing Apigility, a free and open source tool that helps us create Zend Framework 2 back-ends ready to be consumed by desktop, mobile, the internet of things, or anything else that comes along. This session is a step-by-step tutorial. Thus I’ll be using Apigility to create an API that accesses RPG business logic via the PHP Toolkit so you can hit the ground running.
Project Based Learning (A.I).pptx detail explanation
Apigility-Powered APIs on IBM i
1. RPG & PHP REST Services
with Apigility
Chuk Shirley
Sabel Steel Service, Inc.
Club Seiden
2. Chuk Shirley
• Developing web applications
since 2007
• PHP since 2011
• Winner of the 2015 COMMON
Innovation Award for Sabel
Steel Service’s mobile web
application
@chukShirley
chukShirley@gmail.com
3. Today’s topics
• API concepts
• Apigility installation
• Server configuration
• Creating RESTful web services
• Sanitizing input
• Using the toolkit
• Error handling
• Testing
5. API Terminology
• Examples:
• IBM i OS commands (CRTLIB, DSPJOBLOG)
• jQuery methods (jQuery.ajax(), jQuery.on())
• RPG built-in functions (%DEC, %XLATE)
“An API is a way for two computer applications to talk
to each other over a network using a common
language that they both understand.”
Jacobson, Daniel and Greg Brail. Sebastopol, APIs: A Strategy Guide Sebastopol, CA: O’Reilly Media, Inc., 2012
• API = “Application Programming Interface”
6. API Terminology
• API vs Web API
• A Web API is an API accessed over HTTP
• Web service vs Web API
• A web service refers to one or many related API
functions
• A web API is a collection of web services
7. Application Layers
PRESENTATION DOMAIN DATA SOURCE
Web (HTML, CSS, JS) PHP Db2
Mobile (Objective C,
Cordova)
CL, RPG, COBOL, Java Sessions
Toaster app SQL Cache
Web API
10. Requirements
• Code Editor (Zend Studio, RDi, Notepad, etc.)
• Zend Server 6 or above running PHP 5.4.8 or above
• PHP Toolkit and XMLSERVICE Library
• REST Client (optional)
• Postman, cURL, etc.
11. Development Methods
• Local
• Apigility admin UI runs locally
• Edit your code locally
• Push changes to server
• Remote
• Apigility admin UI runs on server
• Edit your code locally (pull files from server first)
• Push changes to server
20. Add Apache Virtual Host
• In /www/zendsvr6/conf/httpd.conf
• Template is on next slide
• You’ll need to supply three values:
• Server IP address (10.x.x.x, 192.x.x.x)
• Port number **Choose one that isn’t in use**
• Path to the project
• Restart Zend Server
24. Initializing the DB2 Table
CREATE TABLE MY_LIB.CUSTOMERS (
ID BIGINT GENERATED ALWAYS AS IDENTITY
(START WITH 1, INCREMENT BY 1, NO ORDER,
NO CYCLE, NO MINVALUE, NO MAXVALUE,
CACHE 20),
NAME VARCHAR (50),
ADDRESS VARCHAR (75)
);
25. Designing the URIs
HTTP REQUEST
METHOD
EXAMPLE URI TASK
GET /customer lists all rows
POST /customer Creates new row in db
GET /customer/{customer id} fetch a particular row
PUT /customer/{customer id} Update row in db
DELETE /customer/{customer id} Remove row from db
44. ZF2 Services
• The toolkit should be used as a service
• Services are configured in the Service Manager
• PHP array
• Assembled at runtime from many locations
• Global application services located at
/config/autoload/application.config.php
56. Error handling
• Use HTTP response status codes for errors
• Apigility supports API Problem format
• Uses application/problem+json mediatype
57. Sending the API Problem
• You can produce an API Problem with one of two
methods:
• return new ZFApiProblemApiProblemResponse()
• throw new Exception()