SlideShare uma empresa Scribd logo
1 de 43
Baixar para ler offline
Consuming Web Services
     Using PHP 5
             Adam Trachtenberg
  Senior Manager of Platform Evangelism, eBay
Plan of Attack


REST
SOAP
Weapons

PHP 5
SimpleXML
Streams
ext/soap
REST


URL + HTTP Verb -> XML Reply
Thai Food near 94306
http://api.example.com/yellowpages?zip=94306&query=thai+restaurant
<ResultSet>
   <Result>
       <Title>Thai City</Title>
       <Address>3691 El Camino Real</Address>
       <City>Palo Alto</City>
       <State>CA</State>
       <Phone>(650) 493-0643</Phone>
       <BusinessUrl>http://www.thaicity.com/</BusinessUrl>
   </Result>
   <Result>
       <Title>Thai Garden Restaurant</Title>
       <Address>4329 El Camino Real</Address>
       …
   </Result>
</ResultSet>
REST (In Theory)

   SQL     REST
  CREATE   POST
  SELECT    GET
  UPDATE    PUT
  DELETE   DELETE
REST (In Practice)

    SQL     REST
   CREATE   GET
   SELECT   GET
   UPDATE   GET
   DELETE   GET
del.icio.us

• Social bookmarking site
• Save pages for yourself
• Share pages with others
• Provide meta-data via folksonomy
• Easy to insert and query the site
del.icio.us/rss

• RSS feeds for everything
• Prepend rss before path
• http://del.icio.us/popular/ebay
• http://del.icio.us/rss/popular/ebay
<rdf:RDF>
   <channel rdf:about="http://del.icio.us/popular/ebay">...</channel>
   <item rdf:about="http://the-winning-bid.com/">
       <title>The-Winning-Bid.com</title>
       <link>http://the-winning-bid.com/</link>
       <dc:creator>minenet</dc:creator>
       <dc:date>2005-10-10T03:40:09Z</dc:date>
   </item>

   <item rdf:about="http://ebaysupplies.usps.com/">
       <title>USPS | eBay</title>
       <link>http://ebaysupplies.usps.com/</link>
       <dc:creator>kresston</dc:creator>
       <dc:date>2004-11-17T14:51:34Z</dc:date>
   </item>

...
</rdf:RDF>
RSS + SimpleXML
$url = 'http://del.icio.us/rss/popular/ebay';

$xml = simplexml_load_file($url);

foreach ($xml->item as $item) {
  printf("%20.20s : %-30.30sn",
    (string) $item->title, (string) $item->link);
}
Popular eBay Pages

 The-Winning-Bid.com   :   http://64.34.178.175/results.p
         USPS | eBay   :   http://ebaysupplies.usps.com/
          GarageSale   :   http://www.iwascoding.com/Gara
:: gumshoo - eBay se   :   http://www.gumshoo.com/
mapBid - View eBay a   :   http://www.mapbid.com/
Auction Mapper | Vis   :   http://www.auctionmapper.com/
ebay api developer d   :   http://developer.ebay.com/
         what is it?   :   http://www.whatis-it.com/
eBay Developer Chall   :   http://ebay.promotionexpert.co
  ebay + google maps   :   http://www.markovic.com/markov
eBay drops charges f   :   http://www.cbronline.com/artic
Mark Pincus Blog: Sh   :   http://markpincus.typepad.com/
RSS + SimpleXML

foreach (simplexml_load_file(
   'http://del.icio.us/rss/popular/ebay')->item as $item) {
      printf("%20.20s : %-30.30sn",
         (string) $item->title, (string) $item->link);
}
REST + GET


• URL + query string
• Test in Firefox
• Still process XML
flickr

• Social digital photo site
• Save photos for yourself
• Share photos with others
• Provide meta-data via folksonomy
• Easy to insert and query the site
flickr/services/rest


• Multiple REST API calls
• API decoupled from site URL structure
• flickr.com/services/rest/?method=...
flickr + GET
Requires developer authentication token
Search flickr
http://www.flickr.com/services/api/
flickr.photos.search.html
http://www.flickr.com/services/rest/?
 method=flickr.photos.search&
 tags=ebay&
 api_key=giantlonguglystring
<?xml version="1.0" encoding="utf-8" ?>
<rsp stat="ok">
  <photos page="1" pages="75"
  perpage="100" total="7422">
    <photo id="71550241"
    owner="27503448@N00"
    secret="51fb62fb95" server="35"
    title="champ1" ispublic="1" isfriend="0"
    isfamily="0"/>
    ...
  </photos>
</rsp>
GET a Request
$base =
  'http://www.flickr.com/services/rest/?';
$qs = array(
  'method' => 'flickr.photos.search',
  'api_key' => 'biglonguglystring',
  'tags' => 'ebay',
  );

$url = $base . http_build_query($qs);
$out = file_get_contents($url);
Create a Gallery
$xml = simplexml_load_string($out);
foreach ($xml->photos->photo as $photo) {
  $server = (string) $photo['server'];
  $id = (string) $photo['id'];
  $secret = (string) $photo['secret'];
  $img .= "<img src="http://static.flickr.com/
   {$server}/{$id}_{$secret}_s.jpg"/>";
}

print $img;
flickr/ebay
REST + POST

• URL + POST Data
• Can’t test in Firefox
• POST body can be anything
• Return data is XML
flickr + POST

• Requires developer authentication token
• Requires user authentication token
• Add tags to a photo
• http://www.flickr.com/services/api/
  flickr.photos.addTags.html
Define POST Data
$url =
  'http://www.flickr.com/services/rest/?';
$qs = array(
  'method' => 'flickr.photos.addTags',
  'api_key' => 'biglonguglystring',
  'auth_token' => 'anotherbiglonguglystring',
  'tags' => 'hongkong',
  'photo_id' => '50021321'
  );
Compute the Signature
$api_sig =
  'would_you_believe_another_long_string?';

ksort($qs);
foreach ($qs as $k => $v) {
  $api_sig .= $k . $v;
}

$qs['api_sig'] = md5($api_sig);
POST a Request
$content = http_build_query($qs);
$options = array(
   'http' => array(
      'method' => 'POST',
      'content' => $content
   )
);

$context = stream_context_create($options);
$out = file_get_contents($url, false, $context);
<?xml version="1.0" encoding="utf-8" ?>
<rsp stat="ok">
</rsp>
SOAP

• Leaky abstraction around XML
• Mapped conversion of native data types to
  XML Schema types and vice versa
• Independent of HTTP
• API described using WSDL
Use ext/soap

• Bundled with PHP 5
• Enabled by default in PHP 5.1
• Written in C not PHP
• Most compatible with other SOAP servers
• Actively maintained
eBay

• Social marketplace site
• Buy items for yourself
• Sell items to others
• Provide meta-data via attributes
• Easy to insert and query the site
api.ebay.com

• Totally decoupled from eBay
• SOAP interface
• Requires developer and user authentication
• Testing and production environments
Search eBay
// Create and configure session
$session = new eBaySession('long_string',
   'another_long_string', 'ya_long_string');
$session->token = 'one_more_long_string';
$session->site = 100; // 100 = Motors;
$session->location =
   https://api.ebay.com/wsapi;
Baby,You Can Find My Car
 try {
    $client = new eBaySOAP($session);
    $params = array(
      'Version' => 435,
      'Query' => '*',
      'CategoryID' => 6001); // Cars
    $results =
      $client->GetSearchResults($params);
 } catch (SOAPFault $f) {
 }
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns1="urn:ebay:apis:eBLBaseComponents">
   <SOAP-ENV:Header>
        <ns1:RequesterCredentials>
            <ns1:eBayAuthToken>one_more_long_string</ns1:eBayAuthToken>
            <ns1:Credentials>
                <ns1:AppId>one_long_string</ns1:AppId>
                <ns1:DevId>another_long_string</ns1:DevId>
                <ns1:AuthCert>ya_long_string</ns1:AuthCert>
            </ns1:Credentials>
        </ns1:RequesterCredentials>
   </SOAP-ENV:Header>
   <SOAP-ENV:Body>
        <ns1:GetSearchResultsRequest>
            <ns1:Version>425</ns1:Version>
            <ns1:Query>*</ns1:Query>
            <ns1:CategoryID>6001</ns1:CategoryID>
            <ns1:TotalOnly>true</ns1:TotalOnly>
        </ns1:GetSearchResultsRequest>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope
    xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soapenv:Body>
        <GetSearchResultsResponse xmlns="urn:ebay:apis:eBLBaseComponents">
            <Timestamp>2005-12-09T02:35:57.881Z</Timestamp>
            <Ack>Success</Ack>
            <Version>437</Version>
            <Build>e437_core_Bundled_2112013_R1</Build>
            <ItemsPerPage>100</ItemsPerPage>
            <PageNumber>1</PageNumber>
            <HasMoreItems>true</HasMoreItems>
            <PaginationResult>
                <TotalNumberOfPages>294</TotalNumberOfPages>
                <TotalNumberOfEntries>29335</TotalNumberOfEntries>
            </PaginationResult>
            <CategoryArray/>
        </GetSearchResultsResponse>
        </soapenv:Body>
</soapenv:Envelope>
$total = number_format(
  $results->PaginationResult
           ->TotalNumberOfEntries);

print "There are {$total} passenger vehicles
  for sale on eBay Motors";
eBay Motors Google Maps
What I’ve learned
• Web services are closed source software
• Documentation and online support is vital
• Debugging is hard
• SOAP sucks! SOAP rocks!
• SOAP interoperability is an issue
• Authentication is ad-hoc
Tips and Tricks

• You’re not querying a local MySQL database
• Cache your data
• Use debugging methods
• Sniff the wire
• Send requests to your own server
It Only Looks Simple
• Web services == (HTTP && XML) != PHP
• You must grok
 • HTTP
 • XML
 • XML Namespaces (Danger!)
 • XML Schema
 • XPath (I <heart> XPath)
 • SOAP
• The data is layered
What Shape Is Your Data?
• REST
 • Angled = <> = XML Database
 • FLOWR: /Items/Item[Price < 100]/ItemID
• SOAP
 • Square = [] = Relational database
 • SQL: SELECT ItemID FROM Items
    WHERE Items.Price < 100
References
• del.icio.us: http://del.icio.us/help/
 • RSS, HTML, Javascript, JSON, IE Active
    Channel, API (REST + GET)
• flickr: http://www.flickr.com/services/api/
 • RSS, Atom, REST, XML-RPC, SOAP
• eBay: http://developer.ebay.com
 • RSS, REST, XML API, SOAP, .NET SDK, Java
    SDK
Questions?
http://www.trachtenberg.com/talks/apachecon2005

Mais conteúdo relacionado

Mais procurados

Rails 3 overview
Rails 3 overviewRails 3 overview
Rails 3 overview
Yehuda Katz
 
Rails 3: Dashing to the Finish
Rails 3: Dashing to the FinishRails 3: Dashing to the Finish
Rails 3: Dashing to the Finish
Yehuda Katz
 
Enter the app era with ruby on rails
Enter the app era with ruby on railsEnter the app era with ruby on rails
Enter the app era with ruby on rails
Matteo Collina
 
cdac@parag.gajbhiye@test123
cdac@parag.gajbhiye@test123cdac@parag.gajbhiye@test123
cdac@parag.gajbhiye@test123
Parag Gajbhiye
 

Mais procurados (20)

Rails 3 overview
Rails 3 overviewRails 3 overview
Rails 3 overview
 
Spark IT 2011 - Simplified Web Development using Java Server Faces 2.0
Spark IT 2011 - Simplified Web Development using Java Server Faces 2.0Spark IT 2011 - Simplified Web Development using Java Server Faces 2.0
Spark IT 2011 - Simplified Web Development using Java Server Faces 2.0
 
Rails 3: Dashing to the Finish
Rails 3: Dashing to the FinishRails 3: Dashing to the Finish
Rails 3: Dashing to the Finish
 
Keeping the frontend under control with Symfony and Webpack
Keeping the frontend under control with Symfony and WebpackKeeping the frontend under control with Symfony and Webpack
Keeping the frontend under control with Symfony and Webpack
 
jQuery UI and Plugins
jQuery UI and PluginsjQuery UI and Plugins
jQuery UI and Plugins
 
Django Heresies
Django HeresiesDjango Heresies
Django Heresies
 
Childthemes ottawa-word camp-1919
Childthemes ottawa-word camp-1919Childthemes ottawa-word camp-1919
Childthemes ottawa-word camp-1919
 
Enter the app era with ruby on rails
Enter the app era with ruby on railsEnter the app era with ruby on rails
Enter the app era with ruby on rails
 
Introduction to plugin development
Introduction to plugin developmentIntroduction to plugin development
Introduction to plugin development
 
Python Code Camp for Professionals 1/4
Python Code Camp for Professionals 1/4Python Code Camp for Professionals 1/4
Python Code Camp for Professionals 1/4
 
AtlasCamp 2015: Using add-ons to build add-ons
AtlasCamp 2015: Using add-ons to build add-onsAtlasCamp 2015: Using add-ons to build add-ons
AtlasCamp 2015: Using add-ons to build add-ons
 
Python Code Camp for Professionals 2/4
Python Code Camp for Professionals 2/4Python Code Camp for Professionals 2/4
Python Code Camp for Professionals 2/4
 
OSCON Google App Engine Codelab - July 2010
OSCON Google App Engine Codelab - July 2010OSCON Google App Engine Codelab - July 2010
OSCON Google App Engine Codelab - July 2010
 
Oracle APEX Performance
Oracle APEX PerformanceOracle APEX Performance
Oracle APEX Performance
 
cdac@parag.gajbhiye@test123
cdac@parag.gajbhiye@test123cdac@parag.gajbhiye@test123
cdac@parag.gajbhiye@test123
 
Spring batch
Spring batchSpring batch
Spring batch
 
Python Code Camp for Professionals 3/4
Python Code Camp for Professionals 3/4Python Code Camp for Professionals 3/4
Python Code Camp for Professionals 3/4
 
Maintainable JavaScript 2012
Maintainable JavaScript 2012Maintainable JavaScript 2012
Maintainable JavaScript 2012
 
Python Code Camp for Professionals 4/4
Python Code Camp for Professionals 4/4Python Code Camp for Professionals 4/4
Python Code Camp for Professionals 4/4
 
Api
ApiApi
Api
 

Semelhante a ApacheCon 2005

Dirty Secrets of the PHP SOAP Extension
Dirty Secrets of the PHP SOAP ExtensionDirty Secrets of the PHP SOAP Extension
Dirty Secrets of the PHP SOAP Extension
Adam Trachtenberg
 
Integration Test Cucumber + Webrat + Selenium
Integration Test Cucumber + Webrat + SeleniumIntegration Test Cucumber + Webrat + Selenium
Integration Test Cucumber + Webrat + Selenium
tka
 
Socket applications
Socket applicationsSocket applications
Socket applications
João Moura
 
PHP and Rich Internet Applications
PHP and Rich Internet ApplicationsPHP and Rich Internet Applications
PHP and Rich Internet Applications
elliando dias
 
SharePoint & jQuery Guide - SPSNashville 2014
SharePoint & jQuery Guide - SPSNashville 2014SharePoint & jQuery Guide - SPSNashville 2014
SharePoint & jQuery Guide - SPSNashville 2014
Mark Rackley
 
An Introduction to Tornado
An Introduction to TornadoAn Introduction to Tornado
An Introduction to Tornado
Gavin Roy
 
#NewMeetup Performance
#NewMeetup Performance#NewMeetup Performance
#NewMeetup Performance
Justin Cataldo
 

Semelhante a ApacheCon 2005 (20)

YQL & Yahoo! Apis
YQL & Yahoo! ApisYQL & Yahoo! Apis
YQL & Yahoo! Apis
 
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)
 
Rails 4.0
Rails 4.0Rails 4.0
Rails 4.0
 
AppForum 2014 Boost Hybrid App Performance
AppForum 2014 Boost Hybrid App PerformanceAppForum 2014 Boost Hybrid App Performance
AppForum 2014 Boost Hybrid App Performance
 
Dirty Secrets of the PHP SOAP Extension
Dirty Secrets of the PHP SOAP ExtensionDirty Secrets of the PHP SOAP Extension
Dirty Secrets of the PHP SOAP Extension
 
SPTechCon DevDays - SharePoint & jQuery
SPTechCon DevDays - SharePoint & jQuerySPTechCon DevDays - SharePoint & jQuery
SPTechCon DevDays - SharePoint & jQuery
 
SPTechCon Boston 2015 - Utilizing jQuery in SharePoint
SPTechCon Boston 2015 - Utilizing jQuery in SharePointSPTechCon Boston 2015 - Utilizing jQuery in SharePoint
SPTechCon Boston 2015 - Utilizing jQuery in SharePoint
 
Harness SharePoint and jQuery to Make Dynamic Displays and Applications
 Harness SharePoint and jQuery to Make Dynamic Displays and Applications Harness SharePoint and jQuery to Make Dynamic Displays and Applications
Harness SharePoint and jQuery to Make Dynamic Displays and Applications
 
Painless Persistence in a Disconnected World
Painless Persistence in a Disconnected WorldPainless Persistence in a Disconnected World
Painless Persistence in a Disconnected World
 
Integration Test Cucumber + Webrat + Selenium
Integration Test Cucumber + Webrat + SeleniumIntegration Test Cucumber + Webrat + Selenium
Integration Test Cucumber + Webrat + Selenium
 
Socket applications
Socket applicationsSocket applications
Socket applications
 
PHP and Rich Internet Applications
PHP and Rich Internet ApplicationsPHP and Rich Internet Applications
PHP and Rich Internet Applications
 
Express Presentation
Express PresentationExpress Presentation
Express Presentation
 
Using Apache Solr
Using Apache SolrUsing Apache Solr
Using Apache Solr
 
SharePoint & jQuery Guide - SPSNashville 2014
SharePoint & jQuery Guide - SPSNashville 2014SharePoint & jQuery Guide - SPSNashville 2014
SharePoint & jQuery Guide - SPSNashville 2014
 
SharePoint and jQuery Essentials
SharePoint and jQuery EssentialsSharePoint and jQuery Essentials
SharePoint and jQuery Essentials
 
A Quick Introduction to YQL
A Quick Introduction to YQLA Quick Introduction to YQL
A Quick Introduction to YQL
 
An Introduction to Tornado
An Introduction to TornadoAn Introduction to Tornado
An Introduction to Tornado
 
Power Automate Techniques that "Saved Our Bacon"
Power Automate Techniques that "Saved Our Bacon"Power Automate Techniques that "Saved Our Bacon"
Power Automate Techniques that "Saved Our Bacon"
 
#NewMeetup Performance
#NewMeetup Performance#NewMeetup Performance
#NewMeetup Performance
 

ApacheCon 2005

  • 1. Consuming Web Services Using PHP 5 Adam Trachtenberg Senior Manager of Platform Evangelism, eBay
  • 4. REST URL + HTTP Verb -> XML Reply
  • 5. Thai Food near 94306 http://api.example.com/yellowpages?zip=94306&query=thai+restaurant <ResultSet> <Result> <Title>Thai City</Title> <Address>3691 El Camino Real</Address> <City>Palo Alto</City> <State>CA</State> <Phone>(650) 493-0643</Phone> <BusinessUrl>http://www.thaicity.com/</BusinessUrl> </Result> <Result> <Title>Thai Garden Restaurant</Title> <Address>4329 El Camino Real</Address> … </Result> </ResultSet>
  • 6. REST (In Theory) SQL REST CREATE POST SELECT GET UPDATE PUT DELETE DELETE
  • 7. REST (In Practice) SQL REST CREATE GET SELECT GET UPDATE GET DELETE GET
  • 8. del.icio.us • Social bookmarking site • Save pages for yourself • Share pages with others • Provide meta-data via folksonomy • Easy to insert and query the site
  • 9. del.icio.us/rss • RSS feeds for everything • Prepend rss before path • http://del.icio.us/popular/ebay • http://del.icio.us/rss/popular/ebay
  • 10. <rdf:RDF> <channel rdf:about="http://del.icio.us/popular/ebay">...</channel> <item rdf:about="http://the-winning-bid.com/"> <title>The-Winning-Bid.com</title> <link>http://the-winning-bid.com/</link> <dc:creator>minenet</dc:creator> <dc:date>2005-10-10T03:40:09Z</dc:date> </item> <item rdf:about="http://ebaysupplies.usps.com/"> <title>USPS | eBay</title> <link>http://ebaysupplies.usps.com/</link> <dc:creator>kresston</dc:creator> <dc:date>2004-11-17T14:51:34Z</dc:date> </item> ... </rdf:RDF>
  • 11. RSS + SimpleXML $url = 'http://del.icio.us/rss/popular/ebay'; $xml = simplexml_load_file($url); foreach ($xml->item as $item) { printf("%20.20s : %-30.30sn", (string) $item->title, (string) $item->link); }
  • 12. Popular eBay Pages The-Winning-Bid.com : http://64.34.178.175/results.p USPS | eBay : http://ebaysupplies.usps.com/ GarageSale : http://www.iwascoding.com/Gara :: gumshoo - eBay se : http://www.gumshoo.com/ mapBid - View eBay a : http://www.mapbid.com/ Auction Mapper | Vis : http://www.auctionmapper.com/ ebay api developer d : http://developer.ebay.com/ what is it? : http://www.whatis-it.com/ eBay Developer Chall : http://ebay.promotionexpert.co ebay + google maps : http://www.markovic.com/markov eBay drops charges f : http://www.cbronline.com/artic Mark Pincus Blog: Sh : http://markpincus.typepad.com/
  • 13. RSS + SimpleXML foreach (simplexml_load_file( 'http://del.icio.us/rss/popular/ebay')->item as $item) { printf("%20.20s : %-30.30sn", (string) $item->title, (string) $item->link); }
  • 14. REST + GET • URL + query string • Test in Firefox • Still process XML
  • 15. flickr • Social digital photo site • Save photos for yourself • Share photos with others • Provide meta-data via folksonomy • Easy to insert and query the site
  • 16. flickr/services/rest • Multiple REST API calls • API decoupled from site URL structure • flickr.com/services/rest/?method=...
  • 17. flickr + GET Requires developer authentication token Search flickr http://www.flickr.com/services/api/ flickr.photos.search.html http://www.flickr.com/services/rest/? method=flickr.photos.search& tags=ebay& api_key=giantlonguglystring
  • 18. <?xml version="1.0" encoding="utf-8" ?> <rsp stat="ok"> <photos page="1" pages="75" perpage="100" total="7422"> <photo id="71550241" owner="27503448@N00" secret="51fb62fb95" server="35" title="champ1" ispublic="1" isfriend="0" isfamily="0"/> ... </photos> </rsp>
  • 19. GET a Request $base = 'http://www.flickr.com/services/rest/?'; $qs = array( 'method' => 'flickr.photos.search', 'api_key' => 'biglonguglystring', 'tags' => 'ebay', ); $url = $base . http_build_query($qs); $out = file_get_contents($url);
  • 20. Create a Gallery $xml = simplexml_load_string($out); foreach ($xml->photos->photo as $photo) { $server = (string) $photo['server']; $id = (string) $photo['id']; $secret = (string) $photo['secret']; $img .= "<img src="http://static.flickr.com/ {$server}/{$id}_{$secret}_s.jpg"/>"; } print $img;
  • 22. REST + POST • URL + POST Data • Can’t test in Firefox • POST body can be anything • Return data is XML
  • 23. flickr + POST • Requires developer authentication token • Requires user authentication token • Add tags to a photo • http://www.flickr.com/services/api/ flickr.photos.addTags.html
  • 24. Define POST Data $url = 'http://www.flickr.com/services/rest/?'; $qs = array( 'method' => 'flickr.photos.addTags', 'api_key' => 'biglonguglystring', 'auth_token' => 'anotherbiglonguglystring', 'tags' => 'hongkong', 'photo_id' => '50021321' );
  • 25. Compute the Signature $api_sig = 'would_you_believe_another_long_string?'; ksort($qs); foreach ($qs as $k => $v) { $api_sig .= $k . $v; } $qs['api_sig'] = md5($api_sig);
  • 26. POST a Request $content = http_build_query($qs); $options = array( 'http' => array( 'method' => 'POST', 'content' => $content ) ); $context = stream_context_create($options); $out = file_get_contents($url, false, $context);
  • 27. <?xml version="1.0" encoding="utf-8" ?> <rsp stat="ok"> </rsp>
  • 28. SOAP • Leaky abstraction around XML • Mapped conversion of native data types to XML Schema types and vice versa • Independent of HTTP • API described using WSDL
  • 29. Use ext/soap • Bundled with PHP 5 • Enabled by default in PHP 5.1 • Written in C not PHP • Most compatible with other SOAP servers • Actively maintained
  • 30. eBay • Social marketplace site • Buy items for yourself • Sell items to others • Provide meta-data via attributes • Easy to insert and query the site
  • 31. api.ebay.com • Totally decoupled from eBay • SOAP interface • Requires developer and user authentication • Testing and production environments
  • 32. Search eBay // Create and configure session $session = new eBaySession('long_string', 'another_long_string', 'ya_long_string'); $session->token = 'one_more_long_string'; $session->site = 100; // 100 = Motors; $session->location = https://api.ebay.com/wsapi;
  • 33. Baby,You Can Find My Car try { $client = new eBaySOAP($session); $params = array( 'Version' => 435, 'Query' => '*', 'CategoryID' => 6001); // Cars $results = $client->GetSearchResults($params); } catch (SOAPFault $f) { }
  • 34. <?xml version="1.0" encoding="UTF-8"?> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:ebay:apis:eBLBaseComponents"> <SOAP-ENV:Header> <ns1:RequesterCredentials> <ns1:eBayAuthToken>one_more_long_string</ns1:eBayAuthToken> <ns1:Credentials> <ns1:AppId>one_long_string</ns1:AppId> <ns1:DevId>another_long_string</ns1:DevId> <ns1:AuthCert>ya_long_string</ns1:AuthCert> </ns1:Credentials> </ns1:RequesterCredentials> </SOAP-ENV:Header> <SOAP-ENV:Body> <ns1:GetSearchResultsRequest> <ns1:Version>425</ns1:Version> <ns1:Query>*</ns1:Query> <ns1:CategoryID>6001</ns1:CategoryID> <ns1:TotalOnly>true</ns1:TotalOnly> </ns1:GetSearchResultsRequest> </SOAP-ENV:Body> </SOAP-ENV:Envelope>
  • 35. <?xml version="1.0" encoding="UTF-8"?> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <soapenv:Body> <GetSearchResultsResponse xmlns="urn:ebay:apis:eBLBaseComponents"> <Timestamp>2005-12-09T02:35:57.881Z</Timestamp> <Ack>Success</Ack> <Version>437</Version> <Build>e437_core_Bundled_2112013_R1</Build> <ItemsPerPage>100</ItemsPerPage> <PageNumber>1</PageNumber> <HasMoreItems>true</HasMoreItems> <PaginationResult> <TotalNumberOfPages>294</TotalNumberOfPages> <TotalNumberOfEntries>29335</TotalNumberOfEntries> </PaginationResult> <CategoryArray/> </GetSearchResultsResponse> </soapenv:Body> </soapenv:Envelope>
  • 36. $total = number_format( $results->PaginationResult ->TotalNumberOfEntries); print "There are {$total} passenger vehicles for sale on eBay Motors";
  • 38. What I’ve learned • Web services are closed source software • Documentation and online support is vital • Debugging is hard • SOAP sucks! SOAP rocks! • SOAP interoperability is an issue • Authentication is ad-hoc
  • 39. Tips and Tricks • You’re not querying a local MySQL database • Cache your data • Use debugging methods • Sniff the wire • Send requests to your own server
  • 40. It Only Looks Simple • Web services == (HTTP && XML) != PHP • You must grok • HTTP • XML • XML Namespaces (Danger!) • XML Schema • XPath (I <heart> XPath) • SOAP • The data is layered
  • 41. What Shape Is Your Data? • REST • Angled = <> = XML Database • FLOWR: /Items/Item[Price < 100]/ItemID • SOAP • Square = [] = Relational database • SQL: SELECT ItemID FROM Items WHERE Items.Price < 100
  • 42. References • del.icio.us: http://del.icio.us/help/ • RSS, HTML, Javascript, JSON, IE Active Channel, API (REST + GET) • flickr: http://www.flickr.com/services/api/ • RSS, Atom, REST, XML-RPC, SOAP • eBay: http://developer.ebay.com • RSS, REST, XML API, SOAP, .NET SDK, Java SDK