Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

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



 
Clound SSD Virtual Server

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
<?php
		require_once("lib/nusoap.php");
		 
		//Create a new soap server
		$server = new soap_server();
		 
		//Define our namespace
		$namespace = "http://localhost/nusoap/index.php";
		$server->wsdl->schemaTargetNamespace = $namespace;
		 
		//Configure our WSDL
		$server->configureWSDL("HelloWorld");
		 
		// Register our method and argument parameters
        $varname = array(
                   'strName' => "xsd:string",
				   'strEmail' => "xsd:string"
        );
		$server->register('HelloWorld',$varname, array('return' => 'xsd:string'));
		 
		function HelloWorld($strName,$strEmail)
		{
			return "Hello, World! Khun ($strName , Your email : $strEmail)";
		}
		 
		// Get our posted data if the service is being consumed
		// otherwise leave this data blank.
		$POST_DATA = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : '';
		 
		// pass our posted data (or nothing) to the soap service
		$server->service($POST_DATA);
		exit(); 
?>


จาก 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 ที่ได้
  <?xml version="1.0" encoding="ISO-8859-1" ?> 
  <definitions 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="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="http://localhost/soap/HelloWorld" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://localhost/soap/HelloWorld">
  <types>
  <xsd:schema targetNamespace="http://localhost/soap/HelloWorld">
  <xsd:import namespace="http://schemas.xmlsoap.org/soap/encoding/" /> 
  <xsd:import namespace="http://schemas.xmlsoap.org/wsdl/" /> 
  </xsd:schema>
  </types>
  <message name="HelloWorldRequest">
  <part name="strName" type="xsd:string" /> 
  <part name="strEmail" type="xsd:string" /> 
  </message>
  <message name="HelloWorldResponse">
  <part name="return" type="xsd:string" /> 
  </message>
  <portType name="HelloWorldPortType">
  <operation name="HelloWorld">
  <input message="tns:HelloWorldRequest" /> 
  <output message="tns:HelloWorldResponse" /> 
  </operation>
  </portType>
  <binding name="HelloWorldBinding" type="tns:HelloWorldPortType">
  <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" /> 
  <operation name="HelloWorld">
  <soap:operation soapAction="http://localhost/nusoap/WebServiceServer.php/HelloWorld" style="rpc" /> 
  <input>
  <soap:body use="encoded" namespace="" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" /> 
  </input>
  <output>
  <soap:body use="encoded" namespace="" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" /> 
  </output>
  </operation>
  </binding>
  <service name="HelloWorld">
  <port name="HelloWorldPort" binding="tns:HelloWorldBinding">
  <soap:address location="http://localhost/nusoap/WebServiceServer.php" /> 
  </port>
  </service>
  </definitions>





Web Service ฝั่ง Client

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

WebServiceClient.php
<html>
<head>
<title>ThaiCreate.Com</title>
</head>
<body>
<?php
		require_once("lib/nusoap.php");
        $client = new nusoap_client("http://localhost/nusoap/WebServiceServer.php?wsdl",true); 
        $params = array(
                   'strName' => "Weerachai Nukitram",
				   'strEmail' => "[email protected]"
        );
       $data = $client->call("HelloWorld",$params); 
       echo $data;
?>
</body>
</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
<?xml version="1.0" encoding="ISO-8859-1" ?> 
 <definitions 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="http://schemas.xmlsoap.org/soap/encoding/" 
	xmlns:tns="http://localhost/SoapServer/HelloWorld" 
	xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" 
	xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" 
	xmlns="http://schemas.xmlsoap.org/wsdl/" 
	targetNamespace="http://localhost/soap/HelloWorld">
  <types>
  <xsd:schema targetNamespace="http://localhost/soap/HelloWorld">
  <xsd:import namespace="http://schemas.xmlsoap.org/soap/encoding/" /> 
  <xsd:import namespace="http://schemas.xmlsoap.org/wsdl/" /> 
  </xsd:schema>
  </types>

  <message name="HelloWorldRequest">
	  <part name="strName" type="xsd:string" /> 
	  <part name="strEmail" type="xsd:string" /> 
  </message>

  <message name="HelloWorldResponse">
	<part name="return" type="xsd:string" /> 
  </message>

  <portType name="HelloWorldPortType">
  <operation name="HelloWorld">
	  <input message="tns:HelloWorldRequest" /> 
	  <output message="tns:HelloWorldResponse" /> 
  </operation>
  </portType>

  <binding name="HelloWorldBinding" type="tns:HelloWorldPortType">
	  <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" /> 
	  <operation name="HelloWorld">
	  <soap:operation soapAction="http://localhost/SoapServer/WebServiceServer.php/HelloWorld" style="rpc" /> 
	  <input>
		<soap:body use="encoded" namespace="" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" /> 
	  </input>
	  <output>
		 <soap:body use="encoded" namespace="" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" /> 
	  </output>
	  </operation>
  </binding>

  <service name="HelloWorld">
 <port name="HelloWorldPort" binding="tns:HelloWorldBinding">
	<soap:address location="http://localhost/SoapServer/WebServiceServer.php" /> 
  </port>
  </service>
</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
<?php
$server = new SoapServer("helloworld.wsdl");

	function HelloWorld($strName,$strEmail){
		 return "Hello, World! Khun ($strName , Your email : $strEmail) ";
	}

$server->AddFunction("HelloWorld");
$server->handle();
?>


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

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



PHP Create - Calling Web Service

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



Web Service ฝั่ง Client

WebServiceClient.php
<html>
<head>
<title>ThaiCreate.Com</title>
</head>
<body>
<?php
	$client = new SoapClient("http://localhost/SoapServer/WebServiceServer.php?wsdl",
			array(
			  "trace"      => 1,		// enable trace to view what is happening
			  "exceptions" => 0,		// disable exceptions
			  "cache_wsdl" => 0) 		// disable any caching on the wsdl, encase you alter the wsdl server
		  );

        $params = array(
                   'strName' => "Weerachai Nukitram",
				   'strEmail' => "[email protected]"
        );

		$data = $client->HelloWorld($params);

       print_r($data);
?>
</body>
</html>


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

Screenshot

PHP Create - Calling Web Service


Download Code !!


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


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

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


บทความอื่น ๆ ที่เกี่ยวข้อง
Go to : ขอคำแนะนำเรื่องเกี่ยวกับการทำ web service ด้วย PHP ครับ (SOAP WSDL และ SoapServer) - Create Web Service


   
Share
Bookmark.   

  By : TC Admin
  Article : บทความเป็นการเขียนโดยสมาชิก หากมีปัญหาเรื่องลิขสิทธิ์ กรุณาแจ้งให้ทาง webmaster ทราบด้วยครับ
  Score Rating :
  Create Date : 2012-05-08
  Download : No files
Sponsored Links
ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 00
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่