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 กับ SoapClient และเรียกเว็บเซอร์วิส ของ ASP.NET Web Service (php_soap.dll)



 
Clound SSD Virtual Server

PHP กับ SoapClient และเรียกเว็บเซอร์วิส ของ ASP.NET Web Service (php_soap.dll)

PHP กับ SoapClient และเรียกเว็บเซอร์วิส ของ ASP.NET Web Service (php_soap.dll) บทความนี้เป็นภาคต่อของ Web Service บน .NET ด้วย nusoap แต่จะใช้ Library ของ SoapClient ซึ่งถูกบรรจุไว้ใน php_soap.dll และการใช้ PHP กับ SoapClient จะต้องเปิด extension ของ 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 '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

PHP- Calling .NET Web Service

ตัวอย่างนี้เป็นการให้บริการ Web Service ที่แสดงคำว่า Hello World Khun ( ค่าที่ส่งไป)

PHP- Calling .NET Web Service

มี Method ชื่อว่า HelloWorld และรับค่า Parameter คำว่า strName

Code ที่อยู่ใน Web Service

Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel

' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
' <System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class Service1
    Inherits System.Web.Services.WebService

    <WebMethod()> _
    Public Function HelloWorld(ByVal strName As String) As String
        Return "Hello World Khun ( " & strName & " )"
    End Function

End Class


จะเห็นว่ามี Method ที่ชื่อว่า HelloWorld และรับค่า strName และมีการ Return "Hello World Khun ( " & strName & " )"








การเรียกใช้งานผ่าน PHP ด้วย SoapClient

WebService1.php
<html>
<head>
<title>ThaiCreate.Com</title>
</head>
<body>
<?php
		$client = new SoapClient("http://localhost:5377/Service1.asmx?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"
        );

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

       print_r($data);

	   echo "<hr>";
	   
	   echo $data->HelloWorldResult;

	   echo "<hr>";
 
	  // display what was sent to the server (the request)
	  echo "<p>Request :".htmlspecialchars($client->__getLastRequest()) ."</p>";

	  echo "<hr>";

	  // display the response from the server
	  echo "<p>Response:".htmlspecialchars($client->__getLastResponse())."</p>";

?>
</body>
</html>


ใน PHP การเรียกใช้งาน Web Service ของ .NET จะต้องเติม ?wsdl เข้าไปด้วย

$client->HelloWorld($params);


- HelloWorld คือ Method ใน Web Service
- $params คือ Parameter ใน Web Service

ทดสอบเรียกด้วยการรัน URL ของ PHP ที่ชื่อว่า WebService1.php

Screenshot

PHP Call ASP.NET Web Service SoapClient

คำอธิบาย

จากรูปจะเห็นสิ่งที่ 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

PHP- Calling .NET Web Service

ตัวอย่างนี้เป็นการให้บริการ Web Service ที่แสดงรายละเอียดของลูกค้า เมื่อมีการส่ง CustomerID ไปยัง Web Service

PHP- Calling .NET Web Service

มี Method ชื่อว่า DetailCustomer และรับค่า Parameter คำว่า strCustomerID

Code ที่อยู่ใน Web Service

Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
Imports System.Data
Imports System.Data.SqlClient

' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
' <System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class getCustomerDetail
    Inherits System.Web.Services.WebService

    <WebMethod()> _
    Public Function DetailCustomer(ByVal strCusID As String) As DataTable
        Dim objConn As New System.Data.SqlClient.SqlConnection
        Dim objCmd As New System.Data.SqlClient.SqlCommand
        Dim dtAdapter As New System.Data.SqlClient.SqlDataAdapter

        Dim ds As New DataSet
        Dim dt As DataTable
        Dim strConnString As String
        Dim strSQL As New StringBuilder

        strConnString = "Server=localhost;UID=sa;PASSWORD=;database=mydatabase;Max Pool Size=400;Connect Timeout=600;"

        strSQL.Append(" SELECT * FROM customer ")
        strSQL.Append(" WHERE [CustomerID] = '" & strCusID & "' ")

        objConn.ConnectionString = strConnString
        With objCmd
            .Connection = objConn
            .CommandText = strSQL.ToString()
            .CommandType = CommandType.Text
        End With
        dtAdapter.SelectCommand = objCmd

        dtAdapter.Fill(ds)
        dt = ds.Tables(0)

        dtAdapter = Nothing
        objConn.Close()
        objConn = Nothing

        Return dt

    End Function

End Class


จะเห็นว่ามี Method ที่ชื่อว่า DetailCustomer และรับค่า strCusID และมีการ Return เป็น DataTable ซึ่งใน .NET สามารถรับค่าเป็น DataTable ได้ในทันที แต่ใน PHP จะต้องใช้ function ของ XML ในการอ่านค่าอีกที

การเรียกใช้งานผ่าน PHP ด้วย SoapClient

WebService2.php
<html>
<head>
<title>ThaiCreate.Com</title>
</head>
<body>
<?php
		$client = new SoapClient("http://localhost:5377/getCustomerDetail.asmx?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(
                   'strCusID' => "C001"
        );

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

       print_r($data);

	   echo "<hr>";
 
	  // display what was sent to the server (the request)
	  echo "<p>Request :".htmlspecialchars($client->__getLastRequest()) ."</p>";

	  echo "<hr>";

	  // display the response from the server
	  echo "<p>Response:".htmlspecialchars($client->__getLastResponse())."</p>";

?>
</body>
</html>


ใน PHP การเรียกใช้งาน Web Service ของ .NET จะต้องเติม ?wsdl เข้าไปด้วย

$client->DetailCustomer($params);


- DetailCustomer คือ Method ใน Web Service
- $params คือ Parameter ใน Web Service

ทดสอบเรียกด้วยการรัน URL ของ PHP ที่ชื่อว่า WebService2.php

Screenshot

PHP Call ASP.NET Web Service SoapClient

คำอธิบาย

จากรูปจะเห็นสิ่งที่ 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)

PHP- Calling .NET Web Service

ตัวอย่างนี้เป็นการให้บริการ Web Service ที่แสดงสถานะการ Login เมื่อส่ง Username และ Password เข้าไปตรวจสอบใน Web Service โดยถ้าข้อมูลถูกต้องจะ return ค่า true (1) และ เมื่อไม่ถูกต้องจะ return ค่า false(0)

PHP- Calling .NET Web Service

มี Method ชื่อว่า CheckUserLogin และรับค่า Parameter คำว่า strUser และ strPassword

Code ที่อยู่ใน Web Service

Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
Imports System.Data
Imports System.Data.SqlClient

' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
' <System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class userLogin
    Inherits System.Web.Services.WebService

    <WebMethod()> _
       Public Function CheckUserLogin(ByVal strUser As String, _
        ByVal strPassword As String) As Boolean

        Dim objConn As New SqlConnection
        Dim objCmd As New SqlCommand
        Dim dtAdapter As SqlDataAdapter

        Dim dt As New DataTable
        Dim strConnString As String
        Dim strSQL As New StringBuilder
        strConnString = "Server=localhost;UID=sa;PASSWORD=;database=mydatabase;Max Pool Size=400;Connect Timeout=600;"

        objConn.ConnectionString = strConnString

        strSQL.Append(" SELECT * FROM member ")
        strSQL.Append(" WHERE [Username] = @sUsername ")
        strSQL.Append(" AND [Password] = @sPassword ")

        dtAdapter = New SqlDataAdapter(strSQL.ToString(), objConn)

        objCmd = dtAdapter.SelectCommand
        With objCmd
            .Parameters.Add("@sUsername", SqlDbType.VarChar).Value = strUser
            .Parameters.Add("@sPassword", SqlDbType.VarChar).Value = strPassword
        End With

        dtAdapter.Fill(dt)

        dtAdapter = Nothing
        objConn.Close()
        objConn = Nothing

        If dt.Rows.Count > 0 Then
            Return True
        Else
            Return False
        End If

    End Function

End Class


จะเห็นว่ามี Method ที่ชื่อว่า CheckUserLogin และรับค่า strUser และ strPassword และมีการ Return ค่า true(1) กรณีที่ถูกต้อง และจะมีการ return ค่าเป็น false(0) กรณีที่ข้อมูลไม่ถูกต้อง



การเรียกใช้งานผ่าน PHP ด้วย SoapClient

WebService3.php
<html>
<head>
<title>ThaiCreate.Com</title>
</head>
<body>
<?php
		$client = new SoapClient("http://localhost:5377/userLogin.asmx?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(
                   'strUser' => "win",
				   'strPassword' => "win001",
        );

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

       print_r($data);

	   echo "<hr>";

	   echo $data->CheckUserLoginResult;

	   echo "<hr>";
 
	  // display what was sent to the server (the request)
	  echo "<p>Request :".htmlspecialchars($client->__getLastRequest()) ."</p>";

	  echo "<hr>";

	  // display the response from the server
	  echo "<p>Response:".htmlspecialchars($client->__getLastResponse())."</p>";

?>
</body>
</html>


ใน PHP การเรียกใช้งาน Web Service ของ .NET จะต้องเติม ?wsdl เข้าไปด้วย

$client->CheckUserLogin($params);


- CheckUserLogin คือ Method ใน Web Service
- $params คือ Parameter ใน Web Service

ทดสอบเรียกด้วยการรัน URL ของ PHP ที่ชื่อว่า WebService3.php

Screenshot

PHP Call ASP.NET Web Service SoapClient

คำอธิบาย

จากรูปจะเห็นสิ่งที่ Web Service ได้ทำการ return ค่า 1 (true) กลับมา แต่จะออกมาในรูปแบบของ XML (ในตัวอย่างจะใช้ print_r เพื่อแสดงค่าในรูปแบบของ array ทั้งนี้สามารถใช้ function ของ XML ใน php อ่านค่าเหล่านี้เพื่อนำไปใช้)

กรณีที่รับส่งค่าสามารถใช้ JSON ในการรับส่งค่าจาก Web Service ของ ASP.NET ได้ ลองอ่านบทความนี้
Go to : ASP.NET กับ JSON และการรับ-ส่งข้อมูล JSON ผ่าน Web Service (VB.NET , C#)

Download Code !!

บทความอื่น ๆ ที่เกี่ยวข้อง
Go to : ASP.NET and Web Service การสร้างและเรียกใช้งาน Web Service บน .NET Framework
Go to : PHP - Calling .NET Web Service ใช้ PHP เรียกเว็บเซอร์วิส ของ .NET ด้วย nusoap


   
Share
Bookmark.   

  By : TC Admin
  Article : บทความเป็นการเขียนโดยสมาชิก หากมีปัญหาเรื่องลิขสิทธิ์ กรุณาแจ้งให้ทาง webmaster ทราบด้วยครับ
  Score Rating :
  Create Date : 2012-05-04
  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 05
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 อัตราราคา คลิกที่นี่