Consuming Web Services Using PHP 5: Adam Trachtenberg Senior Manager of Platform Evangelism, Ebay
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
REST
POST GET PUT DELETE
REST
GET GET GET 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_le($url); foreach ($xml->item as $item) { printf("%20.20s : %-30.30s\n", (string) $item->title, (string) $item->link); }
RSS + SimpleXML
foreach (simplexml_load_le( 'http://del.icio.us/rss/popular/ebay')->item as $item) { printf("%20.20s : %-30.30s\n", (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 ickr.com/services/rest/?method=...
flickr + GET
Requires developer authentication token Search ickr http://www.ickr.com/services/api/ ickr.photos.search.html http://www.ickr.com/services/rest/? method=ickr.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.ickr.com/services/rest/?'; $qs = array( 'method' => 'ickr.photos.search', 'api_key' => 'biglonguglystring', 'tags' => 'ebay', ); $url = $base . http_build_query($qs); $out = le_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.ickr.com/ {$server}/{$id}_{$secret}_s.jpg\"/>"; } print $img;
flickr/ebay
REST + POST
URL + POST Data Cant test in Firefox POST body can be anything Return data is XML
ickr + POST
Requires developer authentication token Requires user authentication token Add tags to a photo http://www.ickr.com/services/api/
ickr.photos.addTags.html
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);
SOAP
Leaky abstraction around XML Mapped conversion of native data types to
XML Schema types and vice versa
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 congure 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;
<?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";
References
del.icio.us: http://del.icio.us/help/ RSS, HTML, Javascript, JSON, IE Active
Channel, API (REST + GET)
ickr: http://www.ickr.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