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
ขอเก็บความรู้ด้วยคนครับ
แต่งานผมยังไม่ได้ใช้พวกนี้เท่าไหร
ผมทดสอบ code ของอาจารย์ แล้ว open source ที่ http://www.thaiall.com/php/indexo.html แล้วทดสอบที่ http://www.thaiall.com/rss/callAmazonRESTWS.php ถ้ามีอะไรที่ผมต้องแก้ไขก็ยินดีครับ เพราะผมศึกษาเรื่องนี้ไว้น้อยมาก
สวัสดีค่ะ แวะมาทักทายค่ะ
ผมไม่เคยใช้ SOAP สำเร็จเลยครับ T_T
พอมาเล่น REST ของ Yahoo Search API แล้วค่อยมีกำลังใจหน่อย
เดี๋ยวนี้โปรแกรมที่ทำเอง แต่ก่อนใช้ XML-RPC เดี๋ยวพยายามจะเปลี่ยนไปเป็น REST แต่ก็ไม่รู้ว่าใช้ JSON จะยังเรียก REST ได้หรือเปล่า :-P