การใช้ WSO2 WSF/PHP เพื่อเรียกใช้เว็บเซอร์วิสแบบ SOAP


WSO2 WSF PHP SOAP Web Service เว็บเซอร์วิส

ารใช้ WSO2 WSF/PHP เพื่อเรียกใช้เว็บเซอร์วิสนั้น WSO2 WSF/PHP รองรับการเรียกใช้เว็บเซอร์วิสทั้งแบบ SOAP และแบบ REST

ในที่นี้ขออธิบายการใช้ WSO2 WSF/PHP เพื่อเรียกใช้เว็บเซอร์วิสทั้งแบบ SOAP และแบบ REST ผ่านตัวอย่างของ WSO2 samples โดยมีการดัดแปลงเพื่อเรียกใช้เว็บเซอร์วิสจากมหาวิทยาลัยขอนแก่นที่มีการติด ตั้ง proxy ที่มี host address อยู่ที่ 202.12.97.116 และ port อยู่ที่ 8088

ในบันทึกนี้ จะพูดถึงการเรียกใช้เว็บเซอร์วิสแบบ SOAP โดยใช้ WSO2 WSF/PHP

1. ระบุรูปแบบของข้อมูล XML ในข้อความที่เราส่งร้องขอไป (Request Message) โดยระบุผ่านรูปแบบ

$requestPayloadString = <<<XML

ข้อความ XML ที่ต้องการส่งไป

XML;

2. สร้างออปเจกต์ WSClient เพื่อเรียกใช้เว็บเซอร์วิส

$client = new WSClient(array("to" => "URL ของเว็บเซอร์วิสที่ต้องการส่งข้อความ XML request ",
"useSOAP"=>"SOAP version ที่ใช้",
"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.
*/

/**
* Note: This is a simole example written just as a proof of concept.
* You may write a nice web interface to improve the usability.
* This sample sends a Goole key and other relevant data to Google
* search service.
* If everyting goes right, it returns a set of search results.
* You can control the number of results through maxResults parameter
* in the request payload.
* Google search uses SOAP 1.1.
*/


$requestPayloadString = <<<XML
<ns1:doGoogleSearch x:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="urn:GoogleSearch" xmlns:x="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance" xmlns:xsd="http://www.w3.org/1999/XMLSchema">
<key xsi:type="xsd:string">9BxDDkNQFHKccc4HKqSxv71RRS7Hkleo</key>
<q xsi:type="xsd:string">Khon Kaen University</q>
<start xsi:type="xsd:int">0</start>
<maxResults xsi:type="xsd:int">10</maxResults>
<filter xsi:type="xsd:boolean">true</filter>
<restrict xsi:type="xsd:string"></restrict>
<safeSearch xsi:type="xsd:boolean">false</safeSearch>
<lr xsi:type="xsd:string"></lr>
<ie xsi:type="xsd:string">latin1</ie>
<oe xsi:type="xsd:string">latin1</oe>
</ns1:doGoogleSearch>
XML;

try {
$client = new WSClient(array("to" => "http://api.google.com/search/beta2",
"useSOAP"=>"1.1",
"proxyHost" => "202.12.97.116",
"proxyPort" => "8088"));

$responsePayload = $client->request($requestPayloadString);
printf("Response = %s <br/> ", htmlspecialchars($responsePayload->str));
} catch (Exception $e) {
if ($e instanceof WSFault) {
printf("Soap Fault: %s ", $e->Reason);
} else {
printf("Message = %s ",$e->getMessage());
}
}
?>

 

 

callSOAPSearch

 

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

http://wso2.org/project/wsf/php/1.2.0/docs/manual.html

หมายเลขบันทึก: 172725เขียนเมื่อ 24 มีนาคม 2008 16:33 น. ()แก้ไขเมื่อ 18 มิถุนายน 2012 12:01 น. ()สัญญาอนุญาต: จำนวนที่อ่านจำนวนที่อ่าน:


ความเห็น (2)

ครับเป็นข้อมูลที่มีค่ามากครับ และ ขอถามต่ออีกนิดครับการใช้ WSO2 WSF/PHP สามารถส่งข้อมูลภาษไทยได้หรือเปล่าครับ ขอบคุณครับอาจาร์ย

อ. เคยลองเล่น gadgets server ของ WSO2 ไหมครับ แนวทาง นี้น่าสนใจไหม ครับ

เพราะเห็นมัน Compat กับหลายเจ้า

พบปัญหาการใช้งานกรุณาแจ้ง LINE ID @gotoknow
ClassStart
ระบบจัดการการเรียนการสอนผ่านอินเทอร์เน็ต
ทั้งเว็บทั้งแอปใช้งานฟรี
ClassStart Books
โครงการหนังสือจากคลาสสตาร์ท