DPC 2007 My First Mashup (Cal Evans)


Published on

Dutch PHP Conference 2007

Published in: Business, Technology
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

DPC 2007 My First Mashup (Cal Evans)

  1. 1. My First Mashup Cal Evans (cal@zend.com) Zend Technologies, Inc. Editor-in-Chief http://devzone.zend.com
  2. 2. What We Will Cover <ul><li>This session will show you some of the issues you will deal with in consuming web services from your web page. </li></ul><ul><li>We will create the same Mashup three times, once in PHP and then twice in Javascript. </li></ul>
  3. 3. Backstory <ul><li>Why? </li></ul>
  4. 4. Our First Mashup <ul><li>Combine APIs from UPS and Google </li></ul><ul><li>Allow a user to not only track a package via UPS but also to see it’s progress plotted on a map. </li></ul><ul><li>It’s a cute mashup and almost useful </li></ul>
  5. 5. UPS Access Key <ul><li>Requires a sacrifice of a small furry animal (not really) </li></ul><ul><ul><li>Start Here http://www.ups.com/content/us/en/bussol/offering/technology/automated_shipping/online_tools.html </li></ul></ul><ul><ul><li>Get an online account </li></ul></ul><ul><ul><li>Get an access key </li></ul></ul><ul><ul><li>Get a key for the service you want to access </li></ul></ul><ul><ul><li>Find the documentation for the service </li></ul></ul>
  6. 6. Message TO UPS <ul><li><?xml version=&quot;1.0&quot;?> </li></ul><ul><li><AccessRequest xml:lang=&quot;en-US&quot;> </li></ul><ul><li><AccessLicenseNumber> YOURACCESSLICENSENUMBER </AccessLicenseNumber> </li></ul><ul><li><UserId> YOURUSERID </UserId> </li></ul><ul><li><Password> YOURPASSWORD </Password> </li></ul><ul><li></AccessRequest> </li></ul><ul><li><?xml version=&quot;1.0&quot;?> </li></ul><ul><li><TrackRequest xml:lang=&quot;en-US&quot;> </li></ul><ul><li><Request> </li></ul><ul><li><TransactionReference> </li></ul><ul><li><CustomerContext> Example 2 </CustomerContext> </li></ul><ul><li><XpciVersion>1.0001</XpciVersion> </li></ul><ul><li></TransactionReference> </li></ul><ul><li><RequestAction>Track</RequestAction> </li></ul><ul><li><RequestOption>activity</RequestOption> </li></ul><ul><li></Request> </li></ul><ul><li><ShipmentIdentificationNumber> </li></ul><ul><li>UPS PACKAGE ID </li></ul><ul><li></ShipmentIdentificationNumber> </li></ul><ul><li></TrackRequest> </li></ul>
  7. 7. Response FROM UPS <ul><li>Way too verbose to show here. (1 package generated 2,128 lines of XML) </li></ul><ul><li>Contains more information than we need for this project </li></ul><ul><li>Can be parsed easily thanks to the miracle of SimpleXML in PHP. </li></ul><ul><li>$xml = new SimpleXMLElement($x); foreach ($xml->xpath('//Activity') as $y) { // doStuff() } </li></ul>
  8. 8. Let’s play with some code <ul><li>raw.php </li></ul><ul><li>A simple program that builds an XML message in a string, transmits it to UPS, and parses the response for display. </li></ul>
  9. 9. Wasn’t that exciting <ul><li>Transactions like this are very easy with Zend_Rest_Client </li></ul><ul><li>Transactions like this are real easy with SimpleXML </li></ul>
  10. 10. GoogleMaps API <ul><li>The original Mashup API. </li></ul><ul><li>Easy to use </li></ul><ul><li>Extremely easy to get approval for </li></ul><ul><li>We use 2 Google APIs in this Mashup </li></ul><ul><ul><li>New-ish Geocoding API </li></ul></ul><ul><ul><li>Mapping API </li></ul></ul>
  11. 11. Google Access Key <ul><li>Easy to get. </li></ul><ul><li>Visit http://www.google.com/apis/maps/signup.html </li></ul><ul><li>Enter your URL </li></ul><ul><li>Copy the key they give you and save it in your code. </li></ul><ul><li>It’s really that simple. </li></ul>
  12. 12. Geocode Message TO Google <ul><li>Documentation http://www.google.com/apis/maps/documentation/#Geocoding_Etc </li></ul><ul><li>HTTP Request </li></ul><ul><li>http://maps.google.com/maps/geo?key=YOURGOOGLEKEYHERE&q=19200+Stevens+Creek+Blvd+Santa+Clara+CA+95014&output=json </li></ul><ul><li>q -- The address that you want to geocode. </li></ul><ul><li>key -- Your API key. </li></ul><ul><li>output -- The format in which the output should be generated. The options are xml, kml, csv, or json. </li></ul><ul><li>KML, or Keyhole Markup Language, is an XML grammar and format for modeling and storing geographic features such as points, lines, images, and polygons for display in Google Earth™, Google Maps™, and Google Maps for mobile. </li></ul>
  13. 13. Geocode Message FROM Google <ul><li>{ </li></ul><ul><li>&quot;name&quot;: &quot;1600 Amphitheatre Parkway, Mountain View, CA, USA&quot;, </li></ul><ul><li>&quot;Status&quot;: { </li></ul><ul><li>&quot;code&quot;: 200, </li></ul><ul><li>&quot;request&quot;: &quot;geocode&quot; </li></ul><ul><li>}, </li></ul><ul><li>&quot;Placemark&quot;: [ </li></ul><ul><li>{ </li></ul><ul><li>&quot;address&quot;: &quot;1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA&quot;, </li></ul><ul><li>&quot;AddressDetails&quot;: { </li></ul><ul><li>&quot;Country&quot;: { </li></ul><ul><li>&quot;CountryNameCode&quot;: &quot;US&quot;, </li></ul><ul><li>&quot;AdministrativeArea&quot;: { </li></ul><ul><li>&quot;AdministrativeAreaName&quot;: &quot;CA&quot;, </li></ul><ul><li>&quot;SubAdministrativeArea&quot;: { </li></ul><ul><li>&quot;SubAdministrativeAreaName&quot;: &quot;Santa Clara&quot;, </li></ul><ul><li>&quot;Locality&quot;: { </li></ul><ul><li>&quot;LocalityName&quot;: &quot;Mountain View&quot;, </li></ul><ul><li>&quot;Thoroughfare&quot;: { </li></ul><ul><li>&quot;ThoroughfareName&quot;: &quot;1600 Amphitheatre Pkwy&quot; </li></ul><ul><li>}, </li></ul><ul><li>&quot;PostalCode&quot;: { </li></ul><ul><li>&quot;PostalCodeNumber&quot;: &quot;94043&quot; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>}, </li></ul><ul><li>&quot;Accuracy&quot;: 8 </li></ul><ul><li>}, </li></ul><ul><li>Point: { </li></ul><ul><li>coordinates: [-122.083739, 37.423021, 0] </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>] </li></ul><ul><li>} </li></ul>
  14. 14. Google Map Interface <ul><li>Gets around the AJAX domain limitation by including Javascript from their server. </li></ul><ul><li><script src=&quot;http://maps.google.com/maps?file=api&amp;v=2&amp;key= abcdefg &quot; type=&quot;text/javascript&quot;></script> </li></ul><ul><li><script type=&quot;text/javascript&quot;> </li></ul><ul><li>Simple implementation </li></ul><ul><li>function load() { if (GBrowserIsCompatible()) { var map = new GMap2(document.getElementById(&quot;map&quot;)); map.setCenter(new GLatLng(37.4419, -122.1419), 13); } } </li></ul><ul><li></script> </li></ul><ul><li>The onload method builds the map </li></ul><ul><li><body onload=&quot;load()&quot; onunload=&quot;GUnload()&quot;> </li></ul><ul><li><div id=&quot;map&quot; style=&quot;width: 500px; height: 300px&quot;></div> </li></ul><ul><li></body> </li></ul>
  15. 15. First (real) Version <ul><li>Traditional forms based POST </li></ul><ul><li>All interaction is done at the server level </li></ul><ul><li>Seriously OLD SCHOOL Style Mashup. </li></ul>
  16. 16. First Version (PHP) <ul><li>GoogleMapAPI.class.php By: Monte Ohrt http://www.phpinsider.com/php/code/GoogleMapAPI/ Handles all of the interaction with Google and builds all necessary JavaScript to display our map and points </li></ul><ul><li>ups_tracker.class.php By Sergey Shilko http://www.phpclasses.org/browse/package/3031.html Handles all of the interaction with UPS. </li></ul>
  17. 17. Let’s play with some code <ul><li>trackpak.php </li></ul><ul><li>A full-featured version of our mashup that will query UPS for the package activity, strip out the cities, geocode them and plot them on a GoogleMap. This version uses the default output options from the chosen classes which results in very little custom code. </li></ul>
  18. 18. Wasn’t that exciting <ul><li>Using pre-packaged objects made this easy to write. </li></ul><ul><li>BAD PRACTICE: Don’t modify someone else’s code, subclass the class and extend it with your code. </li></ul><ul><li>BAD PRACTICE: Don’t output HTML directly from your class. Use a decorator class. </li></ul>
  19. 19. Second Version <ul><li>SJAX So that we can easily track what is going on, we don’t use Asynchronous calls </li></ul><ul><li>Web 1.9 To be truly Web 2.0 you have to use Async. (and you have to have rounded corners.) </li></ul><ul><li>Proxied Since AJAX calls can only be made to the server that served the page, all calls off server have to go through a proxy. </li></ul>
  20. 20. Second Version (JavaScript) <ul><li>Prototype.js http://www.prototypejs.org/ All AJAX and some extra utilities that just make life easier. </li></ul><ul><li>Procedural in nature. To make it easy to follow The Javascript code is mostly procedural. </li></ul>
  21. 21. Second Version (PHP) <ul><li>Proxy.php </li></ul><ul><li>Procedural This is a single use design. It’s only purpose is to serve this demo. It’s basically one big switch. </li></ul>
  22. 22. Let’s play with some code <ul><li>SJAX.php </li></ul><ul><li>First predominantly JavaScript version of the Mashup. This version talks to the proxy to get it’s data and processes it accordingly. </li></ul>
  23. 23. Wasn’t that exciting <ul><li>Separation of logic achieved by moving data processing duties to proxy.php and using JavaScript for the display logic. </li></ul><ul><li>Takes the X out of SJAX because we use JSON. (But SJAJ doesn’t sound as cool) </li></ul><ul><li>Synchronous is harder than Old School but still easy to read because it’s single threaded. </li></ul>
  24. 24. Third Version <ul><li>Put the A back in AJAX Watch the cross-browser nuttiness. </li></ul><ul><li>Web 2.0 Or as close to Web 2.0 as I get. (I don’t do rounded corners) </li></ul><ul><li>Still Proxied </li></ul>
  25. 25. Third Version (JavaScript) <ul><li>Prototype.js http://prototype.conio.net/ All AJAX and some utilities </li></ul><ul><li>Almost fully Object Oriented Javascript. </li></ul><ul><li>Most AJAX is Asynchronous calls. </li></ul>
  26. 26. Let’s play with some code <ul><li>AJAX.php </li></ul><ul><li>Second JavaScript version of the Mashup. This version talks to the proxy to get it’s data and processes it accordingly but in an asynchronous manner. This has ramifications in some browsers. (hint: I.E.) </li></ul>
  27. 27. Wasn’t that exciting <ul><li>I.E. returns the ajax calls in random order. This means that we have to be able to handle the data in whatever order it comes back from the server. </li></ul><ul><li>For some reason this is not an issue with FireFox. </li></ul><ul><li>To solve this problem we use PointManager class which is a Singleton Pattern. </li></ul>
  28. 28. Additional Reading <ul><li>Understanding AJAX: Joshua Eichorn http://www.amazon.com/gp/explorer/0132216353/2/ref=pd_lpo_ase/104-3004796-3584761 ? </li></ul><ul><li>Web APIs with PHP: Paul Reinheimer http://www.amazon.com/Professional-Web-APIs-PHP-Paypal/dp/0764589547/ref=sr_11_1/104-3004796-3584761?ie=UTF8 </li></ul>
  29. 29. Resources <ul><li>www.programmableweb.com </li></ul><ul><li>www.prototypejs.org </li></ul><ul><li>www.ajaxian.com </li></ul><ul><li>http://therichwebexperience.com/show_view.jsp?showId=60 </li></ul><ul><li>http://developer.yahoo.com/ </li></ul><ul><li>http://devzone.zend.com/public/view </li></ul><ul><li>PHP Abstract Podcast </li></ul>
  30. 30. My First Mashup <ul><li>Questions? </li></ul>
  31. 31. Vanity Slide <ul><li>Cal Evans </li></ul><ul><li>Editor-in-Chief, DevZone </li></ul><ul><li>Zend Technologies, Inc. </li></ul><ul><li>http://devzone.zend.com </li></ul><ul><li>http://www.calevans.com </li></ul><ul><li>[email_address] </li></ul>