การใช้ WSO2 WSF/PHP เพื่อเรียกใช้เว็บเซอร์วิสนั้น WSO2 WSF/PHP รองรับการเรียกใช้เว็บเซอร์วิสทั้งแบบ SOAP และแบบ REST
ในที่นี้ขออธิบายการใช้ WSO2 WSF/PHP เพื่อเรียกใช้เว็บเซอร์วิสทั้งแบบ
SOAP และแบบ REST ผ่านตัวอย่างของ WSO2 samples
โดยมีการดัดแปลงเพื่อเรียกใช้เว็บเซอร์วิสจากมหาวิทยาลัยขอนแก่นที่มีการติด
ตั้ง proxy ที่มี host address อยู่ที่ 202.12.97.116 และ port อยู่ที่
8088
ในบันทึกนี้ จะพูดถึงการเรียกใช้เว็บเซอร์วิสแบบ REST โดยใช้ WSO2 WSF/PHP
1. ระบุรูปแบบของข้อมูล XML ในข้อความที่เราส่งร้องขอไป (Request Message) โดยระบุผ่านรูปแบบ
$requestPayloadString = <<<XML
ข้อความ XML ที่ต้องการส่งไป
XML;
2. สร้างออปเจกต์ WSClient เพื่อเรียกใช้เว็บเซอร์วิส
$client = new WSClient(array("to" => "URL ของเว็บเซอร์วิสที่ต้องการส่งข้อความ XML request ",
"HTTPMethod"=> "GET หรือ POST หรือ DELETE หรือ PUT",
"useSOAP"=> FALSE,
"proxyHost" => "Proxy Host Address",
"proxyPort" => "Proxy Port"));
3. ระบุข้อความที่ต้องการส่งไปของออปเจกต์ WSClient ซึ่งจะได้ข้อความ XML ส่งกลับมาจากการร้องขอข้อมูลจากเว็บเซอร์วิส
$responsePayload = $client->request($requestPayloadString);
4. แสดงข้อความ XML ที่ได้รับกลับมา
printf("Response = %s <br/> ", htmlspecialchars($responsePayload->str));
ตัวอย่างโค้ดที่ใช้ในการเรียกใช้เว็บเซอร์วิสของ Google ในการเรียกใช้
Google SOAP Search โดยคำที่เราต้องการค้นหาคือ "Khon Kaen University"
<?php
/*
* Copyright 2005,2006 WSO2, Inc. http://wso2.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
$requestPayloadString = <<<XML
<webSearch><appid>ApacheRestDemo</appid><query>Web Services</query><form/></webSearch>
XML;
try {
$client = new WSClient(
array("to"=>"http://search.yahooapis.com/WebSearchService/V1/webSearch",
"HTTPMethod"=>GET,
"useSOAP"=>FALSE,
"proxyHost" => "202.12.97.116",
"proxyPort" => "8088"));
$responseMessage = $client->request($requestPayloadString);
printf("Response = %s <br>", htmlspecialchars($responseMessage->str));
} catch (Exception $e) {
if ($e instanceof WSFault) {
printf("Soap Fault: %s
", $e->Reason);
} else {
printf("Message = %s
",$e->getMessage());
}
}
?>
แหล่งข้อมูลอ้างอิง