2006/Feb/24

วันนี้มาเขียนต่อเรื่องการใช้ php เชื่อมต่อระบบ web serviceเพื่อตรวจสอบหมายเลขบัตรประชาชนกันต่อ ความจริงยังมีบริการอื่นอีกเช่นการตรวจหมายเลขผู้เสียภาษีสามารถเข้าไปดูรายละเอียดบริการอื่นๆได้ที่ http://www.rd.go.th/webservice/
เป้าหมายหลักในวันนี้ก็คือการขอใช้บริการตรวจสอบหมายเลขบัตรประจำตัวประชาชน
ทีนี้ถ้าจะอธิบายการทำงานหรือพื้นฐานของ web service คงจะว่ากันยาวเลย แนะนะให้ไปอ่านบทความได้ที่นี้นะครับ
http://www.wsiam.com/document/abcwebservices/webservicesabc.jsp

ปกติแล้วเวลาเราทำการสร้างหรืเรียกใช้ web service นั้นเราต้องมีสิ่งต่างๆต่อไปนี้
1.
UDDI(Universal Description, Discovery and Integration protocol)
2.WSDL(Web Services Description Language)
3 SOAP(Simple Object Access Protocol))
4.XML.(The Extensible Markup Language)
เป็นอย่างน้อยครับ ใน php นั้นสนับสนุน การใช้งาน soap อยู่ 2 แบบ
- SOAP object ใน php version 5
-NUSOAP Library สามารถ DownLoad ได้ที่
http://cvs.sourceforge.net/viewcvs.py/nusoap/lib/nusoap.php
ในที่นี้เราจะใช้ NUSOAP ในการติดต่อ
เมื่อติดตั้ง php อยู่ในเครื่องแล้ว เราควรติดตั้ง extention เพิ่มก็คือ
libcurl.dll
copy ได้จาก folder php ที่ต้องการติดตั้ง เอาไปไว้ใน sytem32 แล้วเข้าไปเปิด extention ใน php.ini (เอาเครื่องหมาย ; ออก) หน้า extension=php_curl.dll

มาดู code กันเลย

<?

//เรียกใช้ nusoap
include("lib/nusoap.php");
//กำหนด wsdl
$wsdl="
https://rdws.rd.go.th/ServiceRD/
CheckTINPINService.asmx?WSDL
";
//สร้าง soap action เพื่อส่งค่าไปยัง service ตัวใด ของกรมสรรมพากร
$soapaction="
https://rdws.rd.go.th/ServiceRD/
CheckTINPINService/ServicePIN
";
//สร้าง soap client

$client=new soapclient($wsdl);
// สร้าง message request ส่งไปยัง service กรมสรรพกร
$document='<ServicePIN xmlns="
anonymousanonymous
5809990008454'">
https://rdws.rd.go.th/ServiceRD/CheckTINPINService">
<username>anonymous</username><password>anonymous
</password>
<PIN>5809990008454</PIN></ServicePIN>'
;
$mysoapmsg=$client->serializeEnvelope($document,''
,array(),'document','literal');
//ส่งmessage request
$response=$client->send($mysoapmsg,$soapaction);
//ตรวจสอบผลการส่ง
if ($client->fault) {
echo '<h3>Fault</h3><pre>';
print_r($response);
echo '</pre>';
}

echo "<p>SOAP query</p>";
echo '<pre>' . htmlspecialchars($client->request,
ENT_QUOTES) . '</pre>';
echo "<p>SOAP response</p>";
echo '<pre>' . htmlspecialchars($client->response,
ENT_QUOTES) . '</pre>';
echo '<h2>result</h2><pre>';
//แสดง message response จาก กรมสรรพากร ในรูปแบบ array
print_r($response);

//เข้าถึงค่าที่ต้องการนำมาใช้งาน
if($response['ServicePINResult']['diffgram']
['NewDataSet']['Message']['Code']=="W00000"){
echo "<br>";
echo "<b>".$response['ServicePINResult']['diffgram']
['NewDataSet']['CheckingResult']['ID']."</b>";
echo "<br>";
echo "<b>".$response['ServicePINResult']['diffgram']
['NewDataSet']['CheckingResult']['DigitOk']."</b>";
echo "<br>";

}
echo '</pre>';

?>

ครับลองทดลองดูนะครับไม่ยาก แล้วนำมาประยุกต์ใช้งานต่อcode ตัวนี้แค่ทดสอบ
เท่านั้นแหละครับผิดพลาดประการใด ขออภัย

ตัวอย่างผลการ รัน
SOAP query

POST /ServiceRD/CheckTINPINService.asmx?WSDL HTTP/1.0
Host: rdws.rd.go.th
User-Agent: NuSOAP/0.7.2 (1.94)
Content-Type: text/xml; charset=ISO-8859-1
SOAPAction: "
https://rdws.rd.go.th/ServiceRD/CheckTINPINService/ServicePIN"
Content-Length: 565

<?xml version="1.0" encoding="ISO-8859-1"?><SOAP-ENV:Envelope
SOAP-ENV:encodingStyle="
http://schemas.xmlsoap.org/soap/encoding/"
xmlns:SOAP-ENV="
http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="
http://www.w3.org/2001/XMLSchema"
xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"
xmlns:SOAP-ENC="
anonymousanonymous5809990008454
http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body><ServicePIN">
<username>anonymous</username><password>anonymous
</password><PIN>
5809990008454</PIN></ServicePIN></SOAP-ENV:Body>
</SOAP-ENV:Envelope
https://rdws.rd.go.th/ServiceRD
/CheckTINPINService
xmlns="
anonymousanonymous5809990008454>
SOAP response

HTTP/1.1 200 OK
Date: Fri, 24 Feb 2006 11:19:34 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 1.1.4322
Cache-Control: private, max-age=0
Content-Type: text/xml; charset=utf-8
Content-Length: 1725

<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body>
<ServicePINResponse
xmlns="https://rdws.rd.go.th/ServiceRD/
CheckTINPINService">
<ServicePINResult><xs:schema
id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata=
"urn:schemas-microsoft-com:xml-msdata">
<xs:element name="NewDataSet" msdata:IsDataSet="true">
<xs:complexType><xs:choice maxOccurs="unbounded">
<xs:element name="CheckingResult"><xs:complexType>
<xs:sequence><xs:element name="ID"
type="xs:string" minOccurs="0" />
<xs:element name="DigitOk" type="xs:boolean" minOccurs="0" />
<xs:element name="IsExist" type="xs:string" minOccurs="0" /></xs:sequence></xs:complexType></xs:element>
<xs:element name="Message"><xs:complexType><xs:sequence>
<xs:element name="Code" type="xs:string" minOccurs="0" />
<xs:element name="Description" type="xs:string" minOccurs="0" />
</xs:sequence></xs:complexType>
</xs:element></xs:choice>
</xs:complexType></xs:element></xs:schema>
<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
<NewDataSet xmlns=""><CheckingResult diffgr:id=
"CheckingResult1" msdata:rowOrder="0" diffgr:hasChanges="inserted">
<ID>5809990008454</ID>
<DigitOk>true</DigitOk>
</CheckingResult><Message diffgr:id="Message1" msdata:rowOrder="0" diffgr:hasChanges="inserted"><Code>W00000</Code><Description>
เธเธฒเธฃเธเธณเธเธฒเธเนเธชเธฃเนเธเธชเธกเธเธนเธฃเธเน
&lt;br&gt; Success "</Description></Message></NewDataSet>
</diffgr:diffgram>
</ServicePINResult>
</ServicePINResponse></soap:Body></soap:Envelope>
result
Array
(
[ServicePINResult] => Array
(
[schema] => Array
(
[element] => Array
(
[complexType] => Array
(
[choice] => Array
(
[element] => Array
(
[0] => Array
(
[complexType] => Array
(
[sequence] => Array
(
[element] => Array
(
[0] => Array
(
[!name] => ID
[!minOccurs] => 0
)

[1] => Array
(
[!name] => DigitOk
[!minOccurs] => 0
)

[2] => Array
(
[!name] => IsExist
[!minOccurs] => 0
)

)

)

)

[!name] => CheckingResult
)

[1] => Array
(
[complexType] => Array
(
[sequence] => Array
(
[element] => Array
(
[0] => Array
(
[!name] => Code
[!minOccurs] => 0
)

[1] => Array
(
[!name] => Description
[!minOccurs] => 0
)

)

)

)

[!name] => Message
)

)

[!maxOccurs] => unbounded
)

)

[!name] => NewDataSet
[!msdata:IsDataSet] => true
)

[!id] => NewDataSet
)

[diffgram] => Array
(
[NewDataSet] => Array
(
[CheckingResult] => Array
(
[ID] => 5809990008454
[DigitOk] => true
[!diffgr:id] => CheckingResult1
[!msdata:rowOrder] => 0
[!diffgr:hasChanges] => inserted
)

[Message] => Array
(
[Code] => W00000
[Description] => ???????????????????? Success "
[!diffgr:id] => Message1
[!msdata:rowOrder] => 0
[!diffgr:hasChanges] => inserted
)

)

)

)

)
5809990008454
true


ชื่อ: 
เว็บไซต์: 
คอมเมนต์:




smilebig smileopen-mounthed smileconfused smilesad smileangry smiletonguequestionembarrassedsurprised smilewinkdouble winkcry
ว้าวๆๆ เดี๋ยว ไว้ลองแล้วจะมาบอกกล่าวครับ
#1  by  ฟิวส์ At 2006-02-24 20:01, 
result
Array
(
[faultcode] => soap:Server
[faultstring] => Server was unable to process request. --> Value cannot be null.
Parameter name: password
[detail] =>
)
#2  by  GetStart (203.154.50.22 /203.158.160.253) At 2006-04-19 00:22, 
ไม่ทราบว่าตรง
$document="<ServicePIN xmlns='anonymousanonymous3500500528993https://rdws.rd.go.th/ServiceRD/CheckTINPINService'><username>anonymous</username><password>anonymous</password> <PIN>3500500528993</PIN> </ServicePIN>";

พิมอย่างนี้ถูกหรือเปล่าครับ มันบอกว่า faultstring กับ faultcode พอดีดูตรงข้างบนไม่ค่อยชัดเจนครับ
#3  by   (203.154.50.22 /203.158.160.253) At 2006-04-19 00:24, 
$document='<ServicePIN xmlns="https://rdws.rd.go.th/ServiceRD/CheckTINPINService"><username>anonymous</username><password>anonymous</password><PIN>5809990008454</PIN></ServicePIN>';
ที่ถูกครับ
#4  by  Code SNippet At 2006-04-19 18:01, 

<< Home