ซ่อมคอมพิวเตอร์นอกสถานที่ บางกะปิ
www.becomz.com

  • ซ่อมคอมพิวเตอร์นอกสถานที่ บางกะปิ รามคำแหง

    ซ่อมคอมถึงบ้าน,ซ่อมคอมพิวเตอร์ถึงบ้าน,ซ่อมคอมนอกสถานที่,ซ่อมคอมพิวเตอร์ นอกสถานที่,วางระบบอินเตอร์เน็ต,วางระบบแลน,ระบบเน็คเวิร์ค,เขียนโปรแกรมเว็บไซด์,ดูแลคอมพิวเตอร์แบบรายเดือน-รายปี,พร้อมบริการด้านไอทีจ่าย. สนใจติดต่อ 095-954-4524

  • หากคุณกำลังมองหาสถานที่ รับซ่อมคอมถึงที่

    ราคือหน่วยรับซ่อมคอมพิวเตอร์ถึงที่ ไม่ว่าจะเป็นที่บ้าน ที่ทำงาน บริษัท ห้าง ร้าน สถานสงเคราะห์ โรงเรียน โรงพยาบาล ฯลฯ เราจะไปบริการซ่อมให้ในราคาสุดประหยัด ถูกกว่ายกไปซ่อมที่ห้างหรือร้านซ่อมแน่นอน เนื่องจากทางร้านของเราไม่ต้องเสียค่าเช่าพื้นที่ จึงสามารถลดต้นทุนในส่วนนี้ได้. สนใจติดต่อ 095-954-4524

  • www.becomz.com ให้บริการถึงที่

    บริการซ่อมคอมพิวเตอร์นอกสถานที่ โดยไม่ต้องยก เครื่องคอมให้เหนื่อย หรือ เสียเวลา การทำงานของคุณ เรา คือ ทางออกสำหรับคุณ ที่จะไป บริการถึงบ้าน ที่บ้าน หรือ อ๊อฟฟิต ( office ) และ คอนโด อาพาทเม้น ทุกสถานที่ พร้อม ทั้ง ให้ บริการซ่อมคอมพิวเตอร์ 24 ชั่วโมง สำหรับ ลูกค้าบางท่านที่สะดวก. สนใจติดต่อ 095-954-4524

  • ค่าบริการ

    – ซ่อมโปรแกรม แก้ปัญหาด้านโปรแกรมทั่วไป เครื่องละ 500 บาท – เเละลง Driver 300 บาท รวมกับ ซ่อมปกติเป็น 700 บาท – อะไหล่เสีย จะแจ้งราคาอะไหล่ก่อนซ่อม (ลูกค้าสามารถจัดหาอะไหล่เองได้) เพื่อความมั่นใจ ซ่อมเสร็จเรารับประกันซอฟเเวร์ 7วัน พร้อมให้คำแนะนำ และบริการหลังซ่อม ตลอดการรับประกันน ติดตั้งให้ถึงที่ .สนใจติดต่อ 095-954-4524

  • รับซ่อมทุกปัญหา โทรมาคุยกันก่อนได้ครับ

    – บริการอัพเกรดเครื่อง แก้ปัญหาเครื่องช้า รวนบ่อย ค้างบ่อย – บริการติดตั้ง แก้ปัญหา ระบบคอมพิวเตอร์ ระบบแลน-อินเตอร์เน็ต – บริการลงวินโดว์, ลงโปรแกรม, แก้ไวรัส, แก้ปัญหาต่างๆ – บริการฝากซ่อม-เคลม อะไหล่คอมฯ และสินค้าไอที ทุกชนิด – บริการจัดสเป๊คเครื่อง จัดชุดคอมมือ1-2 พร้อมใช้งาน ติดตั้งให้ถึงที่ .สนใจติดต่อ 095-954-4524

วันอาทิตย์ที่ 15 กรกฎาคม พ.ศ. 2555

PHP Create - Call Web Service สร้างและเรียกเว็บเซอร์วิส ด้วย PHP (NuSoap and Soap)

PHP Create - Call Web Service สร้างและเรียกเว็บเซอร์วิส ด้วย PHP (NuSoap and Soap) บทความการสร้าง Web Service และการเรียกใช้งาน Web Service ด้วย PHP แบบง่าย ๆ ผ่าน Library ของ NuSoap /Soap และฟังก์ชั่น soap_server ใน php การเรียกใช้งาน Web Service ฝั่ง Server หรือ Client จะต้องเรียกผ่าน wsdl ซึ่งเป็น XML Format ที่บ่งบอกและอธิบายเกี่ยวกับ Web Service ที่ใหบริการอยู่ว่ามีรูปแบบและการเรียกใช้งานอย่างไรบ้าง เช่น Service Name , Method หรือ ค่าParameters ต่าง ที่ให้บริการอยู่ในขณะนั้น และเมื่อเราสร้างไฟล์ Web Service ที่มีนามสกุล .php และเรียก URL ตามด้วย file.php?wsdl ก็จะได้ XML Format ออกมา ซึ่ง Web Service ที่ได้จะสามารถเรียกและใช้งานได้กับทุกภาษาที่มีมาตฐานการติดต่อผ่านรูปแบบ wsdl

ตัวอย่างที่ 1 ใช้ NuSoap

PHP Create - Calling Web Service

Download Library ของ NuSoap


หรือจะดาวน์โหลดได้จากในส่วนล่างของบทความนี้

สำหรับการสร้าง Web Service ในตัวอย่างนี้จะแบ่งออกเป็น 2 ประเภทคือ
- Web Service ฝั่ง Server ที่ให้บริการ 
- Web Service ฝั่ง Client ที่เรียกใช้ Web Service ฝั่ง Server

Web Service ฝั่ง Server

WebServiceServer.php
01.<?
02.require_once("lib/nusoap.php");
03. 
04.//Create a new soap server
05.$server new soap_server();
06. 
07.//Define our namespace
09.$server->wsdl->schemaTargetNamespace = $namespace;
10. 
11.//Configure our WSDL
12.$server->configureWSDL("HelloWorld");
13. 
14.// Register our method and argument parameters
15.$varname array(
16.'strName' => "xsd:string",
17.'strEmail' => "xsd:string"
18.);
19.$server->register('HelloWorld',$varnamearray('return' => 'xsd:string'));
20. 
21.function HelloWorld($strName,$strEmail)
22.{
23.return "Hello, World! Khun ($strName , Your email : $strEmail)";
24.}
25. 
26.// Get our posted data if the service is being consumed
27.// otherwise leave this data blank.
28.$POST_DATA = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] :'';
29. 
30.// pass our posted data (or nothing) to the soap service
31.$server->service($POST_DATA);
32.exit();
33.?>


จาก Code มีการสร้าง Service ชื่อว่า HelloWorld และประกอบด้วย Method ชื่อว่า HelloWorld รับค่า argument 2 ตัวคือ $strName และ $strName และทำการ return return "Hello, World! Khun ($strName , Your email : $strEmail)";

ในไฟล์นี้จะจัดเก็บไว้ในโฟเดอร์ \nusoap\WebServiceServer.php

ทดสอบการรัน Web Service


PHP Create - Calling Web Service

ในตัวอย่างจะเห็น URL ของ Web Service ประกอบด้วย Method ชื่อว่า HellowWorld ให้คลิกที่ WSDL เพื่อดูรูปแบบของ XML

PHP Create - Calling Web Service



เมื่อรัน URL ของ Web Service ตามด้วย ?wsdl จะได้ XML ที่ประกอบด้วย Format ของ wsdl ดังรูป ซึ่งตอนที่นำไปใช้งานจริง ๆ เราจะใช้ URL นี้

รูปแบบ XML ที่ได้
01.<?xml version="1.0" encoding="ISO-8859-1" ?>
03.<types>
04.<xsd:schema targetNamespace="http://localhost/soap/HelloWorld">
05.<xsd:import namespace="http://schemas.xmlsoap.org/soap/encoding/" />
06.<xsd:import namespace="http://schemas.xmlsoap.org/wsdl/" />
07.</xsd:schema>
08.</types>
09.<message name="HelloWorldRequest">
10.<part name="strName" type="xsd:string" />
11.<part name="strEmail" type="xsd:string" />
12.</message>
13.<message name="HelloWorldResponse">
14.<part name="return" type="xsd:string" />
15.</message>
16.<portType name="HelloWorldPortType">
17.<operation name="HelloWorld">
18.<input message="tns:HelloWorldRequest" />
19.<output message="tns:HelloWorldResponse" />
20.</operation>
21.</portType>
22.<binding name="HelloWorldBinding" type="tns:HelloWorldPortType">
23.<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" />
24.<operation name="HelloWorld">
25.<soap:operation soapAction="http://localhost/nusoap/WebServiceServer.php/HelloWorld" style="rpc"/>
26.<input>
27.<soap:body use="encoded" namespace="" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
28.</input>
29.<output>
30.<soap:body use="encoded" namespace="" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
31.</output>
32.</operation>
33.</binding>
34.<service name="HelloWorld">
35.<port name="HelloWorldPort" binding="tns:HelloWorldBinding">
36.<soap:address location="http://localhost/nusoap/WebServiceServer.php" />
37.</port>
38.</service>
39.</definitions>





Web Service ฝั่ง Client

สร้างไฟล์สำหรับ Client เพื่อทดสอบเรียก Web Sevice 

WebServiceClient.php
01.<html>
02.<head>
03.<title>ThaiCreate.Com</title>
04.</head>
05.<body>
06.<?
07.require_once("lib/nusoap.php");
08.$client new nusoap_client("http://localhost/nusoap/WebServiceServer.php?wsdl",true);
09.$params array(
10.'strName' => "Weerachai Nukitram",
11.'strEmail' => "is_php@hotmail.com"
12.);
13.$data $client->call("HelloWorld",$params);
14.echo $data;
15.?>
16.</body>
17.</html>


ในตัวอย่าง มีการระบุ URL ของ Web Service ในส่วนของ new nusoap_client

$client->call("HelloWorld",$params);
// HelloWorld คือ Method ที่อยู่ใน Web Service
// $params คือ argument หรือ parameter ที่จะต้องส่งไป


เมื่อลอง echo $data; ก็จะได้สิ่งที่ Web Service ตอบกลับมา ซึ่งอาจจะอยู่ในรูปแบบของ XML หรือ String

Screenshot

PHP Create - Calling Web Service

Download Code !! 






ตัวอย่างที่ 2 ใช้ Soap

PHP Create - Calling Web Service

วิธีนี้จะใช้ Class ที่ชื่อว่า SoapServer ที่อยู่ใน php_soap.dll วิธีใช้งานจะต้องดาวน์โหลด extension ตัวนี้มาติดตั้งก่อน 

Download php_soap.dll


หลังจากดาวน์โหลดแล้วให้แตกไฟล์และ Copy ไปไว้ในโฟเดอร์ของ extension ถ้าอยากรู้ว่าอยู่ที่ไหน ก็ให้เปิดไฟล์ php.ini

C:\Windows\php.ini
; Directory in which the loadable extensions (modules) reside.
extension_dir = "D:/AppServ\php5\ext"


หาบรรทัดที่มีคำว่า extension_dir ซึ่งจะบอกว่า Path ของ extension ถูกจัดเก็บไว้ที่ไหน

หลังจาก Copy เรียบร้อยแล้วให้เพิ่มบรรทัดนี้ใน php.ini

C:\Windows\php.ini
extension=php_soap.dll


php_soap.dll and php.ini

หรือดูภาพประกอบ

หลังจากแก้ไขเรียบร้อยแล้วให้ Restart Web Server หรือ Apache ซะ 1 ครั้ง

กรณี Error Class ของ SoapClient
Fatal error: Class 'SoapServer' not found in C:\AppServ\www\SoapServer\WebServiceServer.php on line 2


เริ่มต้นการสร้าง Web Service ด้วย Soap

ขั้นตอนแรกให้สร้างไฟล์ helloworld.wsdl

helloworld.wsdl
01.<?xml version="1.0" encoding="ISO-8859-1" ?>
02.<definitions xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
10.targetNamespace="http://localhost/soap/HelloWorld">
11.<types>
12.<xsd:schema targetNamespace="http://localhost/soap/HelloWorld">
13.<xsd:import namespace="http://schemas.xmlsoap.org/soap/encoding/" />
14.<xsd:import namespace="http://schemas.xmlsoap.org/wsdl/" />
15.</xsd:schema>
16.</types>
17. 
18.<message name="HelloWorldRequest">
19.<part name="strName" type="xsd:string" />
20.<part name="strEmail" type="xsd:string" />
21.</message>
22. 
23.<message name="HelloWorldResponse">
24.<part name="return" type="xsd:string" />
25.</message>
26. 
27.<portType name="HelloWorldPortType">
28.<operation name="HelloWorld">
29.<input message="tns:HelloWorldRequest" />
30.<output message="tns:HelloWorldResponse" />
31.</operation>
32.</portType>
33. 
34.<binding name="HelloWorldBinding" type="tns:HelloWorldPortType">
35.<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" />
36.<operation name="HelloWorld">
37.<soap:operation soapAction="http://localhost/SoapServer/WebServiceServer.php/HelloWorld"style="rpc" />
38.<input>
39.<soap:body use="encoded" namespace=""encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
40.</input>
41.<output>
42.<soap:body use="encoded" namespace=""encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
43.</output>
44.</operation>
45.</binding>
46. 
47.<service name="HelloWorld">
48.<port name="HelloWorldPort" binding="tns:HelloWorldBinding">
49.<soap:address location="http://localhost/SoapServer/WebServiceServer.php" />
50.</port>
51.</service>
52.</definitions>


สำหรับไฟล์ wsdl อ่านรายละเอียดได้ที่
http://wiki.nectec.or.th/setec/Knowledge/WSDL
http://oreilly.com/catalog/webservess/chapter/ch06.html
http://www.basic-skill.com/content.php?cont_id=74&sec_id=3


สร้างไฟล์ Web Service ฝั่ง Server

WebServiceServer.php
01.<?php
02.$server new SoapServer("helloworld.wsdl");
03. 
04.function HelloWorld($strName,$strEmail){
05.return "Hello, World! Khun ($strName , Your email : $strEmail) ";
06.}
07. 
08.$server->AddFunction("HelloWorld");
09.$server->handle();
10.?>


Save ไฟล์ไว้ที่ \SoapServer\WebServiceServer.php อยู่ภายใต้ root ของ Server

ทดสอบรัน URL ของ Web Service



PHP Create - Calling Web Service

ผลลพัทธ์ที่ได้เมื่อทอสอบรัน URL ของ Web Service


Web Service ฝั่ง Client

WebServiceClient.php
01.<html>
02.<head>
03.<title>ThaiCreate.Com</title>
04.</head>
05.<body>
06.<?
08.array(
09."trace"      => 1,     // enable trace to view what is happening
10."exceptions" => 0,     // disable exceptions
11."cache_wsdl" => 0)         // disable any caching on the wsdl, encase you alter the wsdl server
12.);
13. 
14.$params array(
15.'strName' => "Weerachai Nukitram",
16.'strEmail' => "is_php@hotmail.com"
17.);
18. 
19.$data $client->HelloWorld($params);
20. 
21.print_r($data);
22.?>
23.</body>
24.</html>


ทดสอบการเรียก URL ของ Web Service ฝั่ง Client 

Screenshot

PHP Create - Calling Web Service


Download Code !!


บทความนี้เป็นเพียงพื้นฐานการสร้าง Web Service แบบง่าย ๆ แต่ทั้งนี้สามารถนำไปประยุกต์กับการใช้งานที่ซับซ้อน เช่นการใช้งานร่วมกับ MySQL ซึ่งไว้มีโอกาสจะลองเขียนบทความให้หลากหลายเพื่อเป็นตัวอย่างไว้ให้ศึกษา 


สรุปถ้าจะให้เลือกใช้ระหว่าง NuSoap และ Soap ผมแนะนำให้ใช้ NuSoap ดีกว่า เพราะใช้งานและเข้าใจง่ายกกว่าครับ

และผิดพลาดประการใดขออภัยมา ณ ที่นี้ด้วยครับ
Share:

Disqus Shortname

Comments system

ซ่อมคอมพิวเตอร์นอกสถานที่ บางกะปิ 095-954-4524

ขับเคลื่อนโดย Blogger.

จำนวนการดูหน้าเว็บรวม

Blog Archive

Post Top Ad

คลังบทความของบล็อก

Author Details

Menu - Pages

Business

Random Posts

Recent

Popular

Blog Archive