Ứng dụng khách nusoap PHP

Mục đích của hướng dẫn này là thảo luận chi tiết cách tạo dịch vụ web NuSOAP/PHP/SOAP bằng Adobe Dreamweaver CS4. Hướng dẫn này là lý tưởng cho người mới bắt đầu và các chuyên gia như nhau

Tôi đã viết một bài báo cách đây một thời gian thảo luận về cách phát triển các dịch vụ web SOAP/PHP bằng NuSOAP. Đây là một hướng dẫn rất chung chung và không đi sâu vào chi tiết liên quan đến việc thực sự viết dịch vụ web đầu tiên của bạn. Hướng dẫn này sử dụng một số mã từ trang web của Scott Nichol

điều kiện tiên quyết

  • Hướng dẫn này trích dẫn văn bản, mã và ý tưởng từ hướng dẫn Phát triển dịch vụ web SOAP ban đầu bằng PHP/C#. Tôi thực sự khuyên bạn nên đọc hướng dẫn đó trước khi đọc phần này
  • Đối với hướng dẫn này, tôi sử dụng Adobe Dreamweaver CS4, nhưng bạn có thể sử dụng bất kỳ thứ gì đơn giản (chẳng hạn như Notepad) hoặc phức tạp (chẳng hạn như Microsoft Expression Studio) tùy thích
  • Bài viết này giả định sự hiểu biết cơ bản về PHP
  • NuSOAP – Bộ công cụ SOAP cho PHP
  • Dịch vụ thông tin Internet (IIS) hoặc máy chủ web tương đương với PHP
  • Mã C# kèm theo hướng dẫn này dựa trên. NET Framework 2. 0. Các phiên bản cao hơn của. NET framework có thể không hoạt động

Giới thiệu

Dịch vụ web cung cấp cho chúng ta phương tiện liên lạc giữa cấu hình máy khách/máy chủ. Một dịch vụ web thực sự chỉ là một tập hợp các Giao diện lập trình ứng dụng (API) mà chúng ta có thể sử dụng để trao đổi dữ liệu (thường là qua web). SOAP [dẫn xuất XML] là giao thức thường được sử dụng để cung cấp tiêu chuẩn hóa trên tất cả các nền tảng và công nghệ

NuSOAP là một plug-in của bên thứ ba mang chức năng này vào PHP với tất cả các công việc chân tay được thực hiện cho chúng tôi

Giới thiệu về NuSOAP

NuSOAP cung cấp tất cả mã được viết sẵn mà chúng tôi cần để tạo dịch vụ web của mình

NuSOAP hỗ trợ các tính năng sau

  • Chạy độc lập. Không yêu cầu bất kỳ plug-in bổ sung hoặc cấu hình lại máy chủ nào
  • SOAP phiên bản 1. 1
  • WSDL (Ngôn ngữ mô tả dịch vụ web) 1. 1
  • HTTP
  • Các loại phức tạp

Đảm bảo rằng bạn đã tải xuống và giải nén NuSOAP và sẵn sàng sử dụng nó

Chào thế giới. Dịch vụ web mẫu

Cách nhanh nhất, dễ nhất để học là thực hành, vì vậy hãy đi thẳng vào vấn đề

C#

require_once("nuSOAP/lib/nusoap.php");
 
//Create a new soap server
$server = new soap_server();
 
//Define our namespace
$namespace = "http://localhost/nusoaphelloworld/index.php";
$server->wsdl->schemaTargetNamespace = $namespace;
 
//Configure our WSDL
$server->configureWSDL("HelloWorld");
 
// Register our method
$server->register('HelloWorld');
 
function HelloWorld()
{
return "Hello, World!";
}
 
// 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(); 

Điều này rất giống với mẫu mã ban đầu mà tôi đã viết, nhưng điều quan trọng là phải đặt nền móng. Hãy xem những gì đang xảy ra ở đây

  • Tạo một
    <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:import></xsd:import></xsd:schema>
    </types>
    <message name="HelloWorldRequest">
    <message name="HelloWorldResponse">
    <porttype name="HelloWorldPortType">
    <operation name="HelloWorld">
    <input message="tns:HelloWorldRequest" />
    <output message="tns:HelloWorldResponse">
    </output></operation>
    </porttype>
    <binding name="HelloWorldBinding" type="tns:HelloWorldPortType">
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http">
    <operation name="HelloWorld">
    <soap:operation soapaction="http://localhost/nusoaphelloworld/index.php/HelloWorld">
    <input /><soap:body use="encoded" 
    	encodingstyle="http://schemas.xmlsoap.org/soap/encoding/">
    <output><soap:body use="encoded" 
    	encodingstyle="http://schemas.xmlsoap.org/soap/encoding/" /></output>
    </soap:body></soap:operation></operation>
    </soap:binding></binding>
    <service name="HelloWorld">
    <port name="HelloWorldPort" binding="tns:HelloWorldBinding">
    <soap:address location="http://localhost/nusoaphelloworld/index.php">
    </soap:address></port>
    </service>
    </message></message></definitions> 
    0
  • Xác định không gian tên của máy chủ web và định cấu hình tài liệu WSDL của chúng tôi
  • Đăng ký phương thức
    <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:import></xsd:import></xsd:schema>
    </types>
    <message name="HelloWorldRequest">
    <message name="HelloWorldResponse">
    <porttype name="HelloWorldPortType">
    <operation name="HelloWorld">
    <input message="tns:HelloWorldRequest" />
    <output message="tns:HelloWorldResponse">
    </output></operation>
    </porttype>
    <binding name="HelloWorldBinding" type="tns:HelloWorldPortType">
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http">
    <operation name="HelloWorld">
    <soap:operation soapaction="http://localhost/nusoaphelloworld/index.php/HelloWorld">
    <input /><soap:body use="encoded" 
    	encodingstyle="http://schemas.xmlsoap.org/soap/encoding/">
    <output><soap:body use="encoded" 
    	encodingstyle="http://schemas.xmlsoap.org/soap/encoding/" /></output>
    </soap:body></soap:operation></operation>
    </soap:binding></binding>
    <service name="HelloWorld">
    <port name="HelloWorldPort" binding="tns:HelloWorldBinding">
    <soap:address location="http://localhost/nusoaphelloworld/index.php">
    </soap:address></port>
    </service>
    </message></message></definitions> 
    1 của chúng tôi
  • Viết phương pháp thực tế của chúng tôi
  • Xuất dữ liệu thô

Dịch vụ web này hiển thị một phương thức duy nhất, '

<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:import></xsd:import></xsd:schema>
</types>
<message name="HelloWorldRequest">
<message name="HelloWorldResponse">
<porttype name="HelloWorldPortType">
<operation name="HelloWorld">
<input message="tns:HelloWorldRequest" />
<output message="tns:HelloWorldResponse">
</output></operation>
</porttype>
<binding name="HelloWorldBinding" type="tns:HelloWorldPortType">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http">
<operation name="HelloWorld">
<soap:operation soapaction="http://localhost/nusoaphelloworld/index.php/HelloWorld">
<input /><soap:body use="encoded" 
	encodingstyle="http://schemas.xmlsoap.org/soap/encoding/">
<output><soap:body use="encoded" 
	encodingstyle="http://schemas.xmlsoap.org/soap/encoding/" /></output>
</soap:body></soap:operation></operation>
</soap:binding></binding>
<service name="HelloWorld">
<port name="HelloWorldPort" binding="tns:HelloWorldBinding">
<soap:address location="http://localhost/nusoaphelloworld/index.php">
</soap:address></port>
</service>
</message></message></definitions> 
1', chính phương thức này tạo ra một "
<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:import></xsd:import></xsd:schema>
</types>
<message name="HelloWorldRequest">
<message name="HelloWorldResponse">
<porttype name="HelloWorldPortType">
<operation name="HelloWorld">
<input message="tns:HelloWorldRequest" />
<output message="tns:HelloWorldResponse">
</output></operation>
</porttype>
<binding name="HelloWorldBinding" type="tns:HelloWorldPortType">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http">
<operation name="HelloWorld">
<soap:operation soapaction="http://localhost/nusoaphelloworld/index.php/HelloWorld">
<input /><soap:body use="encoded" 
	encodingstyle="http://schemas.xmlsoap.org/soap/encoding/">
<output><soap:body use="encoded" 
	encodingstyle="http://schemas.xmlsoap.org/soap/encoding/" /></output>
</soap:body></soap:operation></operation>
</soap:binding></binding>
<service name="HelloWorld">
<port name="HelloWorldPort" binding="tns:HelloWorldBinding">
<soap:address location="http://localhost/nusoaphelloworld/index.php">
</soap:address></port>
</service>
</message></message></definitions> 
3"
<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:import></xsd:import></xsd:schema>
</types>
<message name="HelloWorldRequest">
<message name="HelloWorldResponse">
<porttype name="HelloWorldPortType">
<operation name="HelloWorld">
<input message="tns:HelloWorldRequest" />
<output message="tns:HelloWorldResponse">
</output></operation>
</porttype>
<binding name="HelloWorldBinding" type="tns:HelloWorldPortType">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http">
<operation name="HelloWorld">
<soap:operation soapaction="http://localhost/nusoaphelloworld/index.php/HelloWorld">
<input /><soap:body use="encoded" 
	encodingstyle="http://schemas.xmlsoap.org/soap/encoding/">
<output><soap:body use="encoded" 
	encodingstyle="http://schemas.xmlsoap.org/soap/encoding/" /></output>
</soap:body></soap:operation></operation>
</soap:binding></binding>
<service name="HelloWorld">
<port name="HelloWorldPort" binding="tns:HelloWorldBinding">
<soap:address location="http://localhost/nusoaphelloworld/index.php">
</soap:address></port>
</service>
</message></message></definitions> 
0 khi được nâng lên

NuSOAP tạo một tài liệu WSDL giống hệt như thế này

XML

<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:import></xsd:import></xsd:schema>
</types>
<message name="HelloWorldRequest">
<message name="HelloWorldResponse">
<porttype name="HelloWorldPortType">
<operation name="HelloWorld">
<input message="tns:HelloWorldRequest" />
<output message="tns:HelloWorldResponse">
</output></operation>
</porttype>
<binding name="HelloWorldBinding" type="tns:HelloWorldPortType">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http">
<operation name="HelloWorld">
<soap:operation soapaction="http://localhost/nusoaphelloworld/index.php/HelloWorld">
<input /><soap:body use="encoded" 
	encodingstyle="http://schemas.xmlsoap.org/soap/encoding/">
<output><soap:body use="encoded" 
	encodingstyle="http://schemas.xmlsoap.org/soap/encoding/" /></output>
</soap:body></soap:operation></operation>
</soap:binding></binding>
<service name="HelloWorld">
<port name="HelloWorldPort" binding="tns:HelloWorldBinding">
<soap:address location="http://localhost/nusoaphelloworld/index.php">
</soap:address></port>
</service>
</message></message></definitions> 

Có rất nhiều điều khủng khiếp đang diễn ra ở đây, và thật may mắn là chúng ta không phải bận tâm đến hầu hết những điều đó. Khi sử dụng các công cụ tiêu dùng, chẳng hạn như Visual C# chẳng hạn, trình thông dịch sẽ đọc tài liệu WSDL, diễn giải phương thức nào đang được hiển thị, kiểu dữ liệu, kiểu phức tạp, tên và mọi thứ khác của chúng một cách tự động nên chúng tôi thực sự không phải làm gì

Bạn có thể dễ dàng xem tài liệu WSDL bằng cách gõ ?WSDL ở cuối tên tệp PHP của bạn trong thanh địa chỉ. (e. g. http. // localhost/nusoaphelloworld/index. php?WSDL)

Ví dụ ít tầm thường hơn

Ok, ví dụ trên rất đơn giản, nhưng bạn có thể làm nhiều hơn ở đây. Bạn thực sự có thể chuyển các giá trị cho các phương thức của mình để làm cho chúng hữu ích hơn. Vì vậy, giả sử bạn muốn chuyển 'tên' của một người vào chức năng của mình, sau đó xuất nó ra màn hình. Điều này có thể dễ dàng đạt được bằng cách thêm các tham số vào chức năng của bạn. Ví dụ

PHP

require_once("nuSOAP/lib/nusoap.php");
$namespace = "http://localhost/nusoaphelloworld/index.php";
 
// create a new soap server
$server = new soap_server();
 
// configure our WSDL
$server->configureWSDL("HelloExample");
 
// set our namespace
$server->wsdl->schemaTargetNamespace = $namespace;
 
//Register a method that has parameters and return types
$server->register(
// method name:
'HelloWorld',
// parameter list:
array('name'=>'xsd:string'),
// return value(s):
array('return'=>'xsd:string'),
// namespace:
$namespace,
// soapaction: (use default)
false,
// style: rpc or document
'rpc',
// use: encoded or literal
'encoded',
// description: documentation for the method
'Simple Hello World Method');
 
//Create a complex type
$server->wsdl->addComplexType('MyComplexType','complexType','struct','all','',
array( 'ID' => array('name' => 'ID','type' => 'xsd:int'),
'YourName' => array('name' => 'YourName','type' => 'xsd:string')));
 
//Register our method using the complex type
$server->register(
// method name:
'HelloComplexWorld',
// parameter list:
array('name'=>'tns:MyComplexType'),
// return value(s):
array('return'=>'tns:MyComplexType'),
// namespace:
$namespace,
// soapaction: (use default)
false,
// style: rpc or document
'rpc',
// use: encoded or literal
'encoded',
// description: documentation for the method
'Complex Hello World Method');
 
//Our Simple method
function HelloWorld($name)
{
return "Hello " . $name;
}
 
//Our complex method
function HelloComplexWorld($mycomplextype)
{
return $mycomplextype;
}
 
// 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(); 

Lưu ý rằng khi chúng tôi đăng ký phương thức của mình, chúng tôi có thể xác định một số tham số bổ sung cung cấp thêm thông tin. Chúng ta có thể quy định một danh sách các tham số (dưới dạng một mảng), dữ liệu trả về và thậm chí cả văn bản tài liệu để áp dụng

Nếu bạn xem dịch vụ web của mình trong trình duyệt web, bạn sẽ thấy một cái gì đó như thế này

Ứng dụng khách nusoap PHP

Bây giờ chúng ta đã hiểu cách chuyển các kiểu dữ liệu đơn giản (

<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:import></xsd:import></xsd:schema>
</types>
<message name="HelloWorldRequest">
<message name="HelloWorldResponse">
<porttype name="HelloWorldPortType">
<operation name="HelloWorld">
<input message="tns:HelloWorldRequest" />
<output message="tns:HelloWorldResponse">
</output></operation>
</porttype>
<binding name="HelloWorldBinding" type="tns:HelloWorldPortType">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http">
<operation name="HelloWorld">
<soap:operation soapaction="http://localhost/nusoaphelloworld/index.php/HelloWorld">
<input /><soap:body use="encoded" 
	encodingstyle="http://schemas.xmlsoap.org/soap/encoding/">
<output><soap:body use="encoded" 
	encodingstyle="http://schemas.xmlsoap.org/soap/encoding/" /></output>
</soap:body></soap:operation></operation>
</soap:binding></binding>
<service name="HelloWorld">
<port name="HelloWorldPort" binding="tns:HelloWorldBinding">
<soap:address location="http://localhost/nusoaphelloworld/index.php">
</soap:address></port>
</service>
</message></message></definitions> 
0s,
<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:import></xsd:import></xsd:schema>
</types>
<message name="HelloWorldRequest">
<message name="HelloWorldResponse">
<porttype name="HelloWorldPortType">
<operation name="HelloWorld">
<input message="tns:HelloWorldRequest" />
<output message="tns:HelloWorldResponse">
</output></operation>
</porttype>
<binding name="HelloWorldBinding" type="tns:HelloWorldPortType">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http">
<operation name="HelloWorld">
<soap:operation soapaction="http://localhost/nusoaphelloworld/index.php/HelloWorld">
<input /><soap:body use="encoded" 
	encodingstyle="http://schemas.xmlsoap.org/soap/encoding/">
<output><soap:body use="encoded" 
	encodingstyle="http://schemas.xmlsoap.org/soap/encoding/" /></output>
</soap:body></soap:operation></operation>
</soap:binding></binding>
<service name="HelloWorld">
<port name="HelloWorldPort" binding="tns:HelloWorldBinding">
<soap:address location="http://localhost/nusoaphelloworld/index.php">
</soap:address></port>
</service>
</message></message></definitions> 
2s, v.v. ) vào dịch vụ web của chúng tôi và cả cách trả về các loại dữ liệu đơn giản, tiếp theo là gì?

Các loại phức tạp

Tất cả các loại phức tạp đều sử dụng để tạo các loại dữ liệu tùy chỉnh, của riêng chúng tôi để xử lý dữ liệu có cấu trúc hơn, dễ dàng hơn. Các loại đơn giản (chẳng hạn như

<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:import></xsd:import></xsd:schema>
</types>
<message name="HelloWorldRequest">
<message name="HelloWorldResponse">
<porttype name="HelloWorldPortType">
<operation name="HelloWorld">
<input message="tns:HelloWorldRequest" />
<output message="tns:HelloWorldResponse">
</output></operation>
</porttype>
<binding name="HelloWorldBinding" type="tns:HelloWorldPortType">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http">
<operation name="HelloWorld">
<soap:operation soapaction="http://localhost/nusoaphelloworld/index.php/HelloWorld">
<input /><soap:body use="encoded" 
	encodingstyle="http://schemas.xmlsoap.org/soap/encoding/">
<output><soap:body use="encoded" 
	encodingstyle="http://schemas.xmlsoap.org/soap/encoding/" /></output>
</soap:body></soap:operation></operation>
</soap:binding></binding>
<service name="HelloWorld">
<port name="HelloWorldPort" binding="tns:HelloWorldBinding">
<soap:address location="http://localhost/nusoaphelloworld/index.php">
</soap:address></port>
</service>
</message></message></definitions> 
0 và
<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:import></xsd:import></xsd:schema>
</types>
<message name="HelloWorldRequest">
<message name="HelloWorldResponse">
<porttype name="HelloWorldPortType">
<operation name="HelloWorld">
<input message="tns:HelloWorldRequest" />
<output message="tns:HelloWorldResponse">
</output></operation>
</porttype>
<binding name="HelloWorldBinding" type="tns:HelloWorldPortType">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http">
<operation name="HelloWorld">
<soap:operation soapaction="http://localhost/nusoaphelloworld/index.php/HelloWorld">
<input /><soap:body use="encoded" 
	encodingstyle="http://schemas.xmlsoap.org/soap/encoding/">
<output><soap:body use="encoded" 
	encodingstyle="http://schemas.xmlsoap.org/soap/encoding/" /></output>
</soap:body></soap:operation></operation>
</soap:binding></binding>
<service name="HelloWorld">
<port name="HelloWorldPort" binding="tns:HelloWorldBinding">
<soap:address location="http://localhost/nusoaphelloworld/index.php">
</soap:address></port>
</service>
</message></message></definitions> 
2) chỉ được sử dụng hạn chế. Điều gì sẽ xảy ra nếu chúng ta có thể tạo các kiểu của riêng mình?
Create your complex type;

Sử dụng phương pháp

<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:import></xsd:import></xsd:schema>
</types>
<message name="HelloWorldRequest">
<message name="HelloWorldResponse">
<porttype name="HelloWorldPortType">
<operation name="HelloWorld">
<input message="tns:HelloWorldRequest" />
<output message="tns:HelloWorldResponse">
</output></operation>
</porttype>
<binding name="HelloWorldBinding" type="tns:HelloWorldPortType">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http">
<operation name="HelloWorld">
<soap:operation soapaction="http://localhost/nusoaphelloworld/index.php/HelloWorld">
<input /><soap:body use="encoded" 
	encodingstyle="http://schemas.xmlsoap.org/soap/encoding/">
<output><soap:body use="encoded" 
	encodingstyle="http://schemas.xmlsoap.org/soap/encoding/" /></output>
</soap:body></soap:operation></operation>
</soap:binding></binding>
<service name="HelloWorld">
<port name="HelloWorldPort" binding="tns:HelloWorldBinding">
<soap:address location="http://localhost/nusoaphelloworld/index.php">
</soap:address></port>
</service>
</message></message></definitions> 
5 được tích hợp sẵn của NuSOAP để tạo kiểu phức tạp của riêng bạn

PHP

//Create a complex type
$server->wsdl->addComplexType('MyComplexType','complexType','struct','all','',
array( 'ID' => array('name' => 'ID','type' => 'xsd:int'),
'YourName' => array('name' => 'YourName','type' => 'xsd:string'))); 

Phương pháp rất đơn giản. Đặt tên cho loại phức hợp của bạn (

<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:import></xsd:import></xsd:schema>
</types>
<message name="HelloWorldRequest">
<message name="HelloWorldResponse">
<porttype name="HelloWorldPortType">
<operation name="HelloWorld">
<input message="tns:HelloWorldRequest" />
<output message="tns:HelloWorldResponse">
</output></operation>
</porttype>
<binding name="HelloWorldBinding" type="tns:HelloWorldPortType">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http">
<operation name="HelloWorld">
<soap:operation soapaction="http://localhost/nusoaphelloworld/index.php/HelloWorld">
<input /><soap:body use="encoded" 
	encodingstyle="http://schemas.xmlsoap.org/soap/encoding/">
<output><soap:body use="encoded" 
	encodingstyle="http://schemas.xmlsoap.org/soap/encoding/" /></output>
</soap:body></soap:operation></operation>
</soap:binding></binding>
<service name="HelloWorld">
<port name="HelloWorldPort" binding="tns:HelloWorldBinding">
<soap:address location="http://localhost/nusoaphelloworld/index.php">
</soap:address></port>
</service>
</message></message></definitions> 
6), cho biết tài liệu WSDL của bạn rằng đó là loại phức tạp (
<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:import></xsd:import></xsd:schema>
</types>
<message name="HelloWorldRequest">
<message name="HelloWorldResponse">
<porttype name="HelloWorldPortType">
<operation name="HelloWorld">
<input message="tns:HelloWorldRequest" />
<output message="tns:HelloWorldResponse">
</output></operation>
</porttype>
<binding name="HelloWorldBinding" type="tns:HelloWorldPortType">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http">
<operation name="HelloWorld">
<soap:operation soapaction="http://localhost/nusoaphelloworld/index.php/HelloWorld">
<input /><soap:body use="encoded" 
	encodingstyle="http://schemas.xmlsoap.org/soap/encoding/">
<output><soap:body use="encoded" 
	encodingstyle="http://schemas.xmlsoap.org/soap/encoding/" /></output>
</soap:body></soap:operation></operation>
</soap:binding></binding>
<service name="HelloWorld">
<port name="HelloWorldPort" binding="tns:HelloWorldBinding">
<soap:address location="http://localhost/nusoaphelloworld/index.php">
</soap:address></port>
</service>
</message></message></definitions> 
7) và sau đó chỉ định các tham số

Trong ví dụ trên, chúng ta có hai thuộc tính trong kiểu của mình;

Sau đó, chúng ta phải đăng ký phương thức của mình với kiểu phức tạp được chỉ định làm kiểu dữ liệu thay vì kiểu đơn giản. (Chúng tôi sử dụng tns thay vì xsd ở đây. )

PHP

<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:import></xsd:import></xsd:schema>
</types>
<message name="HelloWorldRequest">
<message name="HelloWorldResponse">
<porttype name="HelloWorldPortType">
<operation name="HelloWorld">
<input message="tns:HelloWorldRequest" />
<output message="tns:HelloWorldResponse">
</output></operation>
</porttype>
<binding name="HelloWorldBinding" type="tns:HelloWorldPortType">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http">
<operation name="HelloWorld">
<soap:operation soapaction="http://localhost/nusoaphelloworld/index.php/HelloWorld">
<input /><soap:body use="encoded" 
	encodingstyle="http://schemas.xmlsoap.org/soap/encoding/">
<output><soap:body use="encoded" 
	encodingstyle="http://schemas.xmlsoap.org/soap/encoding/" /></output>
</soap:body></soap:operation></operation>
</soap:binding></binding>
<service name="HelloWorld">
<port name="HelloWorldPort" binding="tns:HelloWorldBinding">
<soap:address location="http://localhost/nusoaphelloworld/index.php">
</soap:address></port>
</service>
</message></message></definitions> 
6

Trong trường hợp này, chúng tôi đã nói rằng chúng tôi muốn tham số của mình thuộc loại

<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:import></xsd:import></xsd:schema>
</types>
<message name="HelloWorldRequest">
<message name="HelloWorldResponse">
<porttype name="HelloWorldPortType">
<operation name="HelloWorld">
<input message="tns:HelloWorldRequest" />
<output message="tns:HelloWorldResponse">
</output></operation>
</porttype>
<binding name="HelloWorldBinding" type="tns:HelloWorldPortType">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http">
<operation name="HelloWorld">
<soap:operation soapaction="http://localhost/nusoaphelloworld/index.php/HelloWorld">
<input /><soap:body use="encoded" 
	encodingstyle="http://schemas.xmlsoap.org/soap/encoding/">
<output><soap:body use="encoded" 
	encodingstyle="http://schemas.xmlsoap.org/soap/encoding/" /></output>
</soap:body></soap:operation></operation>
</soap:binding></binding>
<service name="HelloWorld">
<port name="HelloWorldPort" binding="tns:HelloWorldBinding">
<soap:address location="http://localhost/nusoaphelloworld/index.php">
</soap:address></port>
</service>
</message></message></definitions> 
6và cũng trả về loại dữ liệu
<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:import></xsd:import></xsd:schema>
</types>
<message name="HelloWorldRequest">
<message name="HelloWorldResponse">
<porttype name="HelloWorldPortType">
<operation name="HelloWorld">
<input message="tns:HelloWorldRequest" />
<output message="tns:HelloWorldResponse">
</output></operation>
</porttype>
<binding name="HelloWorldBinding" type="tns:HelloWorldPortType">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http">
<operation name="HelloWorld">
<soap:operation soapaction="http://localhost/nusoaphelloworld/index.php/HelloWorld">
<input /><soap:body use="encoded" 
	encodingstyle="http://schemas.xmlsoap.org/soap/encoding/">
<output><soap:body use="encoded" 
	encodingstyle="http://schemas.xmlsoap.org/soap/encoding/" /></output>
</soap:body></soap:operation></operation>
</soap:binding></binding>
<service name="HelloWorld">
<port name="HelloWorldPort" binding="tns:HelloWorldBinding">
<soap:address location="http://localhost/nusoaphelloworld/index.php">
</soap:address></port>
</service>
</message></message></definitions> 
6

Sự tiêu thụ

Bạn có thể sử dụng (sử dụng, triển khai) các dịch vụ web từ bất kỳ nền tảng/ngôn ngữ nào hỗ trợ chúng. Ví dụ này sử dụng Visual C# 2010 trên Microsoft Windows Vista

Ghi chú. Hướng dẫn này sử dụng Visual C# 2010, nhưng cũng sử dụng. NET Framework 2. 0

Đọc hướng dẫn đầy đủ về mức tiêu thụ Visual C# tại đây

Mã của bạn sẽ trông giống như thế này

C#

<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:import></xsd:import></xsd:schema>
</types>
<message name="HelloWorldRequest">
<message name="HelloWorldResponse">
<porttype name="HelloWorldPortType">
<operation name="HelloWorld">
<input message="tns:HelloWorldRequest" />
<output message="tns:HelloWorldResponse">
</output></operation>
</porttype>
<binding name="HelloWorldBinding" type="tns:HelloWorldPortType">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http">
<operation name="HelloWorld">
<soap:operation soapaction="http://localhost/nusoaphelloworld/index.php/HelloWorld">
<input /><soap:body use="encoded" 
	encodingstyle="http://schemas.xmlsoap.org/soap/encoding/">
<output><soap:body use="encoded" 
	encodingstyle="http://schemas.xmlsoap.org/soap/encoding/" /></output>
</soap:body></soap:operation></operation>
</soap:binding></binding>
<service name="HelloWorld">
<port name="HelloWorldPort" binding="tns:HelloWorldBinding">
<soap:address location="http://localhost/nusoaphelloworld/index.php">
</soap:address></port>
</service>
</message></message></definitions> 
9

Bạn có thể tìm thấy mã này và hơn thế nữa trong ứng dụng demo

Bản thử trực tiếp

Dịch vụ web được thảo luận trong hướng dẫn này đang hoạt động và có thể được xem/sử dụng bất kỳ lúc nào

  • http. //www. jpreece. com/nusoaphelloworld/index. php

Môn lịch sử

  • ngày 27 tháng 12 năm 2010. bài đăng ban đầu

Giấy phép

Bài viết này, cùng với mọi tệp và mã nguồn liên quan, được cấp phép theo Giấy phép Công cộng GNU (GPLv3)

Làm cách nào để sử dụng ứng dụng khách SOAP trong PHP?

NuSOAP trong PHP là gì?

NuSOAP là bản viết lại của SOAPx4, do NuSphere và Dietrich Ayala cung cấp . Nó là một tập hợp các lớp PHP - không yêu cầu phần mở rộng PHP - cho phép các nhà phát triển tạo và sử dụng các dịch vụ web dựa trên SOAP 1. 1, WSDL 1. 1 và HTTP1. 0/1. 1.

Làm cách nào để gọi dịch vụ Web SOAP trong PHP?

Gọi dịch vụ SOAP . Giả sử dịch vụ web SOAP của bạn có tệp WSDL, bạn chỉ cần khởi tạo ứng dụng khách SOAP bằng tệp WSDL và ứng dụng khách sẽ tạo một đối tượng với các phương thức cho từng phương thức do tệp WSDL cung cấp. ext/soap is a pretty easy to way to consume SOAP web services. Assuming your SOAP web service has a WSDL file, you just instantiate a SOAP client with the WSDL file, and the client will make an object with methods for each method offered by the WSDL file.

NuSOAP trong các dịch vụ Web là gì?

NuSOAP là Bộ công cụ SOAP dành cho PHP không yêu cầu phần mở rộng PHP .