REST Web service

REST Web service แบบ REST กำลังเป็นที่นิยมมากขึ้น โดยจะเห็นได้จาก Yahoo ได้พัฒนาเว็บเซอร์วิสแบบ REST ทั้งหมด  ในขณะที่ Amazon และ eBay สนับสนุนการเรียกใช้เว็บเซอร์วิสทั้งแบบ SOAP และ REST เมื่อก่อน Google จะสนับสนุนแต่ SOAP Search API แต่ตั้งแต่วันที่ 5 ธันวาคม 2549 Google ก็ประกาศหยุดพัฒนา SOAP Search API ต่อและหยุดให้รหัสกับนักพัฒนา SOAP Search API ตอนนี้ Google แนะให้ผู้ใช้เรียก AJAX Search API  ซึ่ง REST Web service กับ AJAX ทำงานเข้ากันได้ดี  เพราะ REST Web service ให้ผลลัพธ์ข้อมูลเป็น XML

<?php
// callAmazonRESTWS.php
header("Content-type: text/xml");
$base = 'http://webservices.amazon.com/onca/xml';
$query_string = '';
$params = array(
'Service' =>'AWSECommerceService',
'SubscriptionId' => '16XT8ETKKB7NWHAGCQ02' ,
'Operation' => 'ItemSearch',
'SearchIndex' => 'Books',
'Keywords' => 'Web Services');
foreach ($params as $key => $value) {
   if ($key != 'Keywords')
    $query_string .= "$key=" . urlencode($value) . "&";
  else
   $query_string .="$key=".urlencode($value);
}
$url = "$base?$query_string";

// Define a context for HTTP.
$aContext = array(
   'http' => array(
       'proxy' => 'tcp://202.12.97.116:8080', // This needs to be the server and the port of the Khon Kaen University Authentication Proxy Server.
       'request_fulluri' => True,
       ),
   );
$cxContext = stream_context_create($aContext);
// Now all file stream functions can use this context.
$xml = file_get_contents($url, False, $cxContext);
echo $xml;    
?>

ถ้าเราเรียกไฟล์ php ดังกล่าว เราจะได้ผลลัพธ์ดังนี้

แหล่งข้อมูลอ้างอิง

* Roger L. Costello, “Building Web Services the REST Way”, http://www.xfront.com/REST-Web-Services.html
* Stefan Marr, “RESTful Web Services”, HPI, Seminar Advanced Database Technology
* Pete Freitag, “REST vs SOAP Web Services”, http://www.petefreitag.com/item/431.cfm
* Google, “Google SOAP Search API”, http://code.google.com/apis/soapsearch/
* Sameer Tyagi, “RESTful Web Services”, http://java.sun.com/developer/technicalArticles/WebServices/restful/?feed=JSC