PHP Learning: Use instances of soap in PHP and generate WSDL files, provide a class library that automatically generates WSDL files?? SoapDiscovery.class.php class

Source: Internet
Author: User
1. Web Service Popularity: Webservice The difference between soap WSDL and personal insights

Web Service realizes business demand: Web Service is the one that really "acts", provides a general term of the interface.
WSDL provides a "document description of what can be done": A descriptive format for the service to be provided. I want to help you, but I'm going to tell you what I can do, and what kind of parameters I need to do these things.
SOAP provides the specification of "request": The format of the request 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 "request" format of the SOAP definition "write" the request can ensure that the Web service can correctly interpret what you want it to do and what parameters you provide for it. The main issues you need to describe in this request are: which Web service to send the request to, the parameter type of the request, the parameter value, the return value type. These are all "filled in" and a SOAP message conforming to the SOAP specification is completed.

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 recognized standard, with interfaces provided by WSDL that can access different types of resources (such as Java, C #, C, C, C + +, and so on), because WSDL is XML-based and language platform agnostic. In addition, the WSDL provides binding and service elements to bind the interface to the specific service, realizing the separation between the interface and the implementation.

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

The relationship between WSDL and soap is: the WSDL binding service can be set to use the Protocol, the Protocol can be SOAP, HTTP, SMTP, FTP and so on any kind of transport protocol, in addition to the WSDL can also bind JMS, EJB and local Java, etc. However, it is necessary to extend the binding and service elements and to extend the capabilities of the server to support this extension

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

Simple Object Access Protocol (SOAP) is a note from the World Trade Organization, which describes a lightweight protocol for exchanging information in a decentralized or distributed environment. SOAP is an XML-based protocol that consists of three parts: the soap wrapper (Envelop), which defines a framework that describes what the content in the message is, who sent it, who should accept and handle it, and how to handle it; SOAP encoding Rules (Encoding rules), An instance that represents 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 that can be bound to a variety of transport protocols (binding), exchanging information using the underlying protocol. In this document, only the framework for how SOAP is bound to HTTP and HTTP extensions is currently defined.

SOAP is a communication protocol, SOAP on the basis of the HTTP protocol, the request parameters written as XML, placed on the HTTP body on the submission of a Web service server (servlet,asp, etc.) processing is completed, The result is also written as XML as response back to the client, in order to make the client and the Web service can correspond to each other, you can use WSDL as a description of this mode of communication, using the WSDL tool can automatically 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, while SOAP uses the HTTP protocol as the basic protocol, using port 80 allows soap to complete the RPC function through the firewall.

The SOAP protocol, like the HTTP protocol, is the underlying communication protocol, but the format of the request package is different, the SOAP packet is in XML format, and now we do not need to understand soap in depth to write a Web service. If the service and client are using SOAP in the same environment, there is usually a tool for automatically generating the SOAP program framework, so it's okay not to know the details. However, if the client and service environment are different, such as Java's client and. NET service to communicate, or VB client and Tomcat under the Java Service communication, or to know a little bit of detail as well. In particular, WSDL or UDDI is not a standard, but if not used, you have 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, extensibility is good

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, it is difficult to create a WSDL file in XML format, which you first understand what is xml! Learn XML Syntax! But for a person who is eager to solve the problem! Not so much time to get acquainted! So this is a matter of annoyance! But no hurry, it says, there is a way to not need a WSDL file! Also, this tutorial provides a class that automatically generates WSDL files!

Before you explain, configure the SOAP environment support for PHP:
Locate php.ini File
; Extension=php_soap.dll
Delete ";", restart Apache server

First, there is a WSDL file mode
Here we first introduce the standard webservice. So how do you create WSDL? This is a really hard thing for PHP, and some people say it's easy to create it with Zend Studio, which is a method. 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 (I have downloaded locally), there is a common method: getwsdl (), this method at the end of the return statement, then you modify this method:
return sprintf ('%s%s%s%s%s%s ', $headerWSDL, $portTypeWSDL, $bindingWSDL, $serviceWSDL, $messageWSDL, ');
Generate the WSDL file and remove the return comment above, then
$fso = fopen ($this->class_name. ". wsdl", "w");
Fwrite ($fso, sprintf ('%s%s%s%s%s%s ', $headerWSDL, $portTypeWSDL, $bindingWSDL, $serviceWSDL, $messageWSDL, "));
Now the class that generated the WSDL has, SoapDiscovery.class.php (source at the very end).
You can create a WSDL by preparing a service.php class file or function that provides the service!
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
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: SERVICE.WSDL below), which is the service class, and the second parameter is the name of the services (this can be casually written)
$disc->getwsdl ();
# #说明: By viewing the source discovery of the "SoapDiscovery.class.php" file, this member function returns an XML-formatted string if the return statement is opened, if $strXML = $disc->getwsdl (); Echo $strXML, the WSDL file for the XML will not be displayed in the browser. Because the browser defaults to "Html/text" to return the contents of stdout, it needs to be used in the header of the PHP script:
Header ("content-type:text/html; Charset=utf-8 "); Can let the browser not to the output as HTML but as XML to parse!
Run the create_wsdl.php file with Web Access, a service.wsdl file will be generated, and 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 under Linux does not have a server IP address. So to modify this code: $_server[' server_name '] is $_server[' server_host ', because under Linux (Nginx), the former variable is empty, so that the correct WSDL document can be generated!


and add some code to the service.php file.
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)); # #此处的Service. wsdl file is the one generated above
$server->setclass ("Service"); Registering all methods of the service class
$server->handle (); Processing requests
?>

Create the WebService client program to test whether the webservice is valid and the file name is: client.php
Copy in the following:
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
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
try {
$soap = new SoapClient (NULL, Array (
"Location" = "http://localhost/Dragon/soap/Service.php",
"uri" = "ABCD",//Resource descriptor server and client must correspond to
"Style" = Soap_rpc,
"Use" = soap_encoded
));

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

  • 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.