SlideShare a Scribd company logo
1 of 60
Working with Web Services Lorna Mitchell October 2009
Who am I? ,[object Object]
PHP Developer/Consultant/Trainer at Ibuildings
Personal site at lornajane.net
European Rep for PHPWomen.org
PHPNW organiser
Twitter: @lornajane
Working with Web Services ,[object Object]
Web services overview
Data types
Service types
Debugging
Using services from PHP
What are Web Services? ,[object Object]
Formatted data instead of a web page
Why Do We Care? ,[object Object]
within systems ,[object Object]
Sharing information between systems cleanly
How do Web Services Work? ,[object Object]
Sound familiar?
Request and response, just like a web application
Same theories apply
When Things Go Wrong ,[object Object]
We may not expect them
Apache logs
Debug output and logging
Verbose error-checking and logging from our app
Graceful failure
Data Formats
JSON ,[object Object]
Natively read/write in most languages
Very simple! (we like simple)
Limitations ,[object Object]
no distinction between object and array
Writing JSON from PHP 1  < ?php 2  3  $menu [ 'starter' ] =   array (   &quot;prawn cocktail&quot; , 4  &quot;soup of the day&quot; ); 5  $menu [ 'main course' ] =   array (   &quot;roast chicken&quot; , 6  &quot;fish 'n' chips&quot; , 7  &quot;macaroni cheese&quot; ); 8  $menu [ 'pudding' ] =   array (   &quot;cheesecake&quot; , 9  &quot;treacle sponge&quot; ); 10  11  echo   json_encode ( $menu ); {&quot;starter&quot;:[&quot;prawn cocktail&quot;,&quot;soup of the day&quot;],&quot;main course&quot;:[&quot;roast chicken&quot;,&quot;fish 'n' chips&quot;,&quot;macaroni cheese&quot;],&quot;pudding&quot;:[&quot;cheesecake&quot;,&quot;treacle sponge&quot;]}
Reading JSON from PHP 1  < ?php 2  3  $json   =   '{&quot;starter&quot;:[&quot;prawn cocktail&quot;,&quot;soup of the day&quot;],&quot;main course&quot;:[&quot;roast chicken&quot;,&quot;fish   apos; n apos;   chips&quot;,&quot;macaroni cheese&quot;],&quot;pudding&quot;:[&quot;cheesecake&quot;,&quot;treacle sponge&quot;]}' ; 4  5  print_r ( json_decode ( $json ));
Reading JSON from PHP stdClass Object ( [starter] => Array ( [0] => prawn cocktail [1] => soup of the day ) [main course] => Array ( [0] => roast chicken [1] => fish 'n' chips [2] => macaroni cheese ) [pudding] => Array ( [0] => cheesecake [1] => treacle sponge ) )
XML ,[object Object]
Familiar
Can give more detail than JSON
Native read/write in most languages
Working with XML from PHP ,[object Object]
SimpleXML
DOM
SimpleXML Example 1  < ?php 2  3  $xml   = <<<   XML 4  < ?xml version = &quot;1.0&quot;  ? > 5  < menus > 6  < menu > Lunch </ menu > 7  < menu > Dinner </ menu > 8  < menu > Dessert </ menu > 9  < menu > Drinks </ menu > 10  </ menus > 11  XML ; 12  13  $simplexml   =   new   SimpleXMLElement ( $xml ); 14  var_dump ( $simplexml );
SimpleXML Example object(SimpleXMLElement)#1 (1) { [&quot;menu&quot;]=> array(5) { [0]=> string(5) &quot;Lunch&quot; [1]=> string(6) &quot;Dinner&quot; [2]=> string(7) &quot;Dessert&quot; [3]=> string(6) &quot;Drinks&quot; } }
SimpleXML Example 1  < ?php 2  3  $simplexml   =   simplexml_load_string ( '<?xml version=&quot;1.0&quot; ?><menus/>' ); 4  $simplexml -> addChild ( 'menu' , 'Lunch' ); 5  $simplexml -> addChild ( 'menu' , 'Dinner' ); 6  $simplexml -> addChild ( 'menu' , 'Drinks' ); 7  $simplexml -> addChild ( 'menu' , 'Dessert' ); 8  9  echo   $simplexml -> asXML ();
SimpleXML Example <?xml version=&quot;1.0&quot;?> <menus> <menu>Lunch</menu> <menu>Dinner</menu> <menu>Dessert</menu> <menu>Drinks</menu> </menus>
Serialised PHP ,[object Object]
Useful for values in database
Can also use to move data between PHP applications
Serialising Data in PHP 1  < ?php 2  3  $menu [ 'starter' ] =   array (   &quot;prawn cocktail&quot; , 4  &quot;soup of the day&quot; ); 5  $menu [ 'main course' ] =   array (   &quot;roast chicken&quot; , 6  &quot;fish 'n' chips&quot; , 7  &quot;macaroni cheese&quot; ); 8  $menu [ 'pudding' ] =   array (   &quot;cheesecake&quot; , 9  &quot;treacle sponge&quot; ); 10  11  echo serialize ( $menu ); a:3:{s:7:&quot;starter&quot;;a:2:{i:0;s:14:&quot;prawn cocktail&quot;;i:1;s:15:&quot;soup of the day&quot;;}s:11:&quot;main course&quot;;a:3:{i:0;s:13:&quot;roast chicken&quot;;i:1;s:14:&quot;fish'n' chips&quot;;i:2;s:15:&quot;macaroni cheese&quot;;}s:7:&quot;pudding&quot;;a:2:{i:0;s:10:&quot;cheesecake&quot;;i:1;s:14:&quot;treacle sponge&quot;;}}
Unserialising Data in PHP 1  < ?php 2  3  $serialised   =   'a:3:{s:7:&quot;starter&quot;;a:2:{i:0;s:14:&quot;prawn cocktail&quot;;i:1;s:15:&quot;soup of the day&quot;;}s:11:&quot;main course&quot;;a:3:{i:0;s:13:&quot;roast chicken&quot;;i:1;s:14:&quot;fish   apos; n apos;   chips&quot;;i:2;s:15:&quot;macaroni cheese&quot;;}s:7:&quot;pudding&quot;;a:2:{i:0;s:10:&quot;cheesecake&quot;;i:1;s:14:&quot;treacle sponge&quot;;}}' ; 4  5  var_dump ( unserialize ( $serialised ));
Unserialising Data in PHP array(3) { [&quot;starter&quot;]=> array(2) { [0]=> string(14) &quot;prawn cocktail&quot; [1]=> string(15) &quot;soup of the day&quot; } [&quot;main course&quot;]=> array(3) { [0]=> string(13) &quot;roast chicken&quot; [1]=> string(14) &quot;fish 'n' chips&quot; [2]=> string(15) &quot;macaroni cheese&quot; } [&quot;pudding&quot;]=> array(2) { [0]=> string(10) &quot;cheesecake&quot; [1]=> string(14) &quot;treacle sponge&quot; } }
Service Types
Service Types ,[object Object]
*-RPC ,[object Object]
JSON-RPC ,[object Object]
SOAP ,[object Object]
Defined XML format

More Related Content

What's hot

анатолий шарифулин Mojolicious
анатолий шарифулин Mojoliciousанатолий шарифулин Mojolicious
анатолий шарифулин Mojolicious
rit2010
 
49368010 projectreportontraininganddevelopment(1)
49368010 projectreportontraininganddevelopment(1)49368010 projectreportontraininganddevelopment(1)
49368010 projectreportontraininganddevelopment(1)
Kritika910
 
C A S Sample Php
C A S Sample PhpC A S Sample Php
C A S Sample Php
JH Lee
 

What's hot (9)

06_트위터, 콘텐츠 & 비즈니스 모델
06_트위터, 콘텐츠 & 비즈니스 모델06_트위터, 콘텐츠 & 비즈니스 모델
06_트위터, 콘텐츠 & 비즈니스 모델
 
Mojolicious. Веб в коробке!
Mojolicious. Веб в коробке!Mojolicious. Веб в коробке!
Mojolicious. Веб в коробке!
 
A Backbone.js Tutorial for the Impatient - Part 1
A Backbone.js Tutorial for the Impatient - Part 1A Backbone.js Tutorial for the Impatient - Part 1
A Backbone.js Tutorial for the Impatient - Part 1
 
web design and jQuery introduction in persian
web design and jQuery introduction in persianweb design and jQuery introduction in persian
web design and jQuery introduction in persian
 
13. view data
13. view data13. view data
13. view data
 
анатолий шарифулин Mojolicious
анатолий шарифулин Mojoliciousанатолий шарифулин Mojolicious
анатолий шарифулин Mojolicious
 
Using Geeklog as a Web Application Framework
Using Geeklog as a Web Application FrameworkUsing Geeklog as a Web Application Framework
Using Geeklog as a Web Application Framework
 
49368010 projectreportontraininganddevelopment(1)
49368010 projectreportontraininganddevelopment(1)49368010 projectreportontraininganddevelopment(1)
49368010 projectreportontraininganddevelopment(1)
 
C A S Sample Php
C A S Sample PhpC A S Sample Php
C A S Sample Php
 

Viewers also liked

Creating And Consuming Web Services In Php 5
Creating And Consuming Web Services In Php 5Creating And Consuming Web Services In Php 5
Creating And Consuming Web Services In Php 5
Michael Girouard
 

Viewers also liked (9)

xml rpc
xml rpcxml rpc
xml rpc
 
Uddi Protocol
Uddi ProtocolUddi Protocol
Uddi Protocol
 
Description of soa and SOAP,WSDL & UDDI
Description of soa and SOAP,WSDL & UDDIDescription of soa and SOAP,WSDL & UDDI
Description of soa and SOAP,WSDL & UDDI
 
XML-RPC (XML Remote Procedure Call)
XML-RPC (XML Remote Procedure Call)XML-RPC (XML Remote Procedure Call)
XML-RPC (XML Remote Procedure Call)
 
Creating And Consuming Web Services In Php 5
Creating And Consuming Web Services In Php 5Creating And Consuming Web Services In Php 5
Creating And Consuming Web Services In Php 5
 
Web service introduction
Web service introductionWeb service introduction
Web service introduction
 
Web Service Presentation
Web Service PresentationWeb Service Presentation
Web Service Presentation
 
Web Services PHP Tutorial
Web Services PHP TutorialWeb Services PHP Tutorial
Web Services PHP Tutorial
 
PHP and Web Services
PHP and Web ServicesPHP and Web Services
PHP and Web Services
 

Similar to Working with Web Services

Php Calling Operators
Php Calling OperatorsPhp Calling Operators
Php Calling Operators
mussawir20
 
Php Basic Security
Php Basic SecurityPhp Basic Security
Php Basic Security
mussawir20
 
Architecting Web Services
Architecting Web ServicesArchitecting Web Services
Architecting Web Services
Lorna Mitchell
 
Php Crash Course
Php Crash CoursePhp Crash Course
Php Crash Course
mussawir20
 
Intro to #memtech PHP 2011-12-05
Intro to #memtech PHP   2011-12-05Intro to #memtech PHP   2011-12-05
Intro to #memtech PHP 2011-12-05
Jeremy Kendall
 

Similar to Working with Web Services (20)

Php Calling Operators
Php Calling OperatorsPhp Calling Operators
Php Calling Operators
 
Php 3 1
Php 3 1Php 3 1
Php 3 1
 
Php Basic Security
Php Basic SecurityPhp Basic Security
Php Basic Security
 
Architecting Web Services
Architecting Web ServicesArchitecting Web Services
Architecting Web Services
 
Php Crash Course
Php Crash CoursePhp Crash Course
Php Crash Course
 
PHP Presentation
PHP PresentationPHP Presentation
PHP Presentation
 
Introduction To Lamp
Introduction To LampIntroduction To Lamp
Introduction To Lamp
 
03 Php Array String Functions
03 Php Array String Functions03 Php Array String Functions
03 Php Array String Functions
 
Php Training
Php TrainingPhp Training
Php Training
 
JSP Custom Tags
JSP Custom TagsJSP Custom Tags
JSP Custom Tags
 
Schenker - DSL for quickly creating web applications in Perl
Schenker - DSL for quickly creating web applications in PerlSchenker - DSL for quickly creating web applications in Perl
Schenker - DSL for quickly creating web applications in Perl
 
Neil Patel - What You Need to be Measuring and How to Do It
Neil Patel - What You Need to be Measuring and How to Do ItNeil Patel - What You Need to be Measuring and How to Do It
Neil Patel - What You Need to be Measuring and How to Do It
 
JQuery 101
JQuery 101JQuery 101
JQuery 101
 
Jenny Donnelly
Jenny DonnellyJenny Donnelly
Jenny Donnelly
 
Intro to #memtech PHP 2011-12-05
Intro to #memtech PHP   2011-12-05Intro to #memtech PHP   2011-12-05
Intro to #memtech PHP 2011-12-05
 
PHP - RAQ (Rarely Asked Questions!)
PHP - RAQ (Rarely Asked Questions!)PHP - RAQ (Rarely Asked Questions!)
PHP - RAQ (Rarely Asked Questions!)
 
10 Things You're Not Doing [IBM Lotus Notes Domino Application Development]
10 Things You're Not Doing [IBM Lotus Notes Domino Application Development]10 Things You're Not Doing [IBM Lotus Notes Domino Application Development]
10 Things You're Not Doing [IBM Lotus Notes Domino Application Development]
 
Forum Presentation
Forum PresentationForum Presentation
Forum Presentation
 
Mojolicious on Steroids
Mojolicious on SteroidsMojolicious on Steroids
Mojolicious on Steroids
 
Evolution of API With Blogging
Evolution of API With BloggingEvolution of API With Blogging
Evolution of API With Blogging
 

More from Lorna Mitchell

Understanding Distributed Source Control
Understanding Distributed Source ControlUnderstanding Distributed Source Control
Understanding Distributed Source Control
Lorna Mitchell
 

More from Lorna Mitchell (20)

OAuth: Trust Issues
OAuth: Trust IssuesOAuth: Trust Issues
OAuth: Trust Issues
 
Best Practice in API Design
Best Practice in API DesignBest Practice in API Design
Best Practice in API Design
 
Git, GitHub and Open Source
Git, GitHub and Open SourceGit, GitHub and Open Source
Git, GitHub and Open Source
 
Business 101 for Developers: Time and Money
Business 101 for Developers: Time and MoneyBusiness 101 for Developers: Time and Money
Business 101 for Developers: Time and Money
 
Things I wish web graduates knew
Things I wish web graduates knewThings I wish web graduates knew
Things I wish web graduates knew
 
Teach a Man To Fish (phpconpl edition)
Teach a Man To Fish (phpconpl edition)Teach a Man To Fish (phpconpl edition)
Teach a Man To Fish (phpconpl edition)
 
Web services tutorial
Web services tutorialWeb services tutorial
Web services tutorial
 
Join In With Joind.In
Join In With Joind.InJoin In With Joind.In
Join In With Joind.In
 
Tool Up Your LAMP Stack
Tool Up Your LAMP StackTool Up Your LAMP Stack
Tool Up Your LAMP Stack
 
Going Freelance
Going FreelanceGoing Freelance
Going Freelance
 
Understanding Distributed Source Control
Understanding Distributed Source ControlUnderstanding Distributed Source Control
Understanding Distributed Source Control
 
Best Practice in Web Service Design
Best Practice in Web Service DesignBest Practice in Web Service Design
Best Practice in Web Service Design
 
Coaching Development Teams: Teach A Man To Fish
Coaching Development Teams: Teach A Man To FishCoaching Development Teams: Teach A Man To Fish
Coaching Development Teams: Teach A Man To Fish
 
Zend Certification Preparation Tutorial
Zend Certification Preparation TutorialZend Certification Preparation Tutorial
Zend Certification Preparation Tutorial
 
Implementing OAuth with PHP
Implementing OAuth with PHPImplementing OAuth with PHP
Implementing OAuth with PHP
 
Web Services Tutorial
Web Services TutorialWeb Services Tutorial
Web Services Tutorial
 
Object Oriented Programming in PHP
Object Oriented Programming in PHPObject Oriented Programming in PHP
Object Oriented Programming in PHP
 
Example Presentation
Example PresentationExample Presentation
Example Presentation
 
Could You Telecommute?
Could You Telecommute?Could You Telecommute?
Could You Telecommute?
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 

Recently uploaded

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Recently uploaded (20)

ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 

Working with Web Services

  • 1. Working with Web Services Lorna Mitchell October 2009
  • 2.
  • 4. Personal site at lornajane.net
  • 5. European Rep for PHPWomen.org
  • 8.
  • 14.
  • 15. Formatted data instead of a web page
  • 16.
  • 17.
  • 18. Sharing information between systems cleanly
  • 19.
  • 21. Request and response, just like a web application
  • 23.
  • 24. We may not expect them
  • 26. Debug output and logging
  • 27. Verbose error-checking and logging from our app
  • 30.
  • 31. Natively read/write in most languages
  • 32. Very simple! (we like simple)
  • 33.
  • 34. no distinction between object and array
  • 35. Writing JSON from PHP 1 < ?php 2 3 $menu [ 'starter' ] = array ( &quot;prawn cocktail&quot; , 4 &quot;soup of the day&quot; ); 5 $menu [ 'main course' ] = array ( &quot;roast chicken&quot; , 6 &quot;fish 'n' chips&quot; , 7 &quot;macaroni cheese&quot; ); 8 $menu [ 'pudding' ] = array ( &quot;cheesecake&quot; , 9 &quot;treacle sponge&quot; ); 10 11 echo json_encode ( $menu ); {&quot;starter&quot;:[&quot;prawn cocktail&quot;,&quot;soup of the day&quot;],&quot;main course&quot;:[&quot;roast chicken&quot;,&quot;fish 'n' chips&quot;,&quot;macaroni cheese&quot;],&quot;pudding&quot;:[&quot;cheesecake&quot;,&quot;treacle sponge&quot;]}
  • 36. Reading JSON from PHP 1 < ?php 2 3 $json = '{&quot;starter&quot;:[&quot;prawn cocktail&quot;,&quot;soup of the day&quot;],&quot;main course&quot;:[&quot;roast chicken&quot;,&quot;fish apos; n apos; chips&quot;,&quot;macaroni cheese&quot;],&quot;pudding&quot;:[&quot;cheesecake&quot;,&quot;treacle sponge&quot;]}' ; 4 5 print_r ( json_decode ( $json ));
  • 37. Reading JSON from PHP stdClass Object ( [starter] => Array ( [0] => prawn cocktail [1] => soup of the day ) [main course] => Array ( [0] => roast chicken [1] => fish 'n' chips [2] => macaroni cheese ) [pudding] => Array ( [0] => cheesecake [1] => treacle sponge ) )
  • 38.
  • 40. Can give more detail than JSON
  • 41. Native read/write in most languages
  • 42.
  • 44. DOM
  • 45. SimpleXML Example 1 < ?php 2 3 $xml = <<< XML 4 < ?xml version = &quot;1.0&quot; ? > 5 < menus > 6 < menu > Lunch </ menu > 7 < menu > Dinner </ menu > 8 < menu > Dessert </ menu > 9 < menu > Drinks </ menu > 10 </ menus > 11 XML ; 12 13 $simplexml = new SimpleXMLElement ( $xml ); 14 var_dump ( $simplexml );
  • 46. SimpleXML Example object(SimpleXMLElement)#1 (1) { [&quot;menu&quot;]=> array(5) { [0]=> string(5) &quot;Lunch&quot; [1]=> string(6) &quot;Dinner&quot; [2]=> string(7) &quot;Dessert&quot; [3]=> string(6) &quot;Drinks&quot; } }
  • 47. SimpleXML Example 1 < ?php 2 3 $simplexml = simplexml_load_string ( '<?xml version=&quot;1.0&quot; ?><menus/>' ); 4 $simplexml -> addChild ( 'menu' , 'Lunch' ); 5 $simplexml -> addChild ( 'menu' , 'Dinner' ); 6 $simplexml -> addChild ( 'menu' , 'Drinks' ); 7 $simplexml -> addChild ( 'menu' , 'Dessert' ); 8 9 echo $simplexml -> asXML ();
  • 48. SimpleXML Example <?xml version=&quot;1.0&quot;?> <menus> <menu>Lunch</menu> <menu>Dinner</menu> <menu>Dessert</menu> <menu>Drinks</menu> </menus>
  • 49.
  • 50. Useful for values in database
  • 51. Can also use to move data between PHP applications
  • 52. Serialising Data in PHP 1 < ?php 2 3 $menu [ 'starter' ] = array ( &quot;prawn cocktail&quot; , 4 &quot;soup of the day&quot; ); 5 $menu [ 'main course' ] = array ( &quot;roast chicken&quot; , 6 &quot;fish 'n' chips&quot; , 7 &quot;macaroni cheese&quot; ); 8 $menu [ 'pudding' ] = array ( &quot;cheesecake&quot; , 9 &quot;treacle sponge&quot; ); 10 11 echo serialize ( $menu ); a:3:{s:7:&quot;starter&quot;;a:2:{i:0;s:14:&quot;prawn cocktail&quot;;i:1;s:15:&quot;soup of the day&quot;;}s:11:&quot;main course&quot;;a:3:{i:0;s:13:&quot;roast chicken&quot;;i:1;s:14:&quot;fish'n' chips&quot;;i:2;s:15:&quot;macaroni cheese&quot;;}s:7:&quot;pudding&quot;;a:2:{i:0;s:10:&quot;cheesecake&quot;;i:1;s:14:&quot;treacle sponge&quot;;}}
  • 53. Unserialising Data in PHP 1 < ?php 2 3 $serialised = 'a:3:{s:7:&quot;starter&quot;;a:2:{i:0;s:14:&quot;prawn cocktail&quot;;i:1;s:15:&quot;soup of the day&quot;;}s:11:&quot;main course&quot;;a:3:{i:0;s:13:&quot;roast chicken&quot;;i:1;s:14:&quot;fish apos; n apos; chips&quot;;i:2;s:15:&quot;macaroni cheese&quot;;}s:7:&quot;pudding&quot;;a:2:{i:0;s:10:&quot;cheesecake&quot;;i:1;s:14:&quot;treacle sponge&quot;;}}' ; 4 5 var_dump ( unserialize ( $serialised ));
  • 54. Unserialising Data in PHP array(3) { [&quot;starter&quot;]=> array(2) { [0]=> string(14) &quot;prawn cocktail&quot; [1]=> string(15) &quot;soup of the day&quot; } [&quot;main course&quot;]=> array(3) { [0]=> string(13) &quot;roast chicken&quot; [1]=> string(14) &quot;fish 'n' chips&quot; [2]=> string(15) &quot;macaroni cheese&quot; } [&quot;pudding&quot;]=> array(2) { [0]=> string(10) &quot;cheesecake&quot; [1]=> string(14) &quot;treacle sponge&quot; } }
  • 56.
  • 57.
  • 58.
  • 59.
  • 61. Also includes definition for error format
  • 62. Wrappers available for most languages
  • 63.
  • 64. Example WSDL <?xml version ='1.0' encoding ='UTF-8' ?> <definitions name='MyClass' targetNamespace='urn:MyClassInventory' xmlns:tns='urn:MyClassInventory' xmlns:soap=' http://schemas.xmlsoap.org/wsdl/soap/ ' xmlns:xsd=' http://www.w3.org/2001/XMLSchema ' xmlns:soapenc=' http://schemas.xmlsoap.org/soap/encoding/ ' xmlns:wsdl=' http://schemas.xmlsoap.org/wsdl/ ' xmlns='http://schemas.xmlsoap.org/wsdl/'> <message name='getAccountStatusRequest'> <part name='accountID' type='xsd:string'/> </message> <message name='getAccountStatusResponse'> <part name='accountID' type='xsd:string'/> <part name='counter' type='xsd:float' /> </message> <portType name='MyClassPortType'> <operation name='getAccountStatus'> <input message='tns:getAccountStatusRequest'/> <output message='tns:getAccountStatusResponse'/> </operation> </portType> <binding name='MyClassBinding' type='tns:MyClassPortType'> <soap:binding style='rpc' transport='http://schemas.xmlsoap.org/soap/http'/> <operation name='getAccountStatus'> <soap:operation soapAction='urn:xmethods-delayed-quotes#getAccountStatus'/> <input> <soap:body use='encoded' namespace='urn:xmethods-delayed-quotes' encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/> </input> <output> <soap:body use='encoded' namespace='urn:xmethods-delayed-quotes' encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/> </output> </operation> </binding> <service name='MyClassService'> <port name='MyClassPort' binding='tns:MyClassBinding'> <soap:address location='http://rivendell.local:10002/MyClassServiceServer.php'/> </port> </service> </definitions>
  • 65.
  • 66. Method names and bindings
  • 68. Variable names and data types used for each request/response
  • 71. PHP SOAP Client Example 1 < ?php 2 3 ini_set ( 'soap.wsdl_cache_enabled' , '0' ); 4 5 require_once ( 'lib/Snapshot.php' ); 6 7 $wsdl = &quot;Service.wsdl&quot; ; 8 $client = new SoapClient ( $wsdl , $params ); 9 10 $output = $client -> requestShot ( 11 'http://www.php.net' , '' , 300 , 400 );
  • 72.
  • 73. Data types can be an issue between different client/server languages
  • 74.
  • 75.
  • 80.
  • 81. -v to show request/response
  • 82. -I to show response headers
  • 83. -X to specify HTTP method
  • 84. -d to add a data field
  • 85. -c to store cookies in a cookiejar
  • 86. -b to use a cookiejar with request
  • 91.
  • 93. Call function with arguments
  • 94.
  • 95. Test method: just echoes back to user
  • 97. Flickr Echo Example: XML <?xml version = &quot;1.0&quot;?> <methodCall> <methodName> flickr.test.echo </methodName> <params> <param> <value> <struct> <member> <name> api_key </name> <value>....</value> </member> </struct> </value> </param> </params> </methodCall>
  • 98. RPC from PHP: curl 1 < ?php 2 3 // $xml is existing SimpleXMLElement Object 4 $url = 'http://api.flickr.com/services/xmlrpc/' ; 5 $ch = curl_init ( $url ); 6 7 curl_setopt ( $ch , CURLOPT_POST , 1 ); 8 curl_setopt ( $ch , CURLOPT_POSTFIELDS , $xml -> asXML ()); 9 10 $response = curl_exec ( $ch ); 11 curl_close ( $ch );
  • 99. RPC from PHP: pecl_http 1 < ?php 2 3 $url = 'http://api.flickr.com/services/xmlrpc/' ; 4 5 // procedural method 6 $response = http_post_data ( $url , $xml -> asXML ()); 7 8 // alternative method 9 $request = new HTTPRequest ( $url , HTTP_METH_POST ); 10 $request -> setRawPostData ( $xml -> asXML ()); 11 $request -> send (); 12 $response = $request -> getResponseBody (); 13 14 var_dump ( $response );
  • 100. Flickr Response <?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?> <methodResponse> <params> <param> <value> <string>&lt;api_key&gt;54rt346&lt;/api_key&gt; </string> </value> </param> </params> </methodResponse>
  • 101.
  • 102. Can easily wrap existing libraries to call like this
  • 103. Can wrap an interface to an RPC service to look like a library
  • 104. Wrapping RPC Example 1 < ?php 2 3 class Handler 4 { 5 function __call ( $method , $args ) { 6 $ch = curl_init ( 'http://localhost' ); 7 $data [ 'method' ] = $method ; 8 foreach ( $args as $a ) $data [ $a ] = $a ; 9 10 curl_setopt ( $ch , CURLOPT_POST , 1 ); 11 curl_setopt ( $ch , CURLOPT_POSTFIELDS , $data ); 12 $response = curl_exec ( $ch ); curl_close ( $ch ); 13 14 var_dump ( $response ); 15 } 16 } 17 $h = new Handler (); 18 $h -> dance ( 'cat' , 'dog' , 'rabbit' , 'penguin' ); 19
  • 105.
  • 106. Can you get a response from the service?
  • 107. Can you log in?
  • 108. Is there an error?
  • 109. Try to debug the details of the request/response
  • 110.
  • 112. More like a philosophy http://www.flickr.com/photos/katiew/191128057/
  • 113.
  • 114. Generally uses HTTP (HyperText Transfer Protocol)
  • 115. URLs are resource locations
  • 116. Verbs tell the service what to do
  • 117. Status codes indicate what the outcome was
  • 118. HTTP Status Codes Code Meaning 200 OK 302 Found 301 Moved 401 Not Authorised 403 Forbidden 404 Not Found 500 Internal Server Error
  • 119. REST CRUD Action HTTP Verb Retrieve GET Create POST Update PUT Delete DELETE
  • 120.
  • 121.
  • 122. REST from PHP: GET 1 < ?php 2 3 $result = file_get_contents ( 'http://localhost/users' ); 4 var_dump ( $result );
  • 123.
  • 124. use CURLOPT_RETURNTRANSFER to capture it instead 1 < ?php 2 3 $ch = curl_init ( 'http://localhost/users' ); 4 5 curl_exec ( $ch );
  • 125. REST from PHP: POST 1 < ?php 2 3 $ch = curl_init ( 'http://localhost/users' ); 4 5 $data = array ( &quot;name&quot; => &quot;Cho Chang&quot; , 6 &quot;house&quot; => &quot;Ravenclaw&quot; ); 7 8 curl_setopt ( $ch , CURLOPT_POST , 1 ); 9 curl_setopt ( $ch , CURLOPT_POSTFIELDS , $data ); 10 11 curl_exec ( $ch );
  • 126. REST from PHP: PUT 1 < ?php 2 3 $ch = curl_init ( 'http://localhost/users/cho' ); 4 5 $data = array ( &quot;name&quot; => &quot;Cho Chang&quot; , 6 &quot;house&quot; => &quot;Ravenclaw&quot; 7 &quot;age&quot; => 15 ); 8 9 curl_setopt ( $handle , CURLOPT_CUSTOMREQUEST , 'PUT' ); 10 curl_setopt ( $ch , CURLOPT_POSTFIELDS , $data ); 11 12 curl_exec ( $ch ); 13
  • 127. REST from PHP: DELETE 1 < ?php 2 3 $ch = curl_init ( 'http://localhost/users/ginny' ); 4 5 curl_setopt ( $ch , CURLOPT_CUSTOMREQUEST , 'DELETE' ); 6 7 curl_exec ( $ch );
  • 128.
  • 129. Is there a status code?
  • 130. What does the response body look like?
  • 133. Working with Web Services
  • 134.
  • 140.
  • 141. If you control both server and client, add debug output
  • 142. Use proxies to record what happens
  • 143. Start with the lowest common denominator and work up
  • 144.  

Editor's Notes

  1. can independently scale those modular elements
  2. 15 mins
  3. parse_str(file_get_contents(&amp;quot;php://input&amp;quot;),$post_vars);