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.Services
02.
Imports
System.Web.Services.Protocols
03.
Imports
System.ComponentModel
04.
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
Service1
11.
Inherits
System.Web.Services.WebService
12.
13.
<WebMethod()> _
14.
Public
Function
HelloWorld(
ByVal
strName
As
String
)
As
String
15.
Return
"Hello World Khun ( "
& strName &
" )"
16.
End
Function
17.
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 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.
);
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 server
34.
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.Services
02.
Imports
System.Web.Services.Protocols
03.
Imports
System.ComponentModel
04.
Imports
System.Data
05.
Imports
System.Data.SqlClient
06.
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
getCustomerDetail
13.
Inherits
System.Web.Services.WebService
14.
15.
<WebMethod()> _
16.
Public
Function
DetailCustomer(
ByVal
strCusID
As
String
)
As
DataTable
17.
Dim
objConn
As
New
System.Data.SqlClient.SqlConnection
18.
Dim
objCmd
As
New
System.Data.SqlClient.SqlCommand
19.
Dim
dtAdapter
As
New
System.Data.SqlClient.SqlDataAdapter
20.
21.
Dim
ds
As
New
DataSet
22.
Dim
dt
As
DataTable
23.
Dim
strConnString
As
String
24.
Dim
strSQL
As
New
StringBuilder
25.
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 = strConnString
32.
With
objCmd
33.
.Connection = objConn
34.
.CommandText = strSQL.ToString()
35.
.CommandType = CommandType.Text
36.
End
With
37.
dtAdapter.SelectCommand = objCmd
38.
39.
dtAdapter.Fill(ds)
40.
dt = ds.Tables(0)
41.
42.
dtAdapter =
Nothing
43.
objConn.Close()
44.
objConn =
Nothing
45.
46.
Return
dt
47.
48.
End
Function
49.
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 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.
'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 server
30.
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.Services
02.
Imports
System.Web.Services.Protocols
03.
Imports
System.ComponentModel
04.
Imports
System.Data
05.
Imports
System.Data.SqlClient
06.
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
userLogin
13.
Inherits
System.Web.Services.WebService
14.
15.
<WebMethod()> _
16.
Public
Function
CheckUserLogin(
ByVal
strUser
As
String
, _
17.
ByVal
strPassword
As
String
)
As
Boolean
18.
19.
Dim
objConn
As
New
SqlConnection
20.
Dim
objCmd
As
New
SqlCommand
21.
Dim
dtAdapter
As
SqlDataAdapter
22.
23.
Dim
dt
As
New
DataTable
24.
Dim
strConnString
As
String
25.
Dim
strSQL
As
New
StringBuilder
26.
strConnString =
"Server=localhost;UID=sa;PASSWORD=;database=mydatabase;Max Pool Size=400;Connect Timeout=600;"
27.
28.
objConn.ConnectionString = strConnString
29.
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.SelectCommand
37.
With
objCmd
38.
.Parameters.Add(
"@sUsername"
, SqlDbType.VarChar).Value = strUser
39.
.Parameters.Add(
"@sPassword"
, SqlDbType.VarChar).Value = strPassword
40.
End
With
41.
42.
dtAdapter.Fill(dt)
43.
44.
dtAdapter =
Nothing
45.
objConn.Close()
46.
objConn =
Nothing
47.
48.
If
dt.Rows.Count > 0
Then
49.
Return
True
50.
Else
51.
Return
False
52.
End
If
53.
54.
End
Function
55.
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 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.
'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 server
35.
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