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 = "D:/AppServ\php5\ext"
หาบรรทัดที่มีคำว่า extension_dir ซึ่งจะบอกว่า Path ของ extension ถูกจัดเก็บไว้ที่ไหน
หลังจาก Copy เรียบร้อยแล้วให้เพิ่มบรรทัดนี้ใน php.ini
C:\Windows\php.ini
extension=php_soap.dll
หรือดูภาพประกอบ
หลังจากแก้ไขเรียบร้อยแล้วให้ Restart Web Server หรือ Apache ซะ 1 ครั้ง
กรณี Error Class ของ SoapClient
Fatal error: Class 'SoapClient' not found in /WebService1.php line 5
อันนี้เกิดจากยังไม่ได้ติดตั้ง php_soap.dll หรือ ติดตั้งไม่สมบูรณ์
อธิบายเบื้องต้น ตัวอย่างนี้จะใช้ Web Service เหมือนกับบทความก่อนหน้านี้ โดยใช้ 3 ตัวนี้
Go to : ASP.NET การสร้าง Web Service และการเรียกใช้งาน Web Service ด้วย ASP.NET แบบ Step by Step
Go to : ASP.NET กับ Web Service การเขียนเว็บเซอร์วิสรับ-ส่งข้อมูลจาก Database ผ่าน DataSet และ DataTable
Go to : Check Login ผ่าน Web Service ตัวอย่างเว็บเซอร์วิส ตรวจสอบ Username และ Password (.NET)
ตัวอย่างที่ 1 จากบทความ
Go to : ASP.NET การสร้าง Web Service และการเรียกใช้งาน Web Service ด้วย ASP.NET แบบ Step by Step
ตัวอย่างนี้เป็นการให้บริการ Web Service ที่แสดงคำว่า Hello World Khun ( ค่าที่ส่งไป)
มี Method ชื่อว่า HelloWorld และรับค่า Parameter คำว่า strName
Code ที่อยู่ใน Web Service
01.Imports System.Web.Services02.Imports System.Web.Services.Protocols03.Imports System.ComponentModel04. 05.' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.06.' <System.Web.Script.Services.ScriptService()> _07.<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _08.<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _09.<ToolboxItem(False)> _10.Public Class Service111.Inherits System.Web.Services.WebService12. 13.<WebMethod()> _14.Public Function HelloWorld(ByVal strName As String) As String15.Return "Hello World Khun ( " & strName & " )"16.End Function17. 18.End Classจะเห็นว่ามี Method ที่ชื่อว่า HelloWorld และรับค่า strName และมีการ Return "Hello World Khun ( " & strName & " )"
การเรียกใช้งานผ่าน PHP ด้วย SoapClient
WebService1.php
01.<html>02.<head>03.<title>ThaiCreate.Com</title>04.</head>05.<body>06.<?07.$client = new SoapClient("http://localhost:5377/Service1.asmx?wsdl",08.array(09."trace" => 1, // enable trace to view what is happening10."exceptions" => 0, // disable exceptions11."cache_wsdl" => 0) // disable any caching on the wsdl, encase you alter the wsdl server12.);13. 14.$params = array(15.'strName' => "Weerachai Nukitram"16.);17. 18.$data = $client->HelloWorld($params);19. 20.print_r($data);21. 22.echo "<hr>";23. 24.echo $data->HelloWorldResult;25. 26.echo "<hr>";27. 28.// display what was sent to the server (the request)29.echo "<p>Request :".htmlspecialchars($client->__getLastRequest()) ."</p>";30. 31.echo "<hr>";32. 33.// display the response from the server34.echo "<p>Response:".htmlspecialchars($client->__getLastResponse())."</p>";35. 36.?>37.</body>38.</html>ใน PHP การเรียกใช้งาน Web Service ของ .NET จะต้องเติม ?wsdl เข้าไปด้วย
$client->HelloWorld($params);
- HelloWorld คือ Method ใน Web Service
- $params คือ Parameter ใน Web Service
ทดสอบเรียกด้วยการรัน URL ของ PHP ที่ชื่อว่า WebService1.php
Screenshot
คำอธิบาย
จากรูปจะเห็นสิ่งที่ Web Service ได้ทำการ return ค่ากลับมาในรูปแบบของ XML (ในตัวอย่างจะใช้ print_r เพื่อแสดงค่าในรูปแบบของ array ทั้งนี้สามารถใช้ function ของ XML ใน php อ่านค่าเหล่านี้เพื่อนำไปใช้)
กรณีที่รับส่งค่าสามารถใช้ JSON ในการรับส่งค่าจาก Web Service ของ ASP.NET ได้ ลองอ่านบทความนี้
Go to : ASP.NET กับ JSON และการรับ-ส่งข้อมูล JSON ผ่าน Web Service (VB.NET , C#)
ตัวอย่างที่ 2 จากบทความ
Go to : ASP.NET กับ Web Service การเขียนเว็บเซอร์วิสรับ-ส่งข้อมูลจาก Database ผ่าน DataSet และ DataTable
ตัวอย่างนี้เป็นการให้บริการ Web Service ที่แสดงรายละเอียดของลูกค้า เมื่อมีการส่ง CustomerID ไปยัง Web Service
มี Method ชื่อว่า DetailCustomer และรับค่า Parameter คำว่า strCustomerID
Code ที่อยู่ใน Web Service
01.Imports System.Web.Services02.Imports System.Web.Services.Protocols03.Imports System.ComponentModel04.Imports System.Data05.Imports System.Data.SqlClient06. 07.' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.08.' <System.Web.Script.Services.ScriptService()> _09.<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _10.<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _11.<ToolboxItem(False)> _12.Public Class getCustomerDetail13.Inherits System.Web.Services.WebService14. 15.<WebMethod()> _16.Public Function DetailCustomer(ByVal strCusID As String) As DataTable17.Dim objConn As New System.Data.SqlClient.SqlConnection18.Dim objCmd As New System.Data.SqlClient.SqlCommand19.Dim dtAdapter As New System.Data.SqlClient.SqlDataAdapter20. 21.Dim ds As New DataSet22.Dim dt As DataTable23.Dim strConnString As String24.Dim strSQL As New StringBuilder25. 26.strConnString = "Server=localhost;UID=sa;PASSWORD=;database=mydatabase;Max Pool Size=400;Connect Timeout=600;"27. 28.strSQL.Append(" SELECT * FROM customer ")29.strSQL.Append(" WHERE [CustomerID] = '" & strCusID & "' ")30. 31.objConn.ConnectionString = strConnString32.With objCmd33..Connection = objConn34..CommandText = strSQL.ToString()35..CommandType = CommandType.Text36.End With37.dtAdapter.SelectCommand = objCmd38. 39.dtAdapter.Fill(ds)40.dt = ds.Tables(0)41. 42.dtAdapter = Nothing43.objConn.Close()44.objConn = Nothing45. 46.Return dt47. 48.End Function49. 50.End Classจะเห็นว่ามี Method ที่ชื่อว่า DetailCustomer และรับค่า strCusID และมีการ Return เป็น DataTable ซึ่งใน .NET สามารถรับค่าเป็น DataTable ได้ในทันที แต่ใน PHP จะต้องใช้ function ของ XML ในการอ่านค่าอีกที
การเรียกใช้งานผ่าน PHP ด้วย SoapClient
WebService2.php
01.<html>02.<head>03.<title>ThaiCreate.Com</title>04.</head>05.<body>06.<?07.$client = new SoapClient("http://localhost:5377/getCustomerDetail.asmx?wsdl",08.array(09."trace" => 1, // enable trace to view what is happening10."exceptions" => 0, // disable exceptions11."cache_wsdl" => 0) // disable any caching on the wsdl, encase you alter the wsdl server12.);13. 14.$params = array(15.'strCusID' => "C001"16.);17. 18.$data = $client->DetailCustomer($params);19. 20.print_r($data);21. 22.echo "<hr>";23. 24.// display what was sent to the server (the request)25.echo "<p>Request :".htmlspecialchars($client->__getLastRequest()) ."</p>";26. 27.echo "<hr>";28. 29.// display the response from the server30.echo "<p>Response:".htmlspecialchars($client->__getLastResponse())."</p>";31. 32.?>33.</body>34.</html>ใน PHP การเรียกใช้งาน Web Service ของ .NET จะต้องเติม ?wsdl เข้าไปด้วย
$client->DetailCustomer($params);
- DetailCustomer คือ Method ใน Web Service
- $params คือ Parameter ใน Web Service
ทดสอบเรียกด้วยการรัน URL ของ PHP ที่ชื่อว่า WebService2.php
Screenshot
คำอธิบาย
จากรูปจะเห็นสิ่งที่ Web Service ได้ทำการ return ค่ากลับมาในรูปแบบของ XML (ในตัวอย่างจะใช้ print_r เพื่อแสดงค่าในรูปแบบของ array ทั้งนี้สามารถใช้ function ของ XML ใน php อ่านค่าเหล่านี้เพื่อนำไปใช้)
กรณีที่รับส่งค่าสามารถใช้ JSON ในการรับส่งค่าจาก Web Service ของ ASP.NET ได้ ลองอ่านบทความนี้
Go to : ASP.NET กับ JSON และการรับ-ส่งข้อมูล JSON ผ่าน Web Service (VB.NET , C#)
ตัวอย่างที่ 3 จากบทความ
Go to : Check Login ผ่าน Web Service ตัวอย่างเว็บเซอร์วิส ตรวจสอบ Username และ Password (.NET)
ตัวอย่างนี้เป็นการให้บริการ Web Service ที่แสดงสถานะการ Login เมื่อส่ง Username และ Password เข้าไปตรวจสอบใน Web Service โดยถ้าข้อมูลถูกต้องจะ return ค่า true (1) และ เมื่อไม่ถูกต้องจะ return ค่า false(0)
มี Method ชื่อว่า CheckUserLogin และรับค่า Parameter คำว่า strUser และ strPassword
Code ที่อยู่ใน Web Service
01.Imports System.Web.Services02.Imports System.Web.Services.Protocols03.Imports System.ComponentModel04.Imports System.Data05.Imports System.Data.SqlClient06. 07.' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.08.' <System.Web.Script.Services.ScriptService()> _09.<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _10.<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _11.<ToolboxItem(False)> _12.Public Class userLogin13.Inherits System.Web.Services.WebService14. 15.<WebMethod()> _16.Public Function CheckUserLogin(ByVal strUser As String, _17.ByVal strPassword As String) As Boolean18. 19.Dim objConn As New SqlConnection20.Dim objCmd As New SqlCommand21.Dim dtAdapter As SqlDataAdapter22. 23.Dim dt As New DataTable24.Dim strConnString As String25.Dim strSQL As New StringBuilder26.strConnString = "Server=localhost;UID=sa;PASSWORD=;database=mydatabase;Max Pool Size=400;Connect Timeout=600;"27. 28.objConn.ConnectionString = strConnString29. 30.strSQL.Append(" SELECT * FROM member ")31.strSQL.Append(" WHERE [Username] = @sUsername ")32.strSQL.Append(" AND [Password] = @sPassword ")33. 34.dtAdapter = New SqlDataAdapter(strSQL.ToString(), objConn)35. 36.objCmd = dtAdapter.SelectCommand37.With objCmd38..Parameters.Add("@sUsername", SqlDbType.VarChar).Value = strUser39..Parameters.Add("@sPassword", SqlDbType.VarChar).Value = strPassword40.End With41. 42.dtAdapter.Fill(dt)43. 44.dtAdapter = Nothing45.objConn.Close()46.objConn = Nothing47. 48.If dt.Rows.Count > 0 Then49.Return True50.Else51.Return False52.End If53. 54.End Function55. 56.End Classจะเห็นว่ามี Method ที่ชื่อว่า CheckUserLogin และรับค่า strUser และ strPassword และมีการ Return ค่า true(1) กรณีที่ถูกต้อง และจะมีการ return ค่าเป็น false(0) กรณีที่ข้อมูลไม่ถูกต้อง
การเรียกใช้งานผ่าน PHP ด้วย SoapClient
WebService3.php
01.<html>02.<head>03.<title>ThaiCreate.Com</title>04.</head>05.<body>06.<?07.$client = new SoapClient("http://localhost:5377/userLogin.asmx?wsdl",08.array(09."trace" => 1, // enable trace to view what is happening10."exceptions" => 0, // disable exceptions11."cache_wsdl" => 0) // disable any caching on the wsdl, encase you alter the wsdl server12.);13. 14.$params = array(15.'strUser' => "win",16.'strPassword' => "win001",17.);18. 19.$data = $client->CheckUserLogin($params);20. 21.print_r($data);22. 23.echo "<hr>";24. 25.echo $data->CheckUserLoginResult;26. 27.echo "<hr>";28. 29.// display what was sent to the server (the request)30.echo "<p>Request :".htmlspecialchars($client->__getLastRequest()) ."</p>";31. 32.echo "<hr>";33. 34.// display the response from the server35.echo "<p>Response:".htmlspecialchars($client->__getLastResponse())."</p>";36. 37.?>38.</body>39.</html>ใน PHP การเรียกใช้งาน Web Service ของ .NET จะต้องเติม ?wsdl เข้าไปด้วย
$client->CheckUserLogin($params);
- CheckUserLogin คือ Method ใน Web Service
- $params คือ Parameter ใน Web Service
ทดสอบเรียกด้วยการรัน URL ของ PHP ที่ชื่อว่า WebService3.php
Screenshot
คำอธิบาย
จากรูปจะเห็นสิ่งที่ Web Service ได้ทำการ return ค่า 1 (true) กลับมา แต่จะออกมาในรูปแบบของ XML (ในตัวอย่างจะใช้ print_r เพื่อแสดงค่าในรูปแบบของ array ทั้งนี้สามารถใช้ function ของ XML ใน php อ่านค่าเหล่านี้เพื่อนำไปใช้)
No comments:
Post a Comment