180 likes | 351 Views
Lab #2: Web API Programming Exercises. By J. H. Wang Dec. 19, 2011. Outline. Yahoo! Term Extraction API Bing Search API Others. Platform and compiler. (The same as networking programming in any language on any platform.). Web API programming exercises. Network programming
E N D
Lab #2: Web API Programming Exercises By J. H. Wang Dec. 19, 2011
Outline • Yahoo! Term Extraction API • Bing Search API • Others
Platform and compiler • (The same as networking programming in any language on any platform.)
Web API programming exercises • Network programming • Sending a request packet • Parsing the response from Web server • To start programming with Web API • Document: e.g. http://tw.developer.yahoo.com/cas/ • Register an API key • Start using the API • Protocol and message format • Prepare parameters in request • Parsing responses
Network Programming • Sending an HTTP packet • Parsing the HTTP responses
Yahoo Term Extraction API • Document: http://tw.developer.yahoo.com/cas/ • Register an API key • Start using the API • Request method: HTTP POST • Response format: JSON, XML
Yahoo!搜尋『斷章取義』API • http://asia.search.yahooapis.com/cas/v1/[APIMethod] • API Method: • ws: (word segmentation) • ke: (keyword extraction) • appid=<AppID> • content=<text content> (encoded in UTF-8) • format=json (or xml)
HTTP POST • E.g.: http://asia.search.yahooapis.com/cas/v1/ke?appid=<ID>&format=xml&content=<content> • POST /cas/v1/ke HTTP/1.1\r\nHost: asia.search.yahooapis.com\r\nContent-Length: <len>\r\nContent-Type: application/x-www-form-urlencoded\r\n\r\nappid=<ID>&format=xml&content=<content>
Yahoo Term Extraction Web Service • http://developer.yahoo.com/search/content/V1/termExtraction.html • Sample request: • http://search.yahooapis.com/ContentAnalysisService/V1/termExtraction • appid: YahooDemo • context: Italian sculptors and painters of the renaissance favored the Virgin Mary for inspiration. • query: madonna
Sample response • <ResultSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:yahoo:cate" xsi:schemaLocation="urn:yahoo:cate http://search.yahooapis.com/ContentAnalysisService/V1/TermExtractionResponse.xsd"><Result>italian sculptors</Result><Result>virgin mary</Result><Result>painters</Result><Result>renaissance</Result><Result>inspiration</Result></ResultSet>
Parsing JSON Output • Document: http://www.json.org/ • Implementations • JSON parser in C, C++, Java, Perl, PHP, Python, … • Ex. • JSON_checker, and JSON_parser in C
Bing Search API • Document: http://msdn.microsoft.com/en-us/library/dd251056.aspx • Search: Web, images, news, video, … • Register an API key • Start using the API • Request method: HTTP GET • Response format: JSON, XML, SOAP
Bing Web Search • Example in Web search • Sample request: • http://api.bing.net/json.aspx • AppId= <AppID> • Sources=Web • Query=msdn blogs
HTTP GET • E.g.: http://api.bing.net/json.aspx?AppId= <AppID>&Sources=Web&Query=<query> • GET /json.aspx?AppId= <AppID>&Sources=Web&Query=<query> HTTP/1.1\r\nConnection: close\r\nHost: api.bing.net\r\n\r\n
Request Example • http://api.bing.net/json.aspx?AppId=Insert your AppId here&Query=msdn blogs&Sources=Web&Version=2.0&Market=en-us&Adult=Moderate&Options=EnableHighlighting&Web.Count=10&Web.Offset=0&Web.Options=DisableHostCollapsing+DisableQueryAlterations&JsonType=callback&JsonCallback=SearchCompleted
Response Example • { "SearchResponse":{ "Version":"2.0", "Query":{ "SearchTerms":"msdn blogs" }, "Web":{ "Total":1490, "Offset":0, "Results":[ { "Title":"Monads", "Description":"Monad 1 2 Manifesto . Jeffrey P. Snover. Aug 8, 2002. Version 1.2. What is Monad? Monad is the next generation platform for administrative automation.", "Url":"http:\/\/blogs.msdn.com\/powershell\/attachment\/1910567.ashx", "DisplayUrl":"http:\/\/blogs.msdn.com\/powershell\/attachment\/1910567.ashx", "DateTime":"2008-10-29T05:27:14Z" }, …, { "Title":"Software Design Invitation Proposal Template for Imagine Cup", "Description":"SINGPAPORE SOFTWARE DESIGN INVITATIONAL SUBMISSION PROPOSAL TEMPLATE. NOTE: This section contains a template, not a fixed format. Please return soft copy only – hardcopies will not be ...", "Url":"http:\/\/blogs.msdn.com\/stanley\/attachment\/1610112.ashx", "DisplayUrl":"http:\/\/blogs.msdn.com\/stanley\/attachment\/1610112.ashx", "DateTime":"2008-10-23T18:13:07Z" } ] } }}