Upgrade to Pro — share decks privately, control downloads, hide ads and more …

A PHP micro framework for HTTP API client

A PHP micro framework for HTTP API client

Calling a HTTP API is a repetitive task, you need to prepare the HTTP call, format the input, execute the HTTP request and format the output. The ZendService_Api is a PHP micro framework built with the HTTP component of Zend Framework 2 that can facilitate and automatize the procedure using simple PHP configuration arrays.

Enrico Zimuel

November 20, 2013
Tweet

More Decks by Enrico Zimuel

Other Decks in Programming

Transcript

  1. A MICRO FRAMEWORK FOR HTTP API CLIENT by / Senior

    Software Engineer - Zend Technologies Enrico Zimuel @ezimuel
  2. ABOUT ME I'm a Software Engineer since 1996. I work

    at since 2008. I'm a core contributor of and . I did research in computer science at the of the University of Amsterdam. I live in (Italy), where I work remotely most of the time. Zend Technologies Zend Framework Apigility Informatics Institute Turin
  3. CONSUME API How many times did you write PHP code

    to perform HTTP calls for API services?
  4. OLD SCHOOL EXAMPLE: USING CURL $ s e r v

    i c e _ u r l = ' h t t p : / / e x a m p l e . c o m / r e s t / u s e r / ' ; $ c u r l = c u r l _ i n i t ( $ s e r v i c e _ u r l ) ; $ c u r l _ p o s t _ d a t a = a r r a y ( " u s e r _ i d " = > 4 2 , " e m a i l a d d r e s s " = > ' l o r n a @ e x a m p l e . c o m ' , ) ; c u r l _ s e t o p t ( $ c u r l , C U R L O P T _ R E T U R N T R A N S F E R , t r u e ) ; c u r l _ s e t o p t ( $ c u r l , C U R L O P T _ P O S T , t r u e ) ; c u r l _ s e t o p t ( $ c u r l , C U R L O P T _ P O S T F I E L D S , $ c u r l _ p o s t _ d a t a ) ; $ c u r l _ r e s p o n s e = c u r l _ e x e c ( $ c u r l ) ; c u r l _ c l o s e ( $ c u r l ) ; v a r _ d u m p ( $ c u r l _ r e s p o n s e ) ; From of Lorna Jane Mitchell Using curl and PHP to talk to a REST service
  5. EXAMPLE: RESTFUL API USING JSON $ c u r l

    = c u r l _ i n i t ( ' h t t p : / / e x a m p l e . c o m / r e s t / u s e r ' ) ; $ p a y l o a d = j s o n _ e n c o d e ( a r r a y ( ' u s e r ' = > $ d a t a ) ) ; c u r l _ s e t o p t ( $ c u r l , C U R L O P T _ P O S T , t r u e ) ; c u r l _ s e t o p t ( $ c u r l , C U R L O P T _ P O S T F I E L D S , $ p a y l o a d ) ; c u r l _ s e t o p t ( c u r l , C U R L O P T _ H T T P H E A D E R , a r r a y ( ' C o n t e n t - T y p e : a p p l i c a t i o n / j s o n ' ) ) ; c u r l _ s e t o p t ( $ c u r l , C U R L O P T _ R E T U R N T R A N S F E R , t r u e ) ; $ c u r l _ r e s p o n s e = c u r l _ e x e c ( $ c u r l ) ; c u r l _ c l o s e ( $ c u r l ) ; v a r _ d u m p ( $ c u r l _ r e s p o n s e ) ;
  6. CONSUME HTTP API MANUALLY Repetitive task (boring) Parse the input/output

    format (JSON, XML, raw text, etc) Manage the error messages (HTTP status) Manage the HTTP headers (authentication, etc) ... Generally a bad idea, use a framework/library instead
  7. ZENDSERVICE_API A micro HTTP framework to simplify the consume of

    API Configure the header, method, body, and query string of a HTTP request according to specific API parameters Managed by simple PHP configuration array It uses the client of Zend\Http Zend Framework 2 Github: https://github.com/zendframework/ZendService_Api
  8. INSTALLATION Using composer, add the following require: { " r

    e q u i r e " : { " z e n d f r a m e w o r k / z e n d s e r v i c e - a p i " : " d e v - m a s t e r " } } Install c u r l - s h t t p s : / / g e t c o m p o s e r . o r g / i n s t a l l e r | p h p p h p c o m p o s e r . p h a r i n s t a l l
  9. EXAMPLE: HTTP POST API Suppose you have to send the

    following HTTP request: P O S T / v 1 / a u t h H T T P / 1 . 1 H o s t : l o c a l h o s t C o n n e c t i o n : c l o s e C o n t e n t - T y p e : a p p l i c a t i o n / j s o n C o n t e n t - L e n g t h : 5 7 { ' a u t h ' : { ' u s e r n a m e ' : ' a d m i n ' , ' p a s s w o r d ' : ' t e s t ' } }
  10. CONFIGURING THE API u s e Z e n d

    S e r v i c e \ A p i \ A p i ; $ a p i = n e w A p i ( ) ; $ a p i - > s e t A p i ( ' a u t h ' , f u n c t i o n ( $ p a r a m s ) { r e t u r n a r r a y ( ' u r l ' = > ' h t t p : / / l o c a l h o s t / v 1 / a u t h ' , ' h e a d e r ' = > a r r a y ( ' C o n t e n t - T y p e ' = > ' a p p l i c a t i o n / j s o n ' ) , ' m e t h o d ' = > ' P O S T ' , ' b o d y ' = > j s o n _ e n c o d e ( a r r a y ( ' a u t h ' = > a r r a y ( ' u s e r n a m e ' = > $ p a r a m s [ 0 ] , ' p a s s w o r d ' = > $ p a r a m s [ 1 ] ) ) ) , ' r e s p o n s e ' = > a r r a y ( ' v a l i d _ c o d e s ' = > a r r a y ( ' 2 0 0 ' ) ) ) ; } ) ;
  11. EXECUTING THE API The API is executed as method of

    the $ a p i object: $ r e s u l t = $ a p i - > a u t h ( ' u s e r n a m e ' , ' p a s s w o r d ' ) ; i f ( $ a p i - > i s S u c c e s s ( ) ) { v a r _ d u m p ( $ r e s u l t ) ; } e l s e { p r i n t f ( " E r r o r ( % d ) : % s \ n " , $ a p i - > g e t S t a t u s C o d e ( ) , $ a p i - > g e t E r r o r M s g ( ) ) ; }
  12. MAPPING THE API The $ a p i - >

    a u t h ( ) function is managed using the _ _ c a l l ( ) magic method of PHP The mapping with the arguments of a u t h ( ) function is managed using the array $ p a r a m s of the closure We use the numerical index of the $ p a r a m s to match the order of the arguments in the function
  13. Create a configuration file, : USING A CONFIGURATION FILE a

    u t h . p h p r e t u r n a r r a y ( ' u r l ' = > ' h t t p : / / l o c a l h o s t / v 1 / a u t h ' , ' h e a d e r ' = > a r r a y ( ' C o n t e n t - T y p e ' = > ' a p p l i c a t i o n / j s o n ' ) , ' m e t h o d ' = > ' P O S T ' , ' b o d y ' = > j s o n _ e n c o d e ( a r r a y ( ' a u t h ' = > a r r a y ( ' u s e r n a m e ' = > $ p a r a m s [ 0 ] , ' p a s s w o r d ' = > $ p a r a m s [ 1 ] ) ) ) , ' r e s p o n s e ' = > a r r a y ( ' v a l i d _ c o d e s ' = > a r r a y ( ' 2 0 0 ' ) ) ) ;
  14. EXECUTE THE API USING A FILE u s e Z

    e n d S e r v i c e \ A p i \ A p i ; $ a p i = n e w A p i ( ) ; $ a p i - > s e t A p i P a t h ( ' p a t h / t o / a u t h . p h p ' ) ; $ r e s u l t = $ a p i - > a u t h ( ' u s e r n a m e ' , ' p a s s w o r d ' ) ; i f ( $ a p i - > i s S u c c e s s ( ) ) { v a r _ d u m p ( $ r e s u l t ) ; } e l s e { p r i n t f ( " E r r o r ( % d ) : % s \ n " , $ a p i - > g e t S t a t u s C o d e ( ) , $ a p i - > g e t E r r o r M s g ( ) ) ; }
  15. SET A BASE URL FOR THE APIS You can specify

    a base URL for all the API calls u s e Z e n d S e r v i c e \ A p i \ A p i ; $ a p i = n e w A p i ( ) ; $ a p i - > s e t U r l ( ' h t t p : / / i d e n t i t y . a p i . o p e n s t a c k . o r g ' ) ; We can use relative URL for the API
  16. QUERY STRING IN THE API CALL You can specify a

    query string for the API calls u s e Z e n d S e r v i c e \ A p i \ A p i ; $ a p i = n e w A p i ( ) ; $ a p i - > s e t Q u e r y P a r a m s ( a r r a y ( ' a u t h ' = > ' s t r o n g ' ) ) ; This will add the ? a u t h = s t r o n g query in all the API calls
  17. DEFAULT HEADER IN THE API CALL u s e Z

    e n d S e r v i c e \ A p i \ A p i ; $ a p i = n e w A p i ( ) ; $ a p i - > s e t A p i P a t h ( ' p a t h / t o / a p i / c o n f i g ' ) ; $ a p i - > s e t H e a d e r s ( a r r a y ( ' X - A u t h - T o k e n ' = > ' t o k e n ' ) ) ;
  18. USE CASE: ZENDSERVICE_OPENSTACK We used ZendService_Api to implement the OpenStack

    service component of ZF2 Configuration files stored in the ZendService/OpenStack/api folder Github: https://github.com/zendframework/ZendService_OpenStack
  19. THANKS! Rate this talk at joind.in/9274 More information on ZendService_Api:

    https://github.com/zendframework/ZendService_Api This work is licensed under a . I used to make this presentation. Creative Commons Attribution-ShareAlike 3.0 Unported License reveal.js