PHP Learning: Using instances of soap in PHP and generating WSDL files, providing the class library--soapdiscovery.class.php classes that actively generate WSDL files

Source: Internet
Author: User
Tags php class sprintf wsdl

1. Web Service Popularity : Webservice personal Insights on the differences between soap WSDL

web service to achieve business demand:  web service is really" the one ", Provides a general designation of a service interface.
wsdl provides "documentation of what can be done description ":   A descriptive narrative format for the services to be provided. I want to help you, but I'm going to tell you what I can do, and what type of participation you need to do these things.
soap provides spec :   Pass the request format to the service interface, including methods and parameters. You want to let people do things, you have to tell people what you want to do, soap is the format of the definition of this "request", according to the SOAP definition of "request" format "write" the request to ensure that the Web service can correctly interpret what you want it to do and you provide it with what the number of references. In this request, you need to write a description of the main problem: which Web service to send the request, the type of the request, the number of parameters, the return value type. These are all "filled in" and the SOAP message that conforms to the SOAP specification is finished.

While WSDL and soap are two of the standards of Web service, they are not necessarily connected and can be used independently.
WSDL provides a unified interface that has now become an internationally accepted standard for interfaces provided by WSDL to access different types of resources (such as Java, C #, C, C, C + +, etc.), because WSDL is XML-based and language platform agnostic. In addition, the WSDL provides binding and service elements to bind the interface to the detailed service, realizing the separation between the interface and the implementation.

Soap (Simple Object Access Protocol) is an HTTP-based transport protocol that is used to interview remote services

the relationship between WSDL and soap is that the WSDL binding service is able to set the protocol used, the Protocol can be SOAP, HTTP, SMTP, FTP, and so on whatever kind of transport protocol, in addition to the WSDL can also bind JMS, EJB and Local Java and so on, just need to extend the binding and service elements, and need to extend the functionality of the server to support such an extension

The SOAP protocol is a request and response protocol specification, and HTTP is the Web transport protocol, and SOAP transfers are HTTP-based, but can also be based on other transport protocols such as FTP, SMTP, and so on.

Simple Object Access Protocol (SOAP) is a note by the World Trade Organization that describes a lightweight protocol for exchanging information in a decentralized or distributed environment. SOAP is an XML-based protocol that contains three parts: the soap wrapper (Envelop), which defines a framework that describes what the content in the narrative message is, who sent it, who should accept and handle it, and how to deal with it; SOAP Encoding Rules (Encoding Rules), which represents an instance of the data type that the application needs to use, a SOAP RPC representation (RPC representation), a contract that represents a remote procedure call and an answer, and a soap capable of exchanging information with a variety of transport protocol bindings (binding) using the underlying protocol. In this document, the framework for how SOAP is bound to HTTP and HTTP extensions is now only defined.

SOAP is a communication protocol, SOAP on the basis of the HTTP protocol, the write XML request to the HTTP body on the submission of a Web serviceserver (servlet,asp what) after processing, The result is also written as XML as response back to the client, in order to enable the client and Web service can be corresponding to each other, can use WSDL as a descriptive narrative file of such communication, using the WSDL tool to proactively generate WS and Client framework files, SOAP has the ability to serialize complex objects into XML.

SOAP is the predecessor of RPC, is the protocol of Remote Call processing, this Protocol security is not very good, most firewalls will block RPC communication packets, and SOAP uses the HTTP protocol as the main protocol, using PORT80 allows soap to pass through the firewall, the function of RPC.

The SOAP protocol, like the HTTP protocol, is the underlying communication protocol, just the format of the request package is the same, the SOAP packet is in XML format, and today we write Web service does not need to deep understanding of soap does not matter. Assume that the service and client use soap in the same environment, because there is a common case where you have the tools to actively generate the SOAP program framework, so it's okay not to know the details. However, let's say that the client and service environments are different, such as the Java client and. NET service, or the Java service communication under VB Client and Tomcat, or a little bit of detail. In particular, WSDL or UDDI is not a standard, assuming that it is only good to manually formulate a SOAP message.

2. Use the class "SoapDiscovery.class.php" to generate the WSDL document!

interface of SOAP Type
Irrelevant programming language, irrelevant platform, very good extensibility

To implement a SOAP-type interface, there are two ways: a WSDL file, a non-WSDL file mode!
For people who love research, the first way is to give you a clear idea of how PHP created a Web service! But the first one for beginners, creating a WSDL file in XML format is harder, which you first know what is xml! Learn XML Syntax! But for a person who is eager to solve this problem! Not so much time to get acquainted! So this is a matter of annoyance! Just don't worry, it said, another way without WSDL file! Also, this commentary provides a class that generates WSDL files on its own initiative!

Before the commentary, configure the SOAP environment support for PHP:
Locate php.ini File
; Extension=php_soap.dll
Delete ";", restart Apacheserver

first, there is a WSDL file mode
Here we first introduce the standard webservice. So how do you create WSDL? This is really not easy for PHP, it is said that it is very convenient to create it with Zend Studio, which is a way of doing it. But for those of you who don't like using Zend Studio, it's too much to think of creating a Web service and installing Zend Studio.
Here is a simple way to download the SoapDiscovery.class.php class online (I have a local download), there is aCommon methods: getwsdl (), this method ends with a return statement, so you change this method:
return sprintf ('%s%s%s%s%s%s ', $headerWSDL, $portTypeWSDL, $bindingWSDL, $serviceWSDL, $messageWSDL, ' </ Definitions> ');
Generate the WSDL file, remove the above return gaze, then
$fso = fopen ($this->class_name. ". wsdl", "w");
Fwrite ($fso, sprintf ('%s%s%s%s%s%s ', $headerWSDL, $portTypeWSDL, $bindingWSDL, $serviceWSDL, $messageWSDL, ' </ Definitions> '));
Now the class that generates the WSDL has, SoapDiscovery.class.php (source code at the very end).
You can create a WSDL by preparing a service.php class file or function that provides the service!
<?php
Class Service {

Public Function HelloWorld () {
return "Hello";
}

Public function Add ($a, $b) {
return $a + $b;
}

}
?>

The following creates a WSDL file for the creat_wsdl.php
<?php
Include ("service.php");
Include ("SoapDiscovery.class.php");

$disc = new Soapdiscovery (' Service ', ' soap '); The first parameter is the class name (the generated WSDL file is named after it, for example: The following service.wsdl), the service class, and the second parameter is the name of the services (this can be written casually)
$disc->getwsdl ();
# #说明: By looking at source code discovery for the "SoapDiscovery.class.php" file, this member function assumes that the return statement is turned on, returning an XML-formatted string, assuming $strXML = $disc->getwsdl () echo $strXML; The WSDL file for the XML will not be displayed in the browser. Since the browser defaults to "Html/text" to return the contents of the stdout, it needs to be used in the header of the PHP script:
header ("content-type:text/html; Charset=utf-8 "); Talent enough to let the browser do not use the output as HTML but as XML to parse!
Executes the create_wsdl.php file in a web-access manner and generates aSERVICE.WSDL's File, this file will be used here!
Note: The WSDL file generated by the above method is no problem under Windows, but the "Location=xxx" segment in the WSDL file below Linux does not have a ServerIP address. So toChange the code in: $_server[' server_name '] for $_server[' server_host '], because under the Linux (Nginx), the former variable is empty, so talent to generate the correct WSDL document!


and add it to the service.php file? Some code
<?php
Class Service {

Public Function HelloWorld () {
return "Hello";
}

Public function Add ($a, $b) {
return $a + $b;
}

}

$server = new soapserver('Service. wsdl ', array (' soap_version ' = soap_1_2)); # #此处的 The service.wsdl file is generated above
$server-SetClass("Service"); All methods of service class
$server->handle (); Processing requests
?>

CreateWebService ClientProgram, test webservice is valid, file name is: client.php
Copy the following content:
<?php
Ini_set (' soap.wsdl_cache_enabled ', "0"); Turn off the WSDL cache
$soap = new SoapClient (' http://localhost/Dragon/soap/Service.php?wsdl ');
Echo $soap->add (28, 2);
echo $soap->__soapcall (' Add ', Array (28,2))//or so called
#echo $soap->__call (' Add ', Array (28,2));

?>
Ok! Test pass!

  

Two, no WSDL file mode
Server Side
<?php
Class Service
{
Public Function HelloWorld ()
{
return "Hello";
}
Public function Add ($a, $b)
{
return $a + $b;
}
}
$server =new soapserver (null,array (' uri ' = ' abcd '));
$server->setclass ("Service");
$server->handle ();
?>
Client
<?php
try {
$soap = new SoapClient (NULL, Array (
"Location" = "http://localhost/Dragon/soap/Service.php",
"uri" = "ABCD",//resource descriptive descriptor server and client must be corresponding
"Style" = Soap_rpc,
"Use" = soap_encoded
));

Echo $soap->add (12, 2);
} catch (Exction $e) {
Echo Print_r ($e->getmessage (), true);
}
?>

PHP Learning: Using instances of soap in PHP and generating WSDL files, providing the class library--soapdiscovery.class.php classes that actively generate WSDL files

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.